设计模式之简单工厂模式、工厂模式、抽象工厂模式

参考: 设计模式笔记

简单工厂模式

● 将类的创建过程交给工厂类实现,如果需要一个类对象,则直接通过工厂创建一个类。
● 简单工厂模式不符合开闭原则
● 适用场景:工厂类负责创建的对象比较少;客户端只知道传入工厂类的参数,对于如何创建对象不关心

/*
类的创建过程交给工厂类来实现,如果需要一个类的对象,则直接通过工厂创建一个对应的类对象
*/
#include
using namespace std;
class AbstractFruit{
public:
    virtual void showName()=0;
};

//苹果
class Apple:public AbstractFruit{
public:
    virtual void showName(){
        cout<<"i am a appple"<<endl;
    }
};
//香蕉
class Banana:public AbstractFruit{
public:
    virtual void showName(){
        cout<<"i am a banana"<<endl;
    }
};


//水果工厂
class FruitFactory{
public:
    static AbstractFruit*createFruit(string type){
        if(type=="apple"){
            return new Apple();
        }else if(type=="banana"){
            return new Banana();
        }else return nullptr;
    }
};

int main(){
    FruitFactory*factory=new FruitFactory();
    AbstractFruit*fruit=factory->createFruit("apple");
    fruit->showName();//i am a appple
    delete fruit;
    fruit=factory->createFruit("banana");
    fruit->showName();//i am a banana
    delete fruit;
    delete factory;
    return 0;
    

}

工厂模式(简单工厂模式+开闭原则)

● 代码扩展时不需要修改工厂类
● 类急速膨胀,不利于维护
● 适用场景:消费者不关心它所要创建的产品类;或者知道要创建的产品类二不关心如何创建的时候

/*
    简单工厂模式+开闭原则
*/
#include
using namespace std;
class AbstractFruit{
public:
    virtual void showname()=0;
};

class Apple:public AbstractFruit{
public:
    virtual void showname(){
        cout<<"i am a apple"<<endl;
    }
};

class Banana:public AbstractFruit{
public:
    virtual void showname(){
        cout<<"i am a banana"<<endl;
    }
};
class Orange:public AbstractFruit{
public:
     virtual void showname(){
        cout<<"i am an orange"<<endl;
    }
};

//之前是水果工厂,如果要添加对象都会在工厂里面添加,破坏了开闭原则
//设计成抽象工厂
class AbstractFactory{
public:
    virtual AbstractFruit*createFruit()=0;

};

//苹果工厂
class AppleFactory:public AbstractFactory{
public:
    virtual AbstractFruit*createFruit(){
        return new Apple();
    }
};


//香蕉工厂
class BananaFactory:public AbstractFactory{
public:
    virtual AbstractFruit*createFruit(){
        return new Banana();
    }
};
//橙子工厂
class OrangeFactory:public AbstractFactory{
public:
    virtual AbstractFruit*createFruit(){
        return new Orange();
    }
};


int main(){
    AbstractFactory*factory=new AppleFactory();
    AbstractFruit*fruit=factory->createFruit();
    fruit->showname();//i am a apple
    delete factory;
    delete fruit;
    factory=new BananaFactory();
    fruit=factory->createFruit();
    fruit->showname(); //i am a banana
    delete factory;
    delete fruit;
     factory=new OrangeFactory();
    fruit=factory->createFruit();
    fruit->showname();//i am an orange
    delete factory;
    delete fruit;
    return 0;

}

抽象工厂模式(增加产品族方便)

● 产品族:同一产地或者同一产商,功能不同
● 产品等级:功能相同,产地或者产商不同
● 不符合开闭原则,增加新的产品等级麻烦,甚至要在抽象层修改代码
● 适用场景:

  1. 系统中有多于一个的产品族。而每次只使用其中某一产品族。可以通过配置文件等方式来使得用户可以动态改变产品族,也可以很方便地增加新的产品族。
  2. 产品等级结构稳定。设计完成之后,不会向系统中增加新的产品等级结构或者删除已有的产品等级结构。
/*
 针对产品族而不是产品等级结构
*/
#include
using namespace std;
//抽象产品等级
class AbstractApple{
public:
    virtual void showname()=0;
};
class AbstractBanana{
public:
    virtual void showname()=0;
};
//具体产品等级
//武汉苹果
class AppleFromWhuhan:public AbstractApple{
public:
    virtual void showname(){
        cout<<" apple frome whuhan"<<endl;
    }
};
//上海苹果
class AppleFromShanghai:public AbstractApple{
public:
    virtual void showname(){
        cout<<" apple frome shanghai"<<endl;
    }
};
//武汉香蕉
class BananaFromWhuhan:public AbstractBanana{
public:
    virtual void showname(){
        cout<<" banana frome whuhan"<<endl;
    }
};
//上海香蕉
class BananafromShanghai:public AbstractBanana{
public:
    virtual void showname(){
        cout<<"banana frome shanghai"<<endl;
    }
};
//抽象工厂
class AbstractFactory{
public :
    virtual AbstractApple*createApple()=0;
    virtual AbstractBanana*createBanana()=0;

};
//武汉工厂
class WhuhanFactory:public AbstractFactory{
public:
    AbstractApple*createApple(){
        return new AppleFromWhuhan();
    }
     AbstractBanana*createBanana(){
        return new BananaFromWhuhan();
    }
};
//上海工厂
class ShanghaiFactory:public AbstractFactory{
public:
    AbstractApple*createApple(){
        return new AppleFromShanghai();
    }
     AbstractBanana*createBanana(){
        return new BananafromShanghai();
    }
};

int main(){
    AbstractFactory*factory=nullptr;
    AbstractApple*apple=nullptr;
    AbstractBanana*banana=nullptr;
    //武汉苹果和香蕉
    factory=new WhuhanFactory();
    apple=factory->createApple();
    banana=factory->createBanana();
    apple->showname();// apple frome whuhan
    banana->showname();// banana frome whuhan
    delete factory;
    delete apple;
    delete banana;
    //上海苹果和香蕉
    factory=new ShanghaiFactory();
    apple=factory->createApple();
    banana=factory->createBanana();
    apple->showname();// apple frome shanghai
    banana->showname();//banana frome shanghai
    delete factory;
    delete apple;
    delete banana;
}

你可能感兴趣的:(设计模式,设计模式,简单工厂模式,抽象工厂模式)