前言
行为型模式是对在不同的对象之间划分责任和算法的抽象化。行为型模式不仅仅关注类和对象的结构,而且重点关注它们之间的相互作用。
Interpreter(解释器)
Template Method(模板方法)
GOOD:把不变的代码部分都转移到父类中,将可变的代码用 virtual 留到子类重写。
template.h
#ifndef CLION_TEST_TEMPLATE_H
#define CLION_TEST_TEMPLATE_H#include <iostream>using namespace std;// 金庸小说考题试卷
class TestPaper {
public:void TestQuestion1() {cout << "杨过得到,后来给了郭静,炼成倚天剑、屠龙刀的玄铁可能是[] a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维"<< endl;cout << "答案:" << Answer1() << endl;}void TestQuestion2() {cout << "杨过、程英、陆无双铲除了情花,造成[] a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化"<< endl;cout << "答案:" << Answer2() << endl;}void TestQuestion3() {cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全部对"<< endl;cout << "答案:" << Answer3() << endl;}protected:virtual char Answer1() = 0;virtual char Answer2() = 0;virtual char Answer3() = 0;
};class TestPaperA : public TestPaper {char Answer1() final {return 'b';}char Answer2() final {return 'c';}char Answer3() final {return 'a';}
};class TestPaperB : public TestPaper {char Answer1() final {return 'c';}char Answer2() final {return 'a';}char Answer3() final {return 'a';}
};#endif //CLION_TEST_TEMPLATE_H
main.cpp
#include "template.h"using namespace std;int main() {system("chcp 65001");// 模板方法cout<<"学生甲抄的试卷:"<<endl;TestPaper* studentA = new TestPaperA();studentA->TestQuestion1();studentA->TestQuestion2();studentA->TestQuestion3();cout<<"学生乙抄的试卷:"<<endl;TestPaper* studentB = new TestPaperB();studentB->TestQuestion1();studentB->TestQuestion2();studentB->TestQuestion3();return 0;
}
Chain of Responsibility(责任链)
Command(命令)
Iterator(迭代器)
Mediator(中介者)
Memento(备忘录)
Observer(观察者)
GOOD:定义了一种一对多的关系,让多个观察对象(公司员工)同时监听一个主题对象(秘书),主题对象状态发生变化时,会通知所有的观察者,使它们能够更新自己。
observer.h
#ifndef CLION_TEST_OBSERVER_H
#define CLION_TEST_OBSERVER_H#include <string>
#include <iostream>
#include <vector>using namespace std;class SecretaryBase;// 抽象观察者
class CObserverBase {
protected:string name;SecretaryBase *sub;
public:CObserverBase(string strname, SecretaryBase *strsub) {name = strname;sub = strsub;}virtual void Update() = 0;
};// 具体的观察者,看股票的
class StockObserver : public CObserverBase {
public:StockObserver(string strname, SecretaryBase *strsub) : CObserverBase(strname, strsub) {}virtual void Update();
};// 具体的观察者,看NBA的
class NBAObserver : public CObserverBase {
public:NBAObserver(string strname, SecretaryBase *strsub) : CObserverBase(strname, strsub) {}virtual void Update();
};// 抽象通知者
class SecretaryBase {
public:string action;vector<CObserverBase *> observers;
public:virtual void Attach(CObserverBase *observer) = 0;virtual void Notify() = 0;
};// 具体通知者
class Secretary : public SecretaryBase {
public:void Attach(CObserverBase *ob) {observers.emplace_back(ob);}void Notify() {for (CObserverBase *observer: observers) {observer->Update();}}
};void StockObserver::Update() {cout << name << ":" << sub->action << ",不要玩股票了,要开始工作了" << endl;
}void NBAObserver::Update() {cout << name << ":" << sub->action << ",不要看NBA了,老板来了" << endl;
}#endif //CLION_TEST_OBSERVER_H
main.cpp
#include "observer.h"using namespace std;int main() {system("chcp 65001");// 观察者模式SecretaryBase *p = new Secretary(); // 创建观察者// 被观察者的对象CObserverBase *s1 = new NBAObserver("小李", p);CObserverBase *s2 = new StockObserver("小赵", p);// 加入观察队列p->Attach(s1);p->Attach(s2);// 事件p->action = "老板来了";// 通知p->Notify();return 0;
}
State(状态)
Strategy(策略)
定义算法家族,分别封装起来,让它们之间可以互相替换,让算法变化,不会影响到用户
GOOD:适合类中的成员以方法为主,算法经常变动;简化了单元测试(因为每个算法都有自己的类,可以通过自己的接口单独测试。
策略模式和简单工厂基本相同,但简单工厂模式只能解决对象创建问题,对于经常变动的算法(方法)应使用策略模式。
BUG:客户端要做出判断。
strategy.h
#ifndef CLION_TEST_STRATEGY_H
#define CLION_TEST_STRATEGY_H// 策略基类
class COperation {
public:int m_nFirst;int m_nSecond;virtual double GetResult() {double dResult = 0;return dResult;}
};// 策略具体类——加法类
class AddOperation : public COperation {
public:AddOperation() {}AddOperation(int a, int b) {m_nFirst = a;m_nSecond = b;}double GetResult() final {return m_nFirst + m_nSecond;}
};class Context {
private:COperation *op;
public:Context(COperation *temp) {op = temp;}double GetResult() {return op->GetResult();}
};#endif //CLION_TEST_STRATEGY_H
main.h
#include <iostream>
#include "strategy.h"using namespace std;int main() {system("chcp 65001");// 简单工厂模式int a = 1;int b = 2;// 策略模式char c = '+';switch (c) {case '+':Context* context = new Context(new AddOperation(a,b));cout<<context->GetResult()<<endl;break;default:break;}return 0;
}
策略模式与工厂结合
将实例化具体的类过程移至到Context对象的引用中。
strategy.h
// 策略与工厂结合Context(char cType) {switch(cType) {case '+': op = new AddOperation(3,8);break;default:op = new AddOperation();break;}}
main.h
int main()
{int a,b;cin>>a>>b;Context *test=new Context('+');cout<<test>GetResult()<<endl;return 0;
}