C++ 設(shè)計(jì)模式的基本原則
設(shè)計(jì)模式基本原則
最終目的,高內(nèi)聚,低耦合
開放封閉原則類的改動(dòng)是通過增加代碼進(jìn)行的,不是修改源代碼
銀行類案例代碼
- #include<iostream>
- using namespace std;
- class BankWorker
- {
- public:
- void save()
- {
- cout << "存款" << endl;
- }
- void moveM()
- {
- cout << "取款" << endl;
- }
- void jiaofei()
- {
- cout << "繳費(fèi)" << endl;
- }
- };
- class AbBankWorker
- {
- public:
- virtual void dothing() = 0;
- };
- class SaveBanker :public AbBankWorker
- {
- public:
- virtual void dothing()
- {
- cout << "存款" << endl;
- }
- };
- class MoveBanker :public AbBankWorker
- {
- public:
- virtual void dothing()
- {
- cout << "取款" << endl;
- }
- };
- class SaveBanker :public AbBankWorker
- {
- public:
- virtual void dothing()
- {
- cout << "繳費(fèi)款" << endl;
- }
- };
- void main11()
- {
- BankWorker*bw = new BankWorker;
- bw->jiaofei();
- bw->moveM();
- bw->save();
- cout << "hello..." << endl;
- system("pause");
- return;
- }
- void main22()
- {
- AbBankWorker*bw = NULL;
- bw=new MoveBanker;
- bw->dothing();
- delete bw;
- return;
- }
- void main()
- {
- main22();
- system("pause");
- return;
- }
單一職責(zé)原則類的職責(zé)要單一,對(duì)外只提供一種功能,而引起內(nèi)變化的原因都應(yīng)該只有一個(gè)
依賴倒置原則依賴于抽象接口,不要依賴具體的實(shí)現(xiàn)類,也就是針對(duì)接口編程
- #include<iostream>
- using namespace std;
- class HardDisk
- {public:
- virtual void work();
- };
- class Memory
- {
- public:
- virtual void work();
- };
- class Cpu
- {
- public:
- virtual void work();
- };
- class ComPuter
- {
- public:
- ComPuter(HardDisk*m_handdisk, Memory*m_memory, Cpu*m_cpu)
- {
- m_handdisk = handdisk;
- m_memory = memory;
- m_cpu = cpu;
- }
- public:
- void work()
- {
- m_handdisk->work();
- m_memory->work();
- m_cpu->work();
- }
- private:
- HardDisk*m_handdisk;
- Memory*m_memory;
- Cpu*m_cpu;
- };
- class InterCpu :public Cpu
- {
- public:
- void work()
- {
- cout << "我是因特爾廠家" << endl;
- }
- };
- class XSDisk :public HardDisk
- {
- public:
- void work()
- {
- cout << "我是西數(shù)硬盤廠家" << endl;
- }
- };
- class JSDMem :public Memory
- {
- public:
- void work()
- {
- cout << "我是JSDMem廠家" << endl;
- }
- };
- void main()
- {
- HardDisk*handdisk=NULL;
- Memory*memory=NULL;
- Cpu*cpu=NULL;
- handdisk = new XSDisk;
- memory= new JSDMem;
- cpu = new InterCpu;
- ComPuter*mycomputer = new ComPuter(harddisk, memory, cpu);
- mycomputer->work();
- delete mycomputer;
- delete cpu;
- delete memory;
- delete harddisk;
- cout << "hello" << endl;
- system("pause");
- return;
- }
接口隔離原則不應(yīng)該強(qiáng)迫客戶的程序依賴他們不需要的接口方法,一個(gè)接口應(yīng)該是提供一種對(duì)外功能,不應(yīng)該把所有的操作都封裝到一個(gè)接口中去
里氏替換原則任何抽象類出現(xiàn)的地方都可以用它的實(shí)現(xiàn)類進(jìn)行替換,實(shí)際就是虛擬機(jī)智語言級(jí)別,實(shí)現(xiàn)面向?qū)ο蠊δ?/p>
優(yōu)先使用組合而不是繼承原則如果使用繼承,會(huì)導(dǎo)致復(fù)位的任何變化,都可能影響此類的行為,如果使用對(duì)象組合,就降低了這種依賴關(guān)系
迪米特法則一個(gè)對(duì)象應(yīng)當(dāng)對(duì)其他對(duì)象盡可能少的了解,從而降低各個(gè)對(duì)象之間的耦合,提高系統(tǒng)的可維護(hù)性。例如,在一個(gè)程序中,各個(gè)模塊之間相互調(diào)用時(shí),通常會(huì)提供一個(gè)統(tǒng)一的接口來實(shí)現(xiàn),這樣其他模塊不需要了解另外一個(gè)模塊的內(nèi)部實(shí)現(xiàn)細(xì)節(jié),這樣當(dāng)一個(gè)模塊內(nèi)部的實(shí)現(xiàn)發(fā)生改變的時(shí)候,不會(huì)影響其他模塊的使用黑盒原理。