一. 开闭原则
- 概念
对扩展开放,对修改关闭,增加功能是通过增加代码来实现,而不是去修改源代码 - 例子
设计一个计算器类
class Caculator{
public:
Caculator(int a, int b, string operator){
this->m_a = a;
this->m_b = b;
this->m_operator = operator;
}
int GetResult(){
if(m_operator.compare("+") == 0){
return m_a + m_b;
}
else if(m_operator.compare("-") == 0){
return m_a - m_b;
}
else if(m_operator.compare("*") == 0){
return m_a * m_b;
}
}
private:
int m_a;
int m_b;
string m_operator;
int m_res;
}
- 问题
如果增加取模的运算需要修改 getResult 成员方法,如果增加新功能的情况下要修改源代码,那么就会有修改出错的可能性。我们应该在增加新的功能时候,不能影响其他已经完成的功能。这就是对修改关闭,对扩展开放,叫做幵闭原则
//写一个抽象类
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;
}
public:
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;
}
public:
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;
}
public:
int mA;
int mB;
};
void test01() {
AbstractCaculator* calcultor = new PlusCalcultor;
calcultor->setOperatorNumber(10,20);
cout<<calcultor->getResult()<<endl;
delete calcultor;
calcultor = new MinuteCalcultor;
calcultor->setOperatorNumber(10, 20);
cout << calcultor->getResult() << endl;
delete calcultor;
calcultor = NULL;
}
int main() {
test01();
return 0;
}
二. 迪米特法则
一个对象应当对其他对象尽可能少的了解,从而降低各个对象之间的耦合,提高系统的可维护性。
例如在一个程序中,各个模块之间相互调用时,通常会提供一个统一的接口来实现。这样其他模块不需要了解另外一个模块的内部实现细节,这样当一个模块内部的实现发生改变时,不会影响其他模块的使用。
#include <iostream>
#include <string>
#include <vector>
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<<"被售卖!"<<endl;
}
virtual string getQuality() {
return mQuilty;
}
public:
string mQuilty;
};
//楼盘B
class BuildingB :public AbstractBuilding {
public:
BuildingB() { mQuilty = "低品质"; };
virtual void sale() {
cout << "楼盘B" << mQuilty << "被售卖!" << endl;
}
virtual string getQuality() {
return mQuilty;
}
public:
string mQuilty;
};
//客户端
void test01() {
BuildingA* ba = new BuildingA;
if (ba->mQuilty == "低品质") {
ba->sale();
}
BuildingB* bb = new BuildingB;
if (bb->mQuilty == "低品质") {
bb->sale();
}
}
//中介类
class Mediator {
public:
Mediator() {
AbstractBuilding* building = new BuildingA;
vBuilding.push_back(building);
building = new BuildingB;
vBuilding.push_back(building);
};
//对外提供接口
AbstractBuilding* findMyBuilding(string quality) {
//for (vector<AbstractBuilding*>::iterator it = vBuilding.begin(); it != vBuilding.end(); it++) {
// if ((*it)->getQuality() == quality){
// return *it;
// }
//}
for (auto it : vBuilding) {
if ((it)->getQuality() == quality) {
return it;
}
}
return NULL;
}
~Mediator() {
for (vector<AbstractBuilding*>::iterator it = vBuilding.begin(); it != vBuilding.end(); it++) {
if (*it != NULL) {
delete *it;
}
}
}
public:
vector<AbstractBuilding*> vBuilding;
};
void test02() {
Mediator* mediator = new Mediator;
AbstractBuilding* building = mediator->findMyBuilding("高品质");
if (building != NULL) {
building->sale();
}
else {
cout << "没有符合您条件的楼盘!" << endl;
}
}
int main() {
test02();
return 0;
}
三. 合成复用原则
如果使用继承,会导致父类的任何变换都可能影响到子类的行为。
如果使用对象组合,就降低了这种依赖关系。对于继承和组合,优先使用组合。
#include <bits/stdc++.h>
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;
};
void test02() {
Person* p = new Person;
p->setCar(new Dazhong);
p->Dongfeng();
p->setCar(new Tuolaji);
p->Dongfeng();
delete p;
}
//继承和组合优先使用组合
int main() {
test02();
return 0;
}
四. 依赖倒转原则
依赖于抽象(接口),不要依赖具体的实现(类),也就是针对接口编程。
#include <bits/stdc++.h>
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();
}
void test01() {
BankWorker* worker = new BankWorker;
doSaveBussiness(worker); //办理存款业务
doPayBussiness(worker); //办理支付业务
doTranferBussiness(worker); //办理转账业务
}
int main()
{
test01();
return 0;
}
#include <bits/stdc++.h>
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;
}
void test02() {
DoBankBussiness(new SaveBankWorker); //办理存款业务
DoBankBussiness(new TransferBankWorker); //办理转账业务
DoBankBussiness(new PayBankWorker); //办理付款业务
}
int main()
{
test02();
return 0;
}