常见的设计模式及其C++实现(二):策略,建造者,原型,装饰,组合

转载地址:https://www.jianshu.com/p/3644ec4abdb3

策略模式

策略模式
#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图应该是这样:

简单工厂模式

策略模式的优点是简化了单元测试,每个策略怎么实现完全取决于自己,而不需要简单工厂模式的switch条件判断。

建造者模式

建造者模式

这里我不准备写代码了,因为代码结构和策略模式真的很像很像,唯独产出Product这一点不一样。所以策略模式是行为型的,而建造者模式是创建型的。

原型模式

原型模式

原型模式也是创建型的,和前两者在代码形式上也没有多大区别,主要是功能上的差异,Clone()函数返回的是一个原型对象。
综上可见策略模式、建造者模式、原型模式这三个功能迥异的模式其实核心思想是一致的。

装饰模式

装饰模式
#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;
} 

写到这,会发现装饰模式和策略模式也有共通之处,毕竟都有聚合关系,只不过策略模式有多个具体的策略,而装饰模式有多个具体的装饰(相对于聚合关系的方向不一样)。装饰模式是结构型的。

组合模式

组合模式
#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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值