关于c++设计模式,要考虑内存的回收;
设计原则目的:高内聚,低耦合;
设计原则:
1.
单一职责原则;
2.
开闭原则;
3.
里式转换原则;
4.
依赖倒转原则;
5.
接口隔离原则;
6.
合成复用原则;
7.
迪米特原则;
一级目录
开闭原则, 对扩展开放,对修改关闭,增加功能是增加代码来实现,而不是去修改源代码
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class AbstructCaculator
{
public:
virtual int getResult() = 0;
virtual void setOperatorNumber(int a,int b) = 0;
};
class PlusCaculator :public AbstructCaculator {
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 MinuteCaculator :public AbstructCaculator {
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 MultiplyCaculator :public AbstructCaculator {
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()
{
AbstructCaculator *caculator = new PlusCaculator;
caculator->setOperatorNumber(10, 20);
cout << "ret:" << caculator->getResult() << endl;
}
int main()
{
test01();
return 0;
}
二级目录
//迪米特原则,又叫最少知识原则;
class AbstactBuilding {
public:
virtual void sale() = 0;
virtual string getQulity() = 0;
};
class BuinldingA :public AbstactBuilding
{
public:
BuinldingA() {
mQulity = "高品质";
}
virtual void sale()
{
cout << "楼盘A"<<mQulity<<"被售卖" << endl;
}
virtual string getQulity()
{
return mQulity;
}
public:
string mQulity;
};
class BuinldingB :public AbstactBuilding
{
public:
BuinldingB() {
mQulity = "低品质";
}
virtual void sale()
{
cout << "楼盘B" << mQulity << "被售卖" << endl;
}
virtual string getQulity()
{
return mQulity;
}
public:
string mQulity;
};
void test01()
{
BuinldingA *ba = new BuinldingA;
if (ba->mQulity == "高品质")
{
ba->sale();
}
BuinldingB *bb = new BuinldingB;
if (bb->mQulity == "低品质")
{
bb->sale();
}
}
//中介类
class Mediator {
public:
Mediator()
{
AbstactBuilding * building = new BuinldingA;
va.push_back(building);
building = new BuinldingB;
va.push_back(building);
}
~Mediator()
{
for (vector<AbstactBuilding*>::iterator it = va.begin(); it != va.end(); ++it)
{
if (*it != nullptr)
{
delete *it;
}
}
}
//对外提供接口
AbstactBuilding *findBuilding(string quality)
{
for (vector<AbstactBuilding*>::iterator it = va.begin(); it != va.end(); ++it)
{
if ((*it)->getQulity()==quality)
{
return *it;
}
}
return NULL;
}
public:
vector<AbstactBuilding*> va;
};
//客户端
void test02()
{
Mediator * mediator = new Mediator;
AbstactBuilding * building = mediator->findBuilding("高品质");
if (building != nullptr)
{
building->sale();
}
else
{
cout << "没有符合条件的楼盘" << endl;
}
}
int main()
{
test01();
test02();
return 0;
}
三级目录
//合成复用原则
class AbstractCar
{
public:
virtual void run() = 0;
};
class A :public AbstractCar
{
public:
virtual void run()
{
cout << "A 启动" << endl;
}
};
class B :public AbstractCar
{
public:
virtual void run()
{
cout << "B 启动" << endl;
}
};
//在这里不使用继承,使用组合
class Person
{
public:
void setCar(AbstractCar *car)
{
this->car = car;
}
void Doufeng()
{
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 A);
p->Doufeng();
p->setCar(new B);
p->Doufeng();
delete p;
p = nullptr;
}
int main()
{
test02();
return 0;
}
四级目录
//依赖倒转原则
class AbstractWorker
{
public:
virtual void doBussiness() = 0;
};
//专门负责存款业务的人员
class SaveWorker :public AbstractWorker
{
public:
virtual void doBussiness()
{
cout << "办理存款业务。。。" << endl;
}
};
//专门办理支付业务的人员
class PayBankWorker:public AbstractWorker
{
virtual void doBussiness()
{
cout << "办理支付业务。。。" << endl;
}
};
//专门负责办理转账业务的人员
class TransferABankWorker:public AbstractWorker
{
virtual void doBussiness()
{
cout << "办理转账业务。。。" << endl;
}
};
//中层业务
void doNewBussiness(AbstractWorker * worker)
{
worker->doBussiness();
delete worker;
}
void test02()
{
doNewBussiness(new TransferABankWorker);
doNewBussiness(new SaveWorker);
doNewBussiness(new PayBankWorker);
}
int main()
{
test02();
return 0;
}
//一般实现
class BankWorker
{
public:
void saveService()
{
cout << "办理存款业务 。。。" << endl;
}
void payService()
{
cout << "办理支付业务 。。。" << endl;
}
void transferService()
{
cout << "办理转账业务 。。。" << endl;
}
};
//中层模块
void doSaveBussiness(BankWorker *worker)
{
worker->saveService();
}
void doPayBussiness(BankWorker *worker)
{
worker->payService();
}
void doTransferBussiness(BankWorker *worker)
{
worker->transferService();
}
void test01()
{
BankWorker *worker = new BankWorker;
doSaveBussiness(worker);//办理存款业务
doPayBussiness(worker); //办理支付业务
doTransferBussiness(worker);//办理转账业务
}
int main()
{
return 0;
}