C++ 学习笔记之 工厂模式 代码

简单工厂模式

  1. 客户端和具体实现类解耦。
  2. 简单工厂模式,工厂并不符合开闭原则(每次都得修改工厂代码)。

简单工厂模式使用场景

  1. 工厂类负责创建的对象比较少,由于创建的对象较少,不会造成工厂方法中的业务逻辑太过复杂。
  2. 客户端只知道传入工厂类的参数,对于创建对象不关心。

通过操作简单工厂类,让工厂类来new一个类。示例:

#include<iostream>
//定义水果基类。//定义抽象水果
class AbstractFruit{
    public:
        virtual void Showname()=0;//使用虚函数在这里,定义了一个方法 这个函数在水果里面被实例化 
};



class Apple : public AbstractFruit{ //public 继承,原来 public 变成了public 原来的protected 变成了 protected、、、  按照优先级原则来  private> protected >public 
    public:
        virtual void Showname(){ //实现之前定义的方法
            std::cout<< "我是苹果"<< std::endl;
        }
};

class Pear : public AbstractFruit{ 
    public:
        virtual void Showname(){
            std::cout<< "我是鸭梨"<< std::endl;
        }
};

class Banana : public AbstractFruit{ 
    public:
        virtual void Showname(){
            std::cout<< "我是香蕉"<< std::endl;
        }
};

//定义水果工厂
class FruitFactory{
    public:
        static AbstractFruit* CreateFruit(std::string flag){ //返回一个抽象的类
                if (flag == "apple"){
                    return new Apple;
                }
                else if(flag == "pear"){
                    return new Pear;
                    }
                else if(flag == "banana"){
                    return new Banana;
                    }
            }
        
};

void test01(){
    FruitFactory* factory = new FruitFactory;
    AbstractFruit* fruit = factory->CreateFruit("banana");
    fruit->Showname();
    delete fruit;

    fruit = factory->CreateFruit("apple");
    fruit->Showname();
    delete fruit;

    fruit = factory->CreateFruit("banana");
    fruit->Showname();
    delete fruit;

    fruit = factory->CreateFruit("pear");
    fruit->Showname();
    delete fruit;

}

int main(){
    test01();
}

工厂方法模式在这里插入图片描述

  1. 类的个数成倍增加,导致类越来越多。
  2. 需要自己去判断创建哪一个类。
  3. 创建过程延迟到了子类,实现了对象创建和分离,好处就是符合了开闭原则。

工厂方法模式 = 简单工厂模式 + 开闭原则

#include<iostream>
//定义水果基类。//定义抽象水果
class AbstractFruit{
    public:
        virtual void Showname()=0;//使用虚函数在这里,定义了一个方法 这个函数在水果里面被实例化 
};



class Apple : public AbstractFruit{ //public 继承,原来 public 变成了public 原来的protected 变成了 protected、、、  按照优先级原则来  private> protected >public 
    public:
        virtual void Showname(){ //实现之前定义的方法
            std::cout<< "我是苹果"<< std::endl;
        }
};

class Pear : public AbstractFruit{ 
    public:
        virtual void Showname(){
            std::cout<< "我是鸭梨"<< std::endl;
        }
};

class Banana : public AbstractFruit{ 
    public:
        virtual void Showname(){
            std::cout<< "我是香蕉"<< std::endl;
        }
};

//定义抽象工厂
class AbstractFruitFactory{
    public:
    virtual AbstractFruit* CreateFruit() = 0;
};

class AppleFactory: public AbstractFruitFactory{
    virtual AbstractFruit* CreateFruit(){
        return new Apple;
    }
};
class BananaFactory: public AbstractFruitFactory{
    virtual AbstractFruit* CreateFruit(){
        return new Banana;
    }
};

class PearFactory: public AbstractFruitFactory{
    virtual AbstractFruit* CreateFruit(){
        return new Pear;
    }
};


void test01(){
    //这里我有点疑问就是如果直接定义AbstractFruitFactory* factory = new BananaFactory;
    //好像后面再赋值 factory = new AppleFactory;会报错 这是什么原因呢。
    AbstractFruitFactory* factory = NULL;
    AbstractFruit* fruit = NULL;
    factory = new BananaFactory;
    fruit = factory->CreateFruit();
    fruit->Showname();
    delete fruit;
    delete factory;
    factory = new AppleFactory;
    fruit = factory->CreateFruit();
    fruit->Showname();
    delete fruit;
    delete factory;

    factory = new PearFactory;
    fruit = factory->CreateFruit();
    fruit->Showname();
    delete fruit;
    delete factory;


}

int main(){
    test01();
}

抽象工厂模式

在这里插入图片描述

  1. 先抽象产品等级 如:abstract apple;
  2. 创建抽象类:
class AbstractApple{
public:
	virtual void ShowName() = 0;
};
  1. 创建派生类:
class CNApple : public AbstractApple{
public:
	virtual void ShowName(){
	std::cout<<"中国苹果"<<endl;
	}
};
  1. 创建抽象工厂,针对产品族:
class  AbstractFactory{
public:
	virtual AbstractApple* CreateApple() = 0;
}
  1. 创建派生工厂:
class ChinaFactory: public AbstractFactory{
public:
	virtual AbstractApple* CreateApple(){
		return new CNApple;
	}
}
  1. 调用:
void test(){
	AbstractFactory* factory = NULL;
	AbstractApple* apple = NULL;
	factory = new ChinaFactory;
	apple = factory->CreateApple();
}

完整代码如下:

#include<iostream>
//创建抽象类
class AbstractApple{
public:
    virtual void Showname()=0;
};
class AbstractBanana{
public:
    virtual void Showname()=0;
};
class AbstractPear{
public:
    virtual void Showname()=0;
};

//创建派生类
class CNapple : public AbstractApple{
public:
    virtual void Showname(){
        std::cout<<"Chinese apple"<<std::endl;
    }
};
class CNbanana : public AbstractBanana{
public:
    virtual void Showname(){
        std::cout<<"Chinese banana"<<std::endl;
    }
};
class CNpear : public AbstractPear{
public:
    virtual void Showname(){
        std::cout<<"Chinese pear"<<std::endl;
    }
};


class USAapple : public AbstractApple{
    public:
        virtual void Showname(){
            std::cout<<"American apple"<<std::endl;
        }
};
class USAbanana : public AbstractBanana{
    public:
        virtual void Showname(){
            std::cout<<"American banana"<<std::endl;
        }
};
class USApear : public AbstractPear{
    public:
        virtual void Showname(){
            std::cout<<"American pear"<<std::endl;
        }
};


class JPapple : public AbstractApple{
    public:
        virtual void Showname(){
            std::cout<<"Japanese apple"<<std::endl;
        }
};
class JPbanana : public AbstractBanana{
    public:
        virtual void Showname(){
            std::cout<<"Japanese banana"<<std::endl;
        }
};
class JPpear : public AbstractPear{
    public:
        virtual void Showname(){
            std::cout<<"Japanese pear"<<std::endl;
        }
};

//创建抽象工厂,针对产品族
class AbstractFactory{
    public:
        virtual AbstractApple* CreateApple() = 0;
        virtual AbstractBanana* CreateBanana() = 0;
        virtual AbstractPear* CreatePear() = 0;
};

//创建派生
class ChinaFactory : public AbstractFactory{
    public:
        virtual AbstractApple* CreateApple(){
            return new CNapple;
        }        
        virtual AbstractBanana* CreateBanana(){
            return new CNbanana;
        }
        virtual AbstractPear* CreatePear(){
            return new CNpear;
        }
};

class AmericanFactory : public AbstractFactory{
    public:
        virtual AbstractApple* CreateApple(){
            return new USAapple;
        }        
        virtual AbstractBanana* CreateBanana(){
            return new USAbanana;
        }
        virtual AbstractPear* CreatePear(){
            return new USApear;
        }
};

class JapanFactory : public AbstractFactory{
    public:
        virtual AbstractApple* CreateApple(){
            return new JPapple;
        }        
        virtual AbstractBanana* CreateBanana(){
            return new JPbanana;
        }
        virtual AbstractPear* CreatePear(){
            return new JPpear;
        }
};

void test(){
    AbstractFactory* factory = NULL;    
    AbstractApple* apple = NULL;
    AbstractBanana* banana = NULL;
    AbstractPear* pear = NULL;

    factory = new ChinaFactory;     //定义一个新工厂
    apple = factory->CreateApple(); // factory 生产一个apple
    banana = factory->CreateBanana();
    pear = factory->CreatePear();

    apple->Showname();
    banana->Showname();
    pear ->Showname();

    delete pear;
    delete banana;
    delete apple;
    delete factory;
}

int main(){
    test();
    return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页