简单工厂模式——C++实现

如下图所示,简单工厂模式(以计算器为例):

1:新建一个抽象类,将getResult方法封装为纯虚函数;

2、由上述抽象类派生出各个实际计算操作的类(加、减、乘、除...);

3、在一个新的类里面用静态函数来产生实际操作的对象,这个类即为工厂类;

4、客户端(使用者)调用工厂类,通过加、减、乘等具体需要的操作来产生相应的对象,进而调用对应的操作:比如通过传入‘+’字符给工厂类的createOperator()方法,产生Add类的实例,并通过该实例的指针op调用Add类里的getResult()方法


程序示例:

#include<iostream>

using namespace std;

class operation {
    
public:
    
    operation():A(0),B(0){}
    
    virtual ~operation(){ }
    
    void setA(double x){
        
        A = x;
        
    }
    
    void setB(double y){
        
        B = y;
        
    }
    
    double getA(){
        
        return A;
        
    }
    
    double getB(){
        
        return B;
        
    }
    
    virtual double getResult() = 0;  //纯虚函数
    
private:
    
    double A;
    
    double B;
    
    char ch;
    
};

class Add :public operation {
    
public:
    
    double getResult(){
        
        return getA() + getB();
        
    }
    
};

class Sub :public operation {
    
public:
    
    double getResult(){
        
        return getA() - getB();
        
    }
    
};

class Mul : public operation{
    
public:
    
    double getResult(){
        
        return getA()*getB();
        
    }
    
};

class Div :public operation{
    
public:
    
    double getResult(){
        
        if (getB() == 0){
            
            cout << "input error" << endl;
            
            return -1;
            
        }
        
        else return getA() / getB();
        
    }
    
};

class factory{
    
public:
    
    factory(){ };
    
    ~factory(){ };
    
    static operation* createOperator(char c){
        
        operation* op = NULL;     //基类指针
        
        switch (c){
                
            case '+':
                
                op = new Add();
                
                break;
                
            case '-':
                
                op = new Sub();
                
                break;
                
            case '*':
                
                op = new Mul();
                
                break;
                
            case '/':
                
                op = new Div();
                
                break;
                
            default:
                
                break;
                
        }
        
        return op;       
    }    
};

int main()

{    
    double A, B;
    
    char ch;
    
    while (cin >> A >> ch >> B){
        
        operation *op = factory::createOperator(ch);          //产生实例
        
        op->setA(A);       //动态绑定
        
        op->setB(B);           //动态绑定
        
        cout << op->getResult() << endl;     //动态绑定
        
    }   
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式简单工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。在简单工厂模式中,我们创建对象而无需向客户端暴露创建逻辑。这种类型的设计模式属于创建型模式,提供了一种创建对象的最佳方式。 C++代码实现: ```c++ #include <iostream> using namespace std; // 抽象产品 class Product { public: virtual void Show() = 0; }; // 具体产品 A class ConcreteProductA : public Product { public: void Show() { cout << "ConcreteProductA Show" << endl; } }; // 具体产品 B class ConcreteProductB : public Product { public: void Show() { cout << "ConcreteProductB Show" << endl; } }; // 工厂类 class Factory { public: Product* CreateProduct(int type) { switch (type) { case 1: return new ConcreteProductA(); case 2: return new ConcreteProductB(); default: return nullptr; } } }; int main() { Factory factory; Product* productA = factory.CreateProduct(1); if (productA != nullptr) { productA->Show(); } Product* productB = factory.CreateProduct(2); if (productB != nullptr) { productB->Show(); } delete productA; delete productB; return 0; } ``` 抽象工厂模式: 抽象工厂模式是一种创建型设计模式,它提供了一种创建一系列相关或依赖对象的最佳方式。抽象工厂模式的基本思想是提供一个接口,用于创建与产品簇相关的一族对象,而不需要明确指定它们的类。 C++代码实现: ```c++ #include <iostream> using namespace std; // 抽象产品 A class AbstractProductA { public: virtual void Show() = 0; }; // 具体产品 A1 class ProductA1 : public AbstractProductA { public: void Show() { cout << "ProductA1 Show" << endl; } }; // 具体产品 A2 class ProductA2 : public AbstractProductA { public: void Show() { cout << "ProductA2 Show" << endl; } }; // 抽象产品 B class AbstractProductB { public: virtual void Show() = 0; }; // 具体产品 B1 class ProductB1 : public AbstractProductB { public: void Show() { cout << "ProductB1 Show" << endl; } }; // 具体产品 B2 class ProductB2 : public AbstractProductB { public: void Show() { cout << "ProductB2 Show" << endl; } }; // 抽象工厂 class AbstractFactory { public: virtual AbstractProductA* CreateProductA() = 0; virtual AbstractProductB* CreateProductB() = 0; }; // 具体工厂 1 class ConcreteFactory1 : public AbstractFactory { public: AbstractProductA* CreateProductA() { return new ProductA1(); } AbstractProductB* CreateProductB() { return new ProductB1(); } }; // 具体工厂 2 class ConcreteFactory2 : public AbstractFactory { public: AbstractProductA* CreateProductA() { return new ProductA2(); } AbstractProductB* CreateProductB() { return new ProductB2(); } }; int main() { AbstractFactory* factory = new ConcreteFactory1(); AbstractProductA* productA = factory->CreateProductA(); AbstractProductB* productB = factory->CreateProductB(); productA->Show(); productB->Show(); delete productA; delete productB; factory = new ConcreteFactory2(); productA = factory->CreateProductA(); productB = factory->CreateProductB(); productA->Show(); productB->Show(); delete productA; delete productB; delete factory; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值