转载地址:https://www.jianshu.com/p/3644ec4abdb3
策略模式
![](http://upload-images.jianshu.io/upload_images/2477704-1830423841ed9cbf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/530)
策略模式
#include<iostream>
#include<string>
using namespace std;
class Strategy
{
public:
virtual string operate()=0;
};
class StrategyA:public Strategy
{
public:
virtual string operate()
{
string str="打折";
return str;
}
};
class StrategyB:public Strategy
{
public:
virtual string operate()
{
string str="不打折";
return str;
}
} ;
class StrategyFactory
{
private:
Strategy* strategy;
public:
StrategyFactory(Strategy* s){
strategy=s;
}
string strategyInterface(){
string str=strategy->operate();
return str;
}
};
int main()
{
StrategyFactory* s=new StrategyFactory(new StrategyA);
string str=s->strategyInterface();
cout<<str<<endl;
}
看完程序是不是觉得和简单工厂模式很像,这时候对比一下UML图可以看到策略模式的工厂和策略之间的关系是聚合,这个例子如果改成简单工厂模式的UML图应该是这样:
![](http://upload-images.jianshu.io/upload_images/2477704-a67972fba7b0d97d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/511)
简单工厂模式
策略模式的优点是简化了单元测试,每个策略怎么实现完全取决于自己,而不需要简单工厂模式的switch条件判断。
建造者模式
![](http://upload-images.jianshu.io/upload_images/2477704-21028566b5e41caa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/483)
建造者模式
这里我不准备写代码了,因为代码结构和策略模式真的很像很像,唯独产出Product这一点不一样。所以策略模式是行为型的,而建造者模式是创建型的。
原型模式
![](http://upload-images.jianshu.io/upload_images/2477704-aff9b7881c5eb0bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/466)
原型模式
原型模式也是创建型的,和前两者在代码形式上也没有多大区别,主要是功能上的差异,Clone()函数返回的是一个原型对象。
综上可见策略模式、建造者模式、原型模式这三个功能迥异的模式其实核心思想是一致的。
装饰模式
![](http://upload-images.jianshu.io/upload_images/2477704-6bed5386aec1e52e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/606)
装饰模式
#include<stdio.h>
#include<string>
using namespace std;
class Compoment
{
public:
virtual void Operation()=0;
};
class ConcreteComponent:public Compoment
{
public:
void Operation()override
{
printf("具体操作\n");
}
};
class Decorator:public Compoment
{
protected:
Compoment* component;
public:
void SetComponent(Compoment* component)
{
this->component=component;
}
void Operation()override
{
if(component!=NULL)
{
component->Operation();
}
}
};
class ConcreteDecoratorA:public Decorator
{
private:
string addedState;//本类独有的功能
public:
void Operation()override
{
Decorator::Operation();//继承基类功能
addedState="new state";
printf("A的操作\n");
}
};
class ConcreteDecoratorB:public Decorator
{
private:
void AddBehavior()//本类独有的功能
{
}
public:
void Operation()override
{
Decorator::Operation();//继承基类功能
AddBehavior();
printf("B的操作\n");
}
};
int main()
{
ConcreteComponent* c=new ConcreteComponent();
ConcreteDecoratorA* dA=new ConcreteDecoratorA();
ConcreteDecoratorB* dB=new ConcreteDecoratorB();
dA->SetComponent(c);
dB->SetComponent(dA);
dB->Operation();
return 0;
}
写到这,会发现装饰模式和策略模式也有共通之处,毕竟都有聚合关系,只不过策略模式有多个具体的策略,而装饰模式有多个具体的装饰(相对于聚合关系的方向不一样)。装饰模式是结构型的。
组合模式
![](http://upload-images.jianshu.io/upload_images/2477704-d48086c1eb665a57.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/375)
组合模式
#include<stdio.h>
#include<string>
#include<list>
#include<iostream>
using namespace std;
class Company
{
protected:
string name;
public:
Company(string name)
{
this->name = name;
}
virtual void Add(Company* c)=0;
virtual void Remove(Company* c)=0;
virtual void Display(int depth)=0;
};
class Leaf :public Company//叶节点
{
public:
Leaf(string name) :Company(name){
}
void Add(Company* c)override
{
printf("不能给叶节点添加子节点");
}
void Remove(Company* c)override
{
printf("没有子节点");
}
void Display(int depth)override
{
for (int i = 0; i<depth; i++)printf("-");
cout << name << endl;
}
};
class Department :public Company//枝节点
{
private:
list<Company* >children;
public:
Department(string name) :Company(name){
}
void Add(Company* c)override
{
children.push_back(c);
}
void Remove(Company* c)override
{
//这里还没想好怎么实现
}
void Display(int depth)override
{
for (int i = 0; i<depth; i++)printf("-");
cout << name << endl;
list<Company *>::iterator iter = children.begin();
for (; iter != children.end(); iter++) //显示下层结点
(*iter)->Display(depth + 1);
}
};
int main()
{
Department* root = new Department("root");
root->Add(new Leaf("Leaf A"));
Department* com1 = new Department("subroot");
root->Add(com1);
com1->Add(new Leaf("Leaf B"));
com1->Add(new Leaf("Leaf C"));
root->Display(1);
}
组合模式和装饰模式同属于结构型,只不过组合模式的一个对象里保存着多个子类,而装饰模式只保存一个子类。
作者:野月花环
链接:https://www.jianshu.com/p/3644ec4abdb3
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。