zoukankan      html  css  js  c++  java
  • 产品类大话设计模式——简单工厂模式

    时间紧张,先记一笔,后续优化与完善。

        

    简略工厂式模释解: 

        

           简略工厂式模(Simple Factory Pattern)属于类的创新型式模,又叫静态工厂方法式模(Static FactoryMethod Pattern),是通过专门定义一个类来担任创立其他类的例实,被创立的例实常通都具有同共的父类。

        

    简略工厂式模的UML图: 

        

           简略工厂式模中包括的角色及其响应的职责如下:

        

           工厂角色(Creator):这是简略工厂式模的核心,由它担任创立有所的类的部内逻辑。当然工厂类必须可以被外界调用,创立所须要的产品对象。

        

           象抽(Product)产品角色:简略工厂式模所创立的有所对象的父类,意注,这里的父类可所以接口也可所以象抽类,它担任述描有所例实所共有的大众接口。

        

           体具产品(Concrete Product)角色:简略工厂所创立的体具例实对象,这些体具的产品往往都具有同共的父类。

        

        

    简略工厂式模深入分析

        

           简略工厂式模处理的问题是如何去例实化一个适合的对象。

        

           简略工厂式模的核心思惟就是:有一个专门的类来担任创立例实的程过。

        

           体具来说,把产品看着是一系列的类的集合,这些类是由某个象抽类或者接口生派出来的一个对象树。而工厂类用来生产一个适合的对象来满意户客的要求。

        

           如果简略工厂式模所涉及到的体具产品之间没有同共的逻辑,那么我们以可就应用接口来演扮象抽产品的角色;如果体具产品之间有功能的逻辑或,我们就必须把这些同共的西东取提出来,放在一个象抽类中,然后让体具产品继承象抽类。为实现更好复用的目标,同共的西东老是应当象抽出来的。

        

           在际实的的应用中,闲抽产品和体具产品之间往往是多层次的产品结构,如下图所示:

        

        

    面下以运算器的计设例子,该例子用的板模实现:

        

    象抽产品:运算类

        

     

    #ifndef OPERATION_H
    #define OPERATION_H
    template <class T>
    class Operation
    {
    public:
    	virtual T computeOpreration() = 0;
    	void setNumA(T _numA);
    	void setNumB(T _numB);
    	T getNumA();
    	T getNumB();
    private:
    	T numA;
    	T numB;
    };
    
    template<class T>
    void Operation<T>::setNumA(T _numA)
    {
    	numA = _numA;
    }
    
    template<class T>
    void Operation<T>::setNumB(T _numB)
    {
    	numB = _numB;
    }
    
    template<class T>
    T Operation<T>::getNumA()
    {
    	return numA;
    }
    
    template<class T>
    T Operation<T>::getNumB()
    {
    	return numB;
    }
    #endif

        

    体具产品:加法类

        每日一道理
    春蚕死去了,但留下了华贵丝绸;蝴蝶死去了,但留下了漂亮的衣裳;画眉飞去了,但留下了美妙的歌声;花朵凋谢了,但留下了缕缕幽香;蜡烛燃尽了,但留下一片光明;雷雨过去了,但留下了七彩霓虹。

        

     

    #include"Operation.h"
    #ifndef OPERATIONADD_H
    #define OPERATIONADD_H
    template<class T>
    class OperationAdd : public Operation<T>
    {
    public:
    	T computeOpreration();
    };
    
    template<class T>
    T OperationAdd<T>::computeOpreration()
    {
    	return (getNumA() + getNumB());
    }
    #endif

        

    体具产品:乘法类

        

     

    #include"Operation.h"
    #ifndef OPERATIONMUL_H
    #define OPERATIONMUL_H
    template<class T>
    class OperationMul : public Operation<T>
    {
    public:
    	T computeOpreration();
    };
    
    template<class T>
    T OperationMul<T>::computeOpreration()
    {
    	return (getNumA() * getNumB());
    }
    #endif OPERATIONMUL_H

        

    体具产品:减法类

        

     

    #include"Operation.h"
    #ifndef OPERATIONSUB_H
    #define OPERATIONSUB_H
    template<class T>
    class OperationSub : public Operation<T>
    {
    public:
    	T computeOpreration();
    };
    
    template<class T>
    T OperationSub<T>::computeOpreration()
    {
    	return (getNumA() - getNumB());
    }
    #endif

        

    体具产品:除法类

        

     

    #include"Operation.h"
    #ifndef OPERATIONDIV_H
    #define OPERATIONDIV_H
    template<class T>
    class OperationDiv : public Operation<T>
    {
    public:
    	T computeOpreration();
    };
    
    template<class T>
    T OperationDiv<T>::computeOpreration()
    {
    	return (getNumA() / getNumB());
    }
    #endif

        象抽工厂类:算计工厂类

        

     

    #include"Operation.h"
    #include"OperationSub.h"
    #include"OperationAdd.h"
    #include"OperationMul.h"
    #include"OpertaionDiv.h"
    
    #ifndef OPERATIONFACTORY_H
    #define OPERATIONFACTORY_H
    template<class T>
    class OperationFactory
    {
    public:
    	Operation<T>* createOperation(char operate);
    };
    
    template<class T>
    Operation<T>* OperationFactory<T>::createOperation(char operate)
    {
    	Operation<T> *oper = NULL;
    	switch(operate)
    	{
    	case '+':
    		oper = new OperationAdd<T>();break;
    	case '-':
    		oper = new OperationSub<T>();break;
    	case '*':
    		oper = new OperationMul<T>();break;
    	case '/':
    		oper = new OperationDiv<T>();break;
    	}
    	return oper;
    }
    #endif

        简略的main函数调用:

        

     

    #include<iostream>
    #include"OperationFactory.h"
    using namespace std;
    
    int main()
    {
    	
    	OperationFactory<double> fac;
    	Operation<double> *per = fac.createOperation('+');
    	per->setNumA(10.0);
    	per->setNumB(2.0);
    	cout<<"the result of Add is : "<<per->computeOpreration()<<endl;
    	return 0;
    }

        天今遇到要主问题还是板模的编写,前以得觉挺简略的,始终没去写,写了才发明要意注很多,尤其是译编这一块,这是天今的果成。速度跑起来。

    文章结束给大家分享下程序员的一些笑话语录: Bphone之你们聊,我先走了!移动说:我在phone前加o,我叫o缝;苹果说:我在phone前i,我是i缝;微软说:我在phone前加w,我叫w缝;三星说:你们聊,我先走了!
    将来王建宙写回忆录的时候,一定要有一句“常小兵为中国移动的发展做出了不可磨灭的贡献”。

  • 相关阅读:
    demo04-默认标签
    demo03-段落标签
    demo02-标题标签
    demo01-注释标签
    前端基础介绍
    xadmin的详细使用
    设置Linux环境变量中文显示乱码
    ES应用
    HTTP协议
    jboss
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3047711.html
Copyright © 2011-2022 走看看