Object Oriented Programming(OOP)/Object Oriented Design(OOD)
- Inheritance(繼承)
- Composition(復(fù)合)
- Delegation(委托)
Composition(復(fù)合)秸侣,表示 has-a
template <class T, class Sequence = deque<T>>
class queue
{
…
protected:
Sequence c; // 底層容器
public:
// 以下完全利用 c 的操作函數(shù)完成
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
reference front() { return c.front(); }
reference back() { return c.back(); }
// deque 是兩端可進(jìn)出多矮, queue 是末端進(jìn)前端出(先進(jìn)先出)
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_front(); }
};
以上的設(shè)計(jì)模式被稱為Adapter
,將原有的類包裝滴铅,只開放部分功能弃衍。
構(gòu)造由內(nèi)而外
Container 的構(gòu)造函數(shù)首先調(diào)用 Component 的 default 構(gòu)造函數(shù)招拙,然后才執(zhí)行自己前联。(若要執(zhí)行非默認(rèn)的構(gòu)造函數(shù)证鸥,需要手動(dòng)寫出要調(diào)用的構(gòu)造函數(shù))
Container::Container(…): Component() { … };
析構(gòu)由外而內(nèi)
Container 的析構(gòu)函數(shù)首先執(zhí)行自己僚楞,然后才調(diào)用 Component 的析構(gòu)函數(shù)。
Container::~Container(…) { … ~Component(); };
Delegation(委托). Composition by reference.
// file String.hpp
class StringRep;
class String
{
public:
String();
String(const char* s);
String(const String& s);
String& operator=(const String& s);
~String();
…
private:
StringRep* rep; // pimpl
};
// file String.cpp
#include "String.cpp"
namespace
{
class StringRep
{
friend class String;
StringRep(const char* s);
~StringRep();
int count;
char* rep;
};
}
String::String() { …; }
…
以上的設(shè)計(jì)模式被稱為Handle/Body(pImpl)
枉层,里面包含一個(gè)指針指向?qū)崿F(xiàn)全部功能的類泉褐。
count
的功能是做reference counting
。
Inheritance(繼承)鸟蜡,表示 is-a
struct _List_node_base
{
_List_node_base* _M_next;
_List_node_base* _M_prev;
};
template<typename _Tp>
struct _List_node :public _List_node_base
{
_Tp _M_data;
};
構(gòu)造由內(nèi)而外
Derived 的構(gòu)造函數(shù)首先調(diào)用 Base 的 default 的構(gòu)造函數(shù)膜赃,然后才執(zhí)行自己。
Derived::Derived(…): Base() { … };
析構(gòu)由外而內(nèi)
Derived 的析構(gòu)函數(shù)首先執(zhí)行自己揉忘,然后才調(diào)用 Base 的析構(gòu)函數(shù)跳座。
Derived::~Derived(…) { … ~Base(); };
注意:base class 的 dtor 必須是 virtual 端铛,否則會(huì)出現(xiàn) undefined behavior。
Inheritance(繼承) with virtual functions(虛函數(shù))
- non-virtual 函數(shù):你不希望 derived class 重新定義(override疲眷,覆寫)它沦补。
- virtual 函數(shù):你希望derived class 重新定義(override,覆寫)它咪橙,且你對(duì)它已有默認(rèn)定義夕膀。
- pure virtual 函數(shù):你希望 derived class 一定要重新定義(override,覆寫)它美侦,你對(duì)它沒有默認(rèn)定義产舞。
class Shape
{
public:
virtual void draw() const = 0; // pure virtual
virtual void error(const std::string& msg); // impure virtual
int objectID() const; // non-virtual
};
class Rectangle:public Shape{ … };
class Ellipse:public Shape{ … };
以下的設(shè)計(jì)模式被稱為Template Method
。
#include <iostream>
using namespace std;
class CDocument
{
public:
void OnFileOpen()
{
// 這是個(gè)算法菠剩,每個(gè) cout 輸出代表一個(gè)實(shí)際動(dòng)作
cout << "dialog..." << endl;
cout << "check file status..." << endl;
cout << "open file..." << endl;
Serialize();
cout << "close file..." << endl;
cout << "update all views..." << endl;
}
virtual void Serialize() { };
};
class CMyDoc :public CDocument
{
public:
virtual void Serialize()
{
// 只有應(yīng)用程序本身才知道如何讀取自己的文件(格式)
cout << "CMyDoc::Serialize()" << endl;
}
};
int main()
{
CMyDoc myDoc; // 假設(shè)對(duì)應(yīng)[File/Open]
myDoc.OnFileOpen();
}
Inheritance+Composition 關(guān)系下的構(gòu)造和析構(gòu)
測(cè)試代碼如下:
#include <iostream>
class Base;
class Component;
class Derived;
class Base
{
public:
Base()
{
std::cout << "The ctor of class Base has completed" << std::endl;
}
};
class Component
{
public:
Component()
{
std::cout << "The ctor of class Component has completed" << std::endl;
}
};
class Derived :public Base
{
public:
Derived(int n) :n(n)
{
std::cout << "The ctor of class Derived has completed" << std::endl;
}
private:
int n;
Component a;
};
int main()
{
Derived d(2);
system("pause");
return 0;
}