目录
支持可维护性的同时,提高系统的可复用性,达到高内聚,低耦合目的。
原则 | 概念 | 速记 |
---|---|---|
单一职责原则 | 类的职责单一,对外只提供一种功能,而引起类变化的原因只有一个 | 功能单一 |
开闭原则 | 类的改动是通过增加代码进行,而不是修改源代码 | 不修改已有,只允许增加 |
里式替换原则 | 任何抽象类出现的地方,都可以用它的实现类进行替换。 实际是虚拟机制,语言级别实现面向对象功能 | 对外体现抽象类, 对内来继承实现 |
依赖倒置原则 | 依赖抽象接口,而不是依赖具体的实现或实现类,面向接口编程 | 用抽象接口,而不是具体实现类 |
接口隔离原则 | 一个接口只提供一种对外功能,不应该把所有操作封装到一个接口 | 给别人接口调用自由,而不是把路堵死 |
合成复用原则 | 继承是强耦合,父类的变换可能影响子类。 优先用 | 用类做成员,而不是直接继承 |
迪米特法则 | 对象与对象之间,尽可能减少了解,方便对外使用 | 对外暴露信息最少 |
- #include
- using namespace std;
-
- //写一个抽象类 ,方便扩展其他操作,而不是直接采用一个类,每次都要修改类的源码
- class AbstractCaculator {
- public:
- virtual int getResult() = 0;
- virtual void setOperatorNumber(int a, int b) = 0;
- };
-
- //加法计算器
- class PlusCalcultor :public AbstractCaculator {
- public:
- virtual void setOperatorNumber(int a,int b) {
- this->mA = a;
- this->mB = b;
- }
- virtual int getResult() {
- return mA + mB;
- }
- private:
- int mA;
- int mB;
- };
-
- //减法计算器类
- class MinuteCalcultor :public AbstractCaculator {
- public:
- virtual void setOperatorNumber(int a, int b) {
- this->mA = a;
- this->mB = b;
- }
- virtual int getResult() {
- return mA - mB;
- }
- private:
- int mA;
- int mB;
- };
-
- //乘法计算器类
- class MultiplyCalcultor :public AbstractCaculator {
- public:
- virtual void setOperatorNumber(int a, int b) {
- this->mA = a;
- this->mB = b;
- }
- virtual int getResult() {
- return mA * mB;
- }
- private:
- int mA;
- int mB;
- };
-
-
- int main(void)
- {
- AbstractCaculator* calcultor = new PlusCalcultor;
- calcultor->setOperatorNumber(10,20);
- cout<
getResult()< -
- delete calcultor;
-
- calcultor = new MinuteCalcultor;
- calcultor->setOperatorNumber(10, 20);
- cout << calcultor->getResult() << endl;
-
- delete calcultor;
- calcultor = NULL;
-
- return 0;
- }
2.3 迪米特法则
- #include
- #include
- #include
- using namespace std;
-
- //迪米特原则,又叫最少知道原则,
- class AbstractBuilding {
- public:
- virtual void sale() = 0;
- virtual string getQuality() = 0;
- };
-
- //楼盘A
- class BuildingA :public AbstractBuilding {
- public:
- BuildingA() { mQuilty = "高品质"; };
- virtual void sale() {
- cout << "楼盘A" << mQuilty<<"被售卖!"<
- }
- virtual string getQuality() {
- return mQuilty;
- }
- private:
- string mQuilty;
- };
-
- //楼盘B
- class BuildingB :public AbstractBuilding {
- public:
- BuildingB() { mQuilty = "低品质"; };
- virtual void sale() {
- cout << "楼盘B" << mQuilty << "被售卖!" << endl;
- }
- virtual string getQuality() {
- return mQuilty;
- }
- private:
- string mQuilty;
- };
-
-
- //引入中介类,可以方便直接找到对应品质,而无需暴露所有楼盘
- // 因此需要在中介类中汇总所有信息,方便查找
- class Mediator {
- public:
- Mediator() {
- AbstractBuilding* building = new BuildingA;
- vBuilding.push_back(building);
- building = new BuildingB;
- vBuilding.push_back(building);
- };
-
- //对外提供接口
- AbstractBuilding* findMyBuilding(string quality) {
- for (auto it : vBuilding) {
- if ((it)->getQuality() == quality) {
- return it;
- }
- }
- return NULL;
- }
-
- ~Mediator() {
- for (vector
::iterator it = vBuilding.begin(); it != vBuilding.end(); it++) { - if (*it != NULL) {
- delete *it;
- }
- }
- }
- public:
- vector
vBuilding; - };
-
-
- int main() {
- Mediator* mediator = new Mediator;
-
- AbstractBuilding* building = mediator->findMyBuilding("高品质");
-
- if (building != NULL) {
- building->sale();
- }
- else {
- cout << "没有符合您条件的楼盘!" << endl;
- }
- return 0;
- }
2.4 合成复用原则
- #include
- using namespace std;
-
- //抽象车
- class AbstractCar {
- public:
- virtual void run() = 0;
-
- };
-
- //大众车
- class Dazhong :public AbstractCar {
- public:
- virtual void run() {
- cout << "大众车启动" << endl;
- }
- };
-
- //拖拉机
- class Tuolaji :public AbstractCar {
- public:
- virtual void run() {
- cout << "拖拉机启动" << endl;
- }
- };
-
- //针对具体类 不适用继承,作为成员,而不是直接继承
- #if 0
- class Person :public Tuolaji {
- public:
- void Dongfeng() {
- run();
- }
- };
- class Person2 :public Dazhong {
- public:
- void Dazhong() {
- run();
- }
- };
- #endif
-
- //可以使用合成复用原则
- class Person {
- public:
- void setCar(AbstractCar* car) {
- this->car = car;
- }
- void Dongfeng() {
- this->car->run();
- if (this->car != NULL) {
- delete this->car;
- this->car = NULL;
- }
- }
-
- public:
- AbstractCar* car;
- };
-
-
- //继承和组合优先使用组合
- int main() {
- Person* p = new Person;
- p->setCar(new Dazhong);
- p->Dongfeng();
-
- p->setCar(new Tuolaji);
- p->Dongfeng();
-
- delete p;
- return 0;
- }
2.5 依赖倒置原则
依赖不合理方式
- #include
- using namespace std;;
-
-
- //银行工作人员
- class BankWorker {
- public:
- void saveService() {
- cout << "办理存款业务..." << endl;
- }
- void payService() {
- cout << "办理支付业务..." << endl;
- }
- void tranferService() {
- cout << "办理转账业务..." << endl;
- }
-
- };
-
- //中层模块
- void doSaveBussiness(BankWorker* worker) {
- worker->saveService();
- }
-
- void doPayBussiness(BankWorker* worker) {
- worker->payService();
- }
-
- void doTranferBussiness(BankWorker* worker) {
- worker->tranferService();
- }
-
-
- int main()
- {
- BankWorker* worker = new BankWorker;
- doSaveBussiness(worker); //办理存款业务
- doPayBussiness(worker); //办理支付业务
- doTranferBussiness(worker); //办理转账业务
- return 0;
- }
依赖合理方式
- #include
- using namespace std;;
-
- //依赖倒转原则
- //抽象层
- class AbstractWorkerBank {
- public:
- virtual void doBussiness() = 0;
- };
-
- //只办理存款业务
- class SaveBankWorker : public AbstractWorkerBank {
- public:
- virtual void doBussiness() {
- cout << "办理存款业务" << endl;
- }
- };
-
- //只办理转账业务
- class TransferBankWorker : public AbstractWorkerBank {
- public:
- virtual void doBussiness() {
- cout << "办理存款业务" << endl;
- }
- };
-
- //只办理付款业务
- class PayBankWorker : public AbstractWorkerBank {
- public:
- virtual void doBussiness() {
- cout << "办理存款业务" << endl;
- }
- };
-
- //高层模块
- void DoBankBussiness(AbstractWorkerBank* worker) {
- worker->doBussiness();
- delete worker;
- }
-
-
- int main()
- {
- DoBankBussiness(new SaveBankWorker); //办理存款业务
- DoBankBussiness(new TransferBankWorker); //办理转账业务
- DoBankBussiness(new PayBankWorker); //办理付款业务
- return 0;
- }
-
好在哪里?做了一层真正的抽象,完全只依赖接口,避免了依赖具体实现,从而能够保证隔层,保证上下层真正不会造成耦合影响。
-
相关阅读:
Dockerfile自定义容器
08 robotframework 修改乱码问题
【数据库——MySQL(实战项目1)】(1)图书借阅系统
Kinsoku jikou desu新浪股票接口变动(php)
在vscode如何利用快捷键选择一样的单词
SpringBoot如何上传文件
Fiddler的安装及配置2-1
2.6 二叉树
【java中的反射】1.初识反射
学会背包问题
-
原文地址:https://blog.csdn.net/AgingMoon/article/details/128170924