淺談C++ 設(shè)計(jì)模式的基本原則

先上銀行類(lèi)案例代碼如下:
#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é)原則類(lèi)的職責(zé)要單一,對(duì)外只提供一種功能,而引起內(nèi)變化的原因都應(yīng)該只有一個(gè),就是依賴(lài)倒置原則依賴(lài)于抽象接口,不要依賴(lài)具體的實(shí)現(xiàn)類(lèi),也就是針對(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 << "我是因特爾廠(chǎng)家" << endl;
}
};
class XSDisk :public HardDisk
{
public:
void work()
{
cout << "我是西數(shù)硬盤(pán)廠(chǎng)家" << endl;
}
};
class JSDMem :public Memory
{
public:
void work()
{
cout << "我是JSDMem廠(chǎng)家" << 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)迫客戶(hù)的程序依賴(lài)他們不需要的接口方法,一個(gè)接口應(yīng)該是提供一種對(duì)外功能,不應(yīng)該把所有的操作都封裝到一個(gè)接口中去
里氏替換原則任何抽象類(lèi)出現(xiàn)的地方都可以用它的實(shí)現(xiàn)類(lèi)進(jìn)行替換,實(shí)際就是虛擬機(jī)智語(yǔ)言級(jí)別,實(shí)現(xiàn)面向?qū)ο蠊δ?/p>
優(yōu)先使用組合而不是繼承原則如果使用繼承,會(huì)導(dǎo)致復(fù)位的任何變化,都可能影響此類(lèi)的行為,如果使用對(duì)象組合,就降低了這種依賴(lài)關(guān)系
迪米特法則一個(gè)對(duì)象應(yīng)當(dāng)對(duì)其他對(duì)象盡可能少的了解,從而降低各個(gè)對(duì)象之間的耦合,提高系統(tǒng)的可維護(hù)性。例如,在一個(gè)程序中,各個(gè)模塊之間相互調(diào)用時(shí),通常會(huì)提供一個(gè)統(tǒng)一的接口來(lái)實(shí)現(xiàn),這樣其他模塊不需要了解另外一個(gè)模塊的內(nèi)部實(shí)現(xiàn)細(xì)節(jié),這樣當(dāng)一個(gè)模塊內(nèi)部的實(shí)現(xiàn)發(fā)生改變的時(shí)候,不會(huì)影響其他模塊的使用黑盒原理。
到此這篇關(guān)于淺談C++ 設(shè)計(jì)模式的基本原則的文章就介紹到這了,更多相關(guān)C++ 設(shè)計(jì)模式的基本原則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--定點(diǎn)數(shù)模擬浮點(diǎn)數(shù)運(yùn)算及常見(jiàn)的策略
本文主要講解DSP中定點(diǎn)數(shù)模擬浮點(diǎn)數(shù)運(yùn)算及常見(jiàn)的策略,具有參考價(jià)值,需要的朋友可以參考一下。2016-06-06
Opencv 視頻轉(zhuǎn)為圖像序列的實(shí)現(xiàn)
今天小編就為大家分享一篇Opencv 視頻轉(zhuǎn)為圖像序列的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
C語(yǔ)言?模擬實(shí)現(xiàn)memcpy與memmove函數(shù)詳解
這篇文章主要介紹了C語(yǔ)言詳解如何模擬內(nèi)存函數(shù),用到了mencpy與memmove兩個(gè)函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-04-04

