class MakeCar { public: virtual void MakeHead() = 0; virtual void MakeBody() = 0; virtual void MakeTail() = 0; public: void Make() //模板函数,把业务逻辑给做好 { MakeTail(); MakeBody(); MakeHead(); } }; class Jeep : public MakeCar { public: virtual void MakeHead() { cout << "jeep head" << endl; } virtual void MakeBody() { cout << "jeep body" << endl; } virtual void MakeTail() { cout << "jeep tail" << endl; } }; class Bus : public MakeCar { public: virtual void MakeHead() { cout << "Bus head" << endl; } virtual void MakeBody() { cout << "Bus body" << endl; } virtual void MakeTail() { cout << "Bus tail" << endl; } }; void main() { MakeCar *mycar = new Bus; mycar->Make(); delete mycar; cout << endl; MakeCar *mycar2 = new Jeep; mycar2->Make(); delete mycar2; }
// 发起者、接收者、命令 #include <iostream> using namespace std; #include "list" //医生 class Doctor { public: void treat_eye() { cout << "医生 治疗 眼科病" << endl; } void treat_nose() { cout << "医生 治疗 鼻科病" << endl; } }; //命令 class Command //基类 { public: virtual void treat() = 0; }; class CommandTreatEye : public Command //具体命令 { public: CommandTreatEye(Doctor *doctor) { m_doctor = doctor; } void treat() { m_doctor->treat_eye(); } private: Doctor *m_doctor; }; class CommandTreatNose : public Command //具体命令 { public: CommandTreatNose(Doctor *doctor) { m_doctor = doctor; } void treat() { m_doctor->treat_nose(); } private: Doctor *m_doctor; }; //护士——单个下命令 class BeautyNurse //命令的发起者 { public: BeautyNurse(Command *command) { this->command = command; } public: void SubmittedCase() //提交病例,下命令 { command->treat(); } protected: private: Command *command; //持有命令 }; //护士长——批量下命令 class HeadNurse { public: HeadNurse() { m_list.clear(); } public: void setCommand(Command *command) { m_list.push_back(command); } void SubmittedCase() //提交病例 下命令 { for (list<Command *>::iterator it = m_list.begin(); it != m_list.end(); it++) { (*it)->treat(); } } private: list<Command *> m_list; //持有批量命令 }; void main01() { //1 医生直接看病 /* Doctor *doctor = new Doctor ; doctor->treat_eye(); delete doctor; */ //2 通过一个命令 医生通过 命令 治疗 治病 Doctor *doctor = new Doctor; Command * command = new CommandTreatEye(doctor); command->treat(); delete command; delete doctor; } void main02() { //3 护士提交简历 给医生看病 BeautyNurse *beautynurse = NULL; Doctor *doctor = NULL; Command *command = NULL; doctor = new Doctor; command = new CommandTreatEye(doctor); //command = new CommandTreatNose(doctor); //客户端可以面向命令进行编程了,实现了命令和客户端的解耦合 beautynurse = new BeautyNurse(command); beautynurse->SubmittedCase(); delete doctor; delete command; delete beautynurse; } //4 通过护士长 批量的下命令 void main03() { //护士长提交简历 给医生看病 HeadNurse *headnurse = NULL; Doctor *doctor = NULL; Command *command1 = NULL; Command *command2 = NULL; doctor = new Doctor; command1 = new CommandTreatEye(doctor); command2 = new CommandTreatNose(doctor); headnurse = new HeadNurse(); //new 护士长 headnurse->setCommand(command1);//收集命令 headnurse->setCommand(command2); headnurse->SubmittedCase(); //护士长 批量下命令 delete doctor; delete command1; delete command2; delete headnurse; } void main() { //main01(); //main02(); main03(); }
#include <iostream> using namespace std; /* struct Teacher { char name; int age; Teacher *next; //链!下一个处理单元 }; */ //“造完”车以后,需要把任务 传递下去 class CarHandle { public: virtual void HandleCar() = 0; CarHandle *setNextHandle(CarHandle *handle) { m_handle = handle; return m_handle; } protected: CarHandle *m_handle; //指向自己,类似struct中的Teacher *next,下一个处理单元 }; class HeadCarHandle : public CarHandle { public: virtual void HandleCar() { cout << "造 车头" << endl; //造完 车头 以后,把任务递交给 下一个处理者 if (m_handle != NULL) { m_handle->HandleCar(); } } }; class BodyCarHandle : public CarHandle { public: virtual void HandleCar() { cout << "造 车身" << endl; //造完 车身 以后,把任务递交给 下一个处理者 if (m_handle != NULL) { m_handle->HandleCar(); } } }; class TailCarHandle : public CarHandle { public: virtual void HandleCar() { cout << "造 车尾" << endl; //造完 车尾 以后,把任务递交给 下一个处理者 if (m_handle != NULL) { m_handle->HandleCar(); } } }; void main() { //责任链 不是 “伪面向过程编程”! CarHandle *headHandle = new HeadCarHandle; CarHandle *bodyHandle = new BodyCarHandle; CarHandle *tailHandle = new TailCarHandle; //任务的处理关系 /* headHandle->setNextHandle(bodyHandle); bodyHandle->setNextHandle(tailHandle); tailHandle->setNextHandle(NULL); */ //业务逻辑可以灵活改变,不会被写死到客户端 // headHandle--->bodyHandle--->tailHandle // headHandle--->tailHandle--->bodyHandle headHandle->setNextHandle(tailHandle); //生成责任链 tailHandle->setNextHandle(bodyHandle); bodyHandle->setNextHandle(NULL); headHandle->HandleCar(); //处理 delete headHandle; delete bodyHandle; delete tailHandle; }
#include <iostream> using namespace std; class Strategy //策略抽象 { public: virtual void crypt() = 0; }; //对称加密:速度快 加密大数据块文件;特点:加密密钥和解密密钥是一样的. class AES : public Strategy { public: virtual void crypt() { cout << "AES 加密算法" << endl; } }; //非对称加密:加密速度慢 加密强度高 安全性高;特点: 加密密钥和解密密钥不一样,密钥对(公钥 和 私钥) class DES : public Strategy { public: virtual void crypt() { cout << "DES 加密算法" << endl; } }; class Context //将算法的逻辑抽象接口封装到该类(Context)中 { public: void setStrategy(Strategy *strategy) { this->strategy = strategy; } void myoperator() { strategy->crypt(); } private: Strategy *strategy; }; void main() { /* //1 DES *des = new DES; des->crypt(); delete des; */ Strategy *strategy = NULL; //strategy = new DES; strategy = new AES; Context *context = new Context; context->setStrategy(strategy); context->myoperator(); delete strategy; delete context; }
class Mediator; class Person { public: Person(string name, int sex, int condi, Mediator *m) { m_name = name; m_sex = sex; m_condi = condi; mediator = m; } string getName() { return m_name; } int getSex() { return m_sex; } int getCondi() { return m_condi; } virtual void getParter(Person *p) = 0; protected: string m_name; int m_sex; int m_condi; Mediator *mediator; }; class Mediator //中介者 { public: void setMan(Person *man) { pMan = man; } void setWoman(Person *woman) { pWoman = woman; } public: virtual void getParter() { if (pWoman->getSex() == pMan->getSex()) { cout << "同性恋 之间 不能找对象!" << endl; } if (pWoman->getCondi() == pMan->getCondi()) { cout << pWoman->getName() << " 和 " << pMan->getName() << "绝配 " << endl; } else { cout << pWoman->getName() << " 和 " << pMan->getName() << "不配 " << endl; } } private: Person *pMan; //男 //list<Person *> m_list; Person *pWoman; //女 }; class Man : public Person //男 { public: Man(string name, int sex, int condi, Mediator *m) : Person(name, sex, condi, m) { } virtual void getParter(Person *p) { mediator->setMan(this); mediator->setWoman(p); mediator->getParter(); //找对象 } }; class Woman : public Person //女 { public: Woman(string name, int sex, int condi, Mediator *m) : Person(name, sex, condi, m) { } virtual void getParter(Person *p) { mediator->setMan(p); mediator->setWoman(this); mediator->getParter(); //找对象 } }; void main() { //string name, int sex, int condi Mediator *m = new Mediator; Person *xiaofang = new Woman("小芳", 2, 5, m); Person *zhangsan = new Man("张三", 1, 4, m); Person *lisi = new Man("李四", 1, 5, m); xiaofang->getParter(zhangsan); xiaofang->getParter(lisi); }
class Secretary; //观察者(玩游戏的同事) class PlayserObserver { public: PlayserObserver(Secretary *secretary) { this->m_secretary = secretary; } void update(string action) { cout << "action:" << action << endl; cout << "收到!" << endl; cout << endl; } private: Secretary *m_secretary; }; //秘书(通知者) class Secretary { public: Secretary() { m_list.clear(); } void setPlayserObserver(PlayserObserver *o) { m_list.push_back(o); } void Notify(string info) { //给所有的 观察者 发送 情报 for (list<PlayserObserver *>::iterator it = m_list.begin(); it != m_list.end(); it++) { (*it)->update(info); } } private: list<PlayserObserver *> m_list; }; void main07() { //玩游戏的多个同事同时监听秘书发来的消息,以便做出及时的状态更新! Secretary *secretary = NULL; PlayserObserver *po1 = NULL; PlayserObserver *po2 = NULL; secretary = new Secretary; po1 = new PlayserObserver(secretary); po2 = new PlayserObserver(secretary); secretary->setPlayserObserver(po1); secretary->setPlayserObserver(po2); secretary->Notify("老板来了!");//在list中的多个监听对象,都会收到该通知! secretary->Notify("老板走了!");//在list中的多个监听对象,都会收到该通知! delete secretary; delete po1; delete po2; }
//Caretaker 管理者 // MememTo 备忘录 class MememTo { public: MememTo(string name, int age) { m_name = name; m_age = age; } string getName() { return m_name; } int getAge() { return m_age; } void setName(string name) { m_name = name; } void setAge(int age) { m_age = age; } private: string m_name; int m_age; }; class Person { public: Person(string name, int age) { m_name = name; m_age = age; } string getName() { return m_name; } int getAge() { return m_age; } void setName(string name) { m_name = name; } void setAge(int age) { m_age = age; } //保存 MememTo* createMemto() { return new MememTo(m_name, m_age); } //还原 void setMemTo(MememTo *mememto) { m_age = mememto->getAge(); m_name = mememto->getName(); } void print() { cout << "m_name:" << m_name << " m_age:" << m_age << endl; } private: string m_name; int m_age; }; //管理者 class Caretaker { public: Caretaker(MememTo *memto) { this->memto = memto; } MememTo* getMemTo() { return this->memto; } void setMemTo(MememTo * memto) { this->memto = memto; } private: MememTo *memto; }; void main001() { Person *p = new Person("zhangsan", 33); p->print(); cout << endl; //创建Person对象的一个状态 MememTo *m = NULL; m = p->createMemto(); p->setAge(42); p->print(); cout << "还原旧的状态:" << endl; p->setMemTo(m); p->print(); delete m; delete p; } //加入管理者 Caretaker类 void main0002() { Caretaker *caretaker = NULL; Person *p = new Person("zhangsan", 33); p->print(); cout << endl; //创建Person对象的一个状态 //MememTo *m = NULL; caretaker = new Caretaker(p->createMemto()); //m = p->createMemto(); p->setAge(42); p->print(); cout << "还原旧的状态:" << endl; p->setMemTo(caretaker->getMemTo()); p->print(); delete caretaker; delete p; } void main() { //main001(); main0002(); }
State模式,状态模式,是行为设计模式的一种。State模式允许通过改变对象的内部状态而改变对象的行为,这个对象表现得就好像修改了它的类一样。 状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转译到表现不同状态的一系列类当中,可以把复杂的判断逻辑简化。适用于:对象的行为,依赖于它所处的当前状态。行为随状态改变而改变的场景。
#include <iostream> using namespace std; class Worker; class State { public: virtual void doSomeThing(Worker *w) = 0; }; class Worker { public: Worker(); int getHour() { return m_hour; } void setHour(int hour) //改变状态 7 { m_hour = hour; } State* getCurrentState() { return m_currstate; } void setCurrentState(State* state) { m_currstate = state; } void doSomeThing() { m_currstate->doSomeThing(this); } private: int m_hour; State *m_currstate; //对象的当前状态 }; class State1 : public State { public: void doSomeThing(Worker *w); }; class State2 : public State { public: void doSomeThing(Worker *w); }; void State1::doSomeThing(Worker *w) { if (w->getHour() == 7 || w->getHour() == 8) { cout << "吃早饭" << endl; } else { //状态1 不满足,要转到状态2 delete w->getCurrentState(); //干掉状态1 w->setCurrentState(new State2); //换成状态2 w->getCurrentState()->doSomeThing(w); } } void State2::doSomeThing(Worker *w) { if (w->getHour() == 9 || w->getHour() == 10) { cout << "工作" << endl; } else { delete w->getCurrentState(); //状态2 不满足 要转到状态3 后者恢复到初始化状态 w->setCurrentState(new State1); //恢复到当初状态 cout << "当前时间点:" << w->getHour() << "未知状态" << endl; } } Worker::Worker() { m_currstate = new State1; } void main() { Worker *w1 = new Worker; w1->setHour(7); w1->doSomeThing(); w1->setHour(9); w1->doSomeThing(); delete w1; cout << "hello..." << endl; system("pause"); return; }
#include <iostream> using namespace std; // Context // Expression // PlusExpression MinusExpression class Context { public: Context(int num) { this->m_num = num; } int getNum() { return m_num; } int getRes() { return m_res; } void setNum(int num) { this->m_num = num; } void setRes(int res) { this->m_res = res; } private: int m_num; //输入 int m_res; //输出 }; class Expression //解释器 { public: virtual void interpreter(Context *context) = 0; private: Context *m_context; //关联Context }; //加法 class PlusExpression : public Expression { public: PlusExpression() { this->context = NULL; } virtual void interpreter(Context *context) { int num = context->getNum(); num++; context->setNum(num); context->setRes(num); } private: Context *context; }; // 减法 class MinusExpression : public Expression { public: MinusExpression() { this->context = NULL; } virtual void interpreter(Context *context) { int num = context->getNum(); num--; context->setNum(num); context->setRes(num); } private: Context *context; }; void main() { Expression *expression = NULL; Context *context = NULL; Expression *expression2 = NULL; context = new Context(10); cout << context->getNum() << endl; expression = new PlusExpression; expression->interpreter(context); cout << context->getRes() << endl; expression2 = new MinusExpression; expression2->interpreter(context); cout << context->getRes() << endl; }
#include <iostream> using namespace std; // MyIterator、Aggregate、ContreteIterator、ConcreteAggregate // a b c d // ▲ typedef int Object; //用int型模拟变量 #define SIZE 5 //迭代器抽象层 class MyIterator { public: virtual void First() = 0; //让迭代器位置回到首位 virtual void Next() = 0; virtual bool IsDone() = 0; //检查迭代器是否遍历完毕 virtual Object CurrentItem() = 0; //获取一个对象,用上述的object来模拟对象 }; //集合抽象层 class Aggregate { public: virtual MyIterator *CreateIterator() = 0; //创建迭代器 virtual Object getItem(int index) = 0; //获取当前元素 virtual int getSize() = 0; //获取集合大小 }; //具体的迭代器 class ContreteIterator : public MyIterator { public: ContreteIterator(Aggregate *ag) { _ag = ag; _current_index = 0; } virtual void First() { _current_index = 0; //让当前 游标 回到位置0 } virtual void Next() { if (_current_index < _ag->getSize()) { _current_index++; } } virtual bool IsDone() { return (_current_index == _ag->getSize()); } virtual Object CurrentItem() { return _ag->getItem(_current_index); } private: int _current_index; Aggregate *_ag; }; //具体的集合 class ConcreteAggregate : public Aggregate { public: ConcreteAggregate() { for (int i = 0; i<SIZE; i++) { object[i] = i + 100; } } virtual MyIterator *CreateIterator() { return new ContreteIterator(this); //让迭代器 持有一个 集合的 引用 } virtual Object getItem(int index) { return object[index]; } virtual int getSize() { return SIZE; } private: Object object[SIZE]; }; void main() { Aggregate *ag = new ConcreteAggregate; MyIterator *it = ag->CreateIterator(); for (; !(it->IsDone()); it->Next()) { cout << it->CurrentItem() << " "; } delete it; delete ag; }