设计模式:工厂模式

一.简单工厂模式

简单工厂模式是根据传入工厂类的参数,动态决定创建哪一类产品类

一个工厂类负责多个产品类,根据传进来的参数决定生产哪个产品

 

复制代码
//产品类
class Product{

public:
        virtual void show()==0;

};

class Product_A:public Product{

public:
        void show(){
            cout<<"Product_A"<<endl;
        }

};
class Product_B:public Product{
    
public:
        void show(){
            cout<<"Product_B"<<endl;
        }
};
复制代码

 

复制代码
//工厂类
class Factory{

public:
        Product* Create(int i){
            switch(i){
                case 1:
                    return new Product_A;
                    break;
                case 2:
                    return new Product_B;
                    break;
                default:
                    break;
            }
        }
};
复制代码
//调用
int main(){
    Factory* factory=new Factory();
    factory->Create(1)->show();
    factory->Create(2)->show();
}

 

二.工厂方法模式

多个工厂类对应多个产品类,一个工厂类对应一个产品类,互不干扰

 

复制代码
//产品类
class Product{

public:
        virtual void show()=0;
};

class Product_A:public Product{

public:
        void show(){
            cout<<"Product_A"<<endl;
        }
};

class Product_B:public Product{

public:
        void show(){
            cout<<"Product_B"<<endl;
        }
};
复制代码

 

复制代码
//工厂类
class Factory{

public:
        virtual Product* create()=0;
};

class Factory_A:public Factory{

public:
        Product* create(){
            return new Product_A;
        }
};

class Factory_B:public Factory{

pubilc:
        Product* create(){
            return new Product_B;
        }
};
复制代码
//调用
int main(){
    Factory_A* productA=new Factory_A();
    Factory_B* productB=new Factory_B();

    productA->create()->show();
    productB->create()->show();
}

 

三.抽象工厂模式

多个工厂类对应多个不同种类的产品类,一个工厂类对应一个种类多个产品类,互不干扰

 

复制代码
//产品类
class Product1{

public:
        virtual void show()=0;
};

class Product_A1:public Product1{

public:
        void show(){
            cout<<"product_A1"<<endl;
        }
};

class Product_B1:public Product1{
    
public:
        void shwo(){
            cout<<"product_B1"<<endl;
        }
};


class Product2{

public:
        virtual void show()=0;
};

class product_A2:public Product2{

public:
        void show(){
            cout<<"product_A2"<<endl;
        }
};

class product_B2:public Product2{

public:
        void show(){
            cout<<"product_B2"<<endl;
        }
};
复制代码

 

复制代码
//工厂类
class Factory{

public:
        virtual Product1* create1()=0;
        virtaul Product2* create2()=0;
};

class FactoryA{

public:
        Product1* create1(){
            return new product_A1();
        }
        Product2* create2(){
            return new product_A2();
        }
};

class FactoryB{

public:
        Product1* create1(){
            return new product_B1();
        }
        Product2* create2(){
            return new product_B2();
        }
};
复制代码
复制代码
int main(){
    FactoryA* factoryA=new FactoryA();
    factoryA->create1()->show();
    factoryB->create2()->show();

    FactoryB* factoryB=new FactoryB();
    factoryB->create1()->show();
    factoryB->create2()->show();
}
复制代码

 

posted @   言午丶  阅读(137)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示