设计模式——简单工厂

第一版: 简单实现

  在int main 主函数中读取数据,使用switch语句分别进行操作。不同操作用类实现。

 

#include <iostream>
using namespace std;
    static int num_a;
    static int num_b;
    static char op;


void input_num()
{
    cout<<"input_num : ";
}
void input_op()
{
    cout<<"input_op : ";
}
class Op_Add
{
    public:
        int calc()
        {
            return num_a+num_b;
        }    
};
class Op_Minus
{
    public:
        int calc()
        {
            return num_a-num_b;
        }    
};
class Op_Mut
{
    public:
        int calc()
        {
            return num_a*num_b;
        }    
};
class Op_Div
{
    public:
        int calc()
        {
            if(num_b)
            {
                return num_a/num_b;
            }
            else
            {
                cout<<"分母为0"<<endl;
                return 0;
            }
            
        }    
};
int main(int argc, char *argv[])
{

    input_num();
    cin>>num_a;
    input_num();
    cin>>num_b;
    input_op();
    cin>>op;
    switch (op)
    {
        case '+':
        {
            Op_Add op1;
            cout<<"result is "<<op1.calc()<<endl;
            break;
        }
        case '-':
        {
            Op_Minus op2;
            cout<<"result is "<<op2.calc()<<endl;
            break;
        }
        case '*':
        {
            Op_Mut op3;
            cout<<"result is "<<op3.calc()<<endl;
            break;
        }
        case '/':
        {
            Op_Div op4;
            cout<<"result is "<<op4.calc()<<endl;
            break;
        }
        default:
            break;
        
        
    }
    return 0;
}

 

第二版:输入数据和计算结果分离

  使用Input类输入数据,operation类实现计算。

  int main主函数语句变少。

#include <iostream>
using namespace std;
//依据使用习惯,输入数字,再输入运算符 
//使用静态变量存储要进行操作的数和符号 
//Input类从终端读取数据
 

static int num_a=0;
static int num_b=0;
static char op;
class Input 
{
    public:
        void input_num();
        void input_op();
        //static int num_a;    
        //static int num_b;    
        //char op;
        istream& set_num_a(istream& is);
        istream& set_num_b(istream& is);
        istream& set_op(istream& is);
        void read_data()
        {
            set_num_a(cin);
            cout<<"input num_a"<<endl;
            set_num_b(cin);
            cout<<"input num_b"<<endl;
            set_op(cin);
            cout<<"input op"<<endl;
            cout<<"input done"<<endl;
        }
        
};

void Input::input_num()
{
    cout<<"input_num : ";
}
void Input::input_op()
{
    cout<<"input_op : ";
}
istream& Input::set_num_a(istream& is)
{
    this->input_num();
    is >> num_a;
    //this->num_a = _a;
}
istream& Input::set_num_b(istream& is)
{
    this->input_num();
    is >> num_b;
    //this->num_b = _b;
}
istream& Input::set_op(istream& is)
{
    this->input_op();
    is >> op;
    //this->op = _op;
}
class Op_four //貌似没用的纯虚类 
{
    public:
        virtual int calc()=0;    
        
} ;
class Op_Add: public Op_four
{
    public:
        int calc()
        {
            cout<<"num_a "<<num_a<<endl;
            cout<<"num_b "<<num_b<<endl;
            return num_a+num_b;
        }    
};
class Op_Minus: public Op_four
{
    public:
        int calc()
        {
            return num_a-num_b;
        }    
};
class Op_Mut: public Op_four
{
    public:
        int calc()
        {
            return num_a*num_b;
        }    
};
class Op_Div: public Op_four
{
    public:
        int calc()
        {
            if(num_b)
            {
                return num_a/num_b;
            }
            else
            {
                cout<<"分母为0"<<endl;
                return 0;
            }
            
        }    
};
class Op_sqrt: public Op_four
{
    public:
        int calc()
        {
            if(num_a >=0 )
            {
                //开更号(假装完成)
             cout<<"开更号"<<endl; 
            }    
        }
         
        
};
class Operation //运算类 
{
    public:
        
        int get_result(int _a,int _b,char _op)
        {
            int result;
            
    switch (_op)
    {
        case '+':
        {
            Op_Add op1;
            cout<<"result is "<<op1.calc()<<endl;
            break;
        }
        case '-':
        {
            Op_Minus op2;
            cout<<"result is "<<op2.calc()<<endl;
            break;
        }
        case '*':
        {
            Op_Mut op3;
            cout<<"result is "<<op3.calc()<<endl;
            break;
        }
        case '/':
        {
            Op_Div op4;
            cout<<"result is "<<op4.calc()<<endl;
            break;
        }
        case 's':
        {
            Op_sqrt op5;
            cout<<"result is "<<op5.calc()<<endl;
        }
        default:
            break;
        
        
    }
            
        }    
};
int main(int argc, char *argv[])
{
    Input input;
    input.read_data();
    Operation op1;
    op1.get_result(num_a,num_b,op);

    
    return 0;
}

 

第三版:

  简单工厂模式:产品和工厂类。产品为抽象类,可以派生出不同的产品,工厂类中有switch语句可以进行判断。

 

#include <iostream>
using namespace std;
//先输入运算符,再输入数据 
//定以四则运算的抽象类,作为父类,具有操作数字段 
class Op_four// 抽象类 
{
    private:
        int _num_a=0;
        int _num_b=0;
    public:
        virtual int calc()=0;//重载函数 
        //获取运算符 
        int get_num_a()
        {
            return _num_a;
        }
        int get_num_b()
        {
            return _num_b;
        }
        //设置操作数 
        void set_num_a(istream& is)
        {
            cout<<"input_num : ";
            is >> this->_num_a ;
        }
        void set_num_b(istream& is)
        {
            cout<<"input_num : ";
            is >> this->_num_b ;
        }
            
        
} ;
class Op_Add: public Op_four
{
    public:
        int calc()
        {
            //cout<<"num_a "<<num_a<<endl;
            //cout<<"num_b "<<num_b<<endl;
            return get_num_a()+get_num_b();
        }    
};
class Op_Minus: public Op_four
{
    public:
        int calc()
        {
            return get_num_a()- get_num_b();
        }    
};
class Op_Mut: public Op_four
{
    public:
        int calc()
        {
            return get_num_a()*get_num_b();
        }    
};
class Op_Div: public Op_four
{
    public:
        int calc()
        {
            if(get_num_b())
            {
                return get_num_a()/get_num_b();
            }
            else
            {
                cout<<"分母为0"<<endl;
                return 0;
            }
            
        }    
};
/*class Op_sqrt: public Op_four
{
    public:
        int calc()
        {
            if(num_a >=0 )
            {
                //开更号
             cout<<"开更号"<<endl; 
            }    
        }
         
        
};*/
class OperationFactory //运算工厂类 
{
    public:
        
        int get_result(char _op)
        {
            int result;
            
    switch (_op)
    {
        case '+':
        {
            Op_Add op1;
            op1.set_num_a(cin);
            op1.set_num_b(cin);
            
            cout<<"result is "<<op1.calc()<<endl;
            break;
        }
        case '-':
        {
            Op_Minus op2;
            op2.set_num_a(cin);
            op2.set_num_b(cin);
            cout<<"result is "<<op2.calc()<<endl;
            break;
        }
        case '*':
        {
            Op_Mut op3;
            op3.set_num_a(cin);
            op3.set_num_b(cin);
            cout<<"result is "<<op3.calc()<<endl;
            break;
        }
        case '/':
        {
            Op_Div op4;
            op4.set_num_a(cin);
            op4.set_num_b(cin);
            cout<<"result is "<<op4.calc()<<endl;
            break;
        }
        /*case 's':
        {
            Op_sqrt op5;
            cout<<"result is "<<op5.calc()<<endl;
        }*/
        default:
            break;
        
        
    }
            
        }    
};
int main(int argc, char *argv[])
{
    OperationFactory *of=new OperationFactory();
    //输入要进行的运算符号 
    of->get_result('-');
    delete of;
    
    return 0;
}

 第四版:优化数据读入,在产品的构造函数中进行数据读取

#include <iostream>
//工厂模式
 
using namespace std;
class Operation//产品类 
{
    private:
        int _num_a=0;
        int _num_b=0;
    public:
        void set_num(istream& is)
        {
            cout<<"input num_a: ";
            is>>_num_a;
            cout<<"input num_b: ";
            is>>_num_b;
        }
        Operation()
        {
            set_num(cin);//在构造产品时输入数据 
        }
        int get_num_a()    
        {
            return _num_a;
        }
        int get_num_b()    
        {
            return _num_b;
        }
        virtual int calc()=0;
        
};
class Op_Add:public Operation
{
    public:
    int calc()
    {
        
        return get_num_a()+get_num_b();
    }    
};
class Factory
{
    public:
        int creat()//在工厂中获得运算符 
        {
            char op;
            cout<<"input op: ";
            cin>>op;
            switch(op)
            {
                case '+':
                {
                    Operation *op1=new Op_Add();
                    //Op_Add *op1 = new Op_Add();
                    cout<<"result is "<<op1->calc();
                    delete op1;
                    break;
                }
                case '-':
                {
                    break;
                }
                default:
                {
                    cout<<"do not support"<<endl;
                    break;
                }
                    
            }
        }
            
};
int main(int argc, char *argv[])
{
    
    Factory *fac = new Factory();
    fac->creat();
    delete fac;
    return 0;
}

 

posted @ 2022-04-09 22:55  菠萝超级酸  阅读(23)  评论(0编辑  收藏  举报