23种设计模式
image.png

1.工厂模式

工厂模式包括了三种,分别为简单工厂模式,工厂方法模式以及抽象工厂模式。
简单工厂模式:在工厂类中去做判断创建新产品,但是增加产品,需要修改工厂类

  1. enum CTYPE {COREA, COREB};
  2. class SingleCore
  3. {
  4. public:
  5. virtual void Show() = 0;
  6. };
  7. //单核A
  8. class SingleCoreA: public SingleCore
  9. {
  10. public:
  11. void Show() { cout<<"SingleCore A"<<endl; }
  12. };
  13. //单核B
  14. class SingleCoreB: public SingleCore
  15. {
  16. public:
  17. void Show() { cout<<"SingleCore B"<<endl; }
  18. };
  19. //唯一的工厂,可以生产两种型号的处理器核,在内部判断
  20. class Factory
  21. {
  22. public:
  23. SingleCore* CreateSingleCore(enum CTYPE ctype)
  24. {
  25. if(ctype == COREA) //工厂内部判断
  26. return new SingleCoreA(); //生产核A
  27. else if(ctype == COREB)
  28. return new SingleCoreB(); //生产核B
  29. else
  30. return NULL;
  31. }
  32. };

工厂方法模式:定义一个用于创建对象的接口,但是每增加一个产品,就需要增加一个对象的工厂

class SingleCore  
{  
public:  
    virtual void Show() = 0;
};  

//单核A  
class SingleCoreA: public SingleCore  
{  
public:  
    void Show() { cout<<"SingleCore A"<<endl; }  
};  
//单核B  
class SingleCoreB: public SingleCore  
{  
public:  
    void Show() { cout<<"SingleCore B"<<endl; }  
};  
class Factory  //定义一个接口父类
{  
public:  
    virtual SingleCore* CreateSingleCore() = 0;
};  

//生产A核的工厂  
class FactoryA: public Factory  
{  
public:  
    SingleCoreA* CreateSingleCore() { return new SingleCoreA(); }  
};  
//生产B核的工厂  
class FactoryB: public Factory  
{  
public:  
    SingleCoreB* CreateSingleCore() { return new SingleCoreB() ; }  
};

抽象工厂模式:定义为提供一个创建一系列的相互依赖对象的接口,而无需指定具体的类。本质就是用子类的对象指向父类的指针,再利用虚函数实现覆盖。

//单核  
class SingleCore   
{  
public:  
    virtual void Show() = 0;
};  

class SingleCoreA: public SingleCore    
{  
public:  
    void Show() { cout<<"Single Core A"<<endl; }  
};  
class SingleCoreB :public SingleCore  
{  
public:  
    void Show() { cout<<"Single Core B"<<endl; }  
};  

//多核  
class MultiCore    
{  
public:  
    virtual void Show() = 0;
};  
class MultiCoreA : public MultiCore    
{  
public:  
    void Show() { cout<<"Multi Core A"<<endl; }  

};  
class MultiCoreB : public MultiCore    
{  
public:  
    void Show() { cout<<"Multi Core B"<<endl; }  
};  

//工厂  
class CoreFactory    
{  
public:  
    virtual SingleCore* CreateSingleCore() = 0;
    virtual MultiCore* CreateMultiCore() = 0;
};  

//工厂A,专门用来生产A型号的处理器  
class FactoryA :public CoreFactory  
{  
public:  
    SingleCore* CreateSingleCore() { return new SingleCoreA(); }  
    MultiCore* CreateMultiCore() { return new MultiCoreA(); }  
};  

//工厂B,专门用来生产B型号的处理器  
class FactoryB : public CoreFactory  
{  
public:  
    SingleCore* CreateSingleCore() { return new SingleCoreB(); }  
    MultiCore* CreateMultiCore() { return new MultiCoreB(); }  
};
简单工厂模式 工厂方法模式 抽象工厂模式
image.png image.png image.png

2.Template Method

image.png
image.png
image.png
image.png

3.策略模式

image.png
原本的代码

enum TaxBase {
    CN_Tax,
    US_Tax,
    DE_Tax,
    FR_Tax       //更改
};

class SalesOrder{
    TaxBase tax;
public:
    double CalculateTax(){
        //...

        if (tax == CN_Tax){
            //CN***********
        }
        else if (tax == US_Tax){
            //US***********
        }
        else if (tax == DE_Tax){
            //DE***********
        }
        else if (tax == FR_Tax){  //更改
            //...
        }

        //....
     }

};

采用策略模式后

class TaxStrategy{
public:
    virtual double Calculate(const Context& context)=0;
    virtual ~TaxStrategy(){}
};


class CNTax : public TaxStrategy{
public:
    virtual double Calculate(const Context& context){
        //***********
    }
};

class USTax : public TaxStrategy{
public:
    virtual double Calculate(const Context& context){
        //***********
    }
};

class DETax : public TaxStrategy{
public:
    virtual double Calculate(const Context& context){
        //***********
    }
};



//扩展
//*********************************
class FRTax : public TaxStrategy{
public:
    virtual double Calculate(const Context& context){
        //.........
    }
};


class SalesOrder{
private:
    TaxStrategy* strategy;

public:
    SalesOrder(StrategyFactory* strategyFactory){
        this->strategy = strategyFactory->NewStrategy(); //工厂模式产生的对象
    }
    ~SalesOrder(){
        delete this->strategy;
    }

    public double CalculateTax(){
        //...
        Context context();

        double val = 
            strategy->Calculate(context); //多态调用
        //...
    }

};

image.png

4.观察者模式

5.装饰者模式

image.png

image.png

6.桥模式

image.png
image.png