简单工厂: 用一个单独的类来做创造实例的过程,就是工厂。

优点:在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。

例子:实现计算器代码

1 计算和显示分开
2 灵活地修改和扩展(开放封闭原则)
3 单一原则(加减乘除各个自成一类)

/**
*    File    : IOperation.h
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: interface of operation
*/

#if !defined _IOPERATION_H_
#define _IOPERATION_H_

class IOperation
{
public:
    IOperation(double _doubleA = 0, double _doubleB = 0);
    virtual ~IOperation(){};
   
    virtual double Execute() = 0;

    void SetA(double _doubleA);
    double GetA();

    void SetB(double _doubleB);
    double GetB();

protected:
private:
    double m_doubleA;
    double m_doubleB;

};

#endif

/**
*    File    : IOperation.cpp
*    Author    : Eastong
*    Date    : 11/3/2008
*    Function: the realization of IOperation
*/

#include "IOperation.h"


IOperation::IOperation(double _doubleA /* = 0 */, double _doubleB /* = 0 */)
{
    m_doubleA = _doubleA;
    m_doubleB = _doubleB;
}

void IOperation::SetA(double _doubleA)
{
    m_doubleA = _doubleA;
}
double IOperation::GetA()
{
    return m_doubleA;
}

void IOperation::SetB(double _doubleB)
{
    m_doubleB = _doubleB;
}

double IOperation::GetB()
{
    return m_doubleB;
}

/**
*    File    : CAdd.h
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: the declaration of Add operation
*/
#if !defined _CADD_H_
#define _CADD_H_

#include "IOperation.h"

class CAdd : public IOperation
{
public:
    virtual double Execute();
protected:
private:
};

#endif

/**
*    File    : CAdd.cpp
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: the realization of Add operation
*/

#include "CAdd.h"

double CAdd::Execute()
{
    return GetA() + GetB();
}

/**
*    File    : CSub.h
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: the declaration of sub operation
*/
#if !defined _CSUB_H_
#define  _CSUB_H_

#include "IOperation.h"

class CSub : public IOperation
{
public:
    virtual double Execute();
protected:
private:
};

#endif

/**
*    File    : CSub.h
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: the realization of sub operation
*/

#include "CSub.h"

double CSub::Execute()
{
    return GetA() - GetB();
}

/**
*    File    : CMul.h
*    Author  : Eastsong
*    Date    : 11/3/2008
*    Function: the declaration of mul operation
*/
#if !defined _CMUL_H_
#define  _CMUL_H_

#include "IOperation.h"

class CMul : public IOperation
{
public:
    virtual double Execute();
protected:
private:
};

#endif
/**
*    File    : CMul.cpp
*    Author  : Eastsong
*    Date    : 11/3/2008
*    Function: the realization of mul operation
*/
#include "CMul.h"

double CMul::Execute()
{
    return GetA() * GetB();
}

/**
*    File    : CDiv.h
*    Author  : Eastsong
*    Date    : 11/3/2008
*    Function: the declaration of div operation
*/
#if !defined _CDIV_H_
#define _CDIV_H_

#include "IOperation.h"

class CDiv : public IOperation
{
public:
    virtual double Execute();
protected:
private:
};

#endif

/**
*    File    : CDiv.cpp
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: the realization of div operation
*/
#include "CDiv.h"

double CDiv::Execute()
{
    if (0 != GetB())
    {
        return GetA() / GetB();
    }
    else
    {
        /**
        * I do not know how to throw the exception using c++
        */
        //throw Exception();

        return 0;
    }
}

/**
*    File    : OperationFactory.h
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: factory of create operation object
*/

#if !defined _OPERATION_FACTORY_H_
#define _OPERATION_FACTORY_H_

#include "IOperation.h"

class OperationFactory
{
public:
    static IOperation * CreateOperation(char  op);
protected:
private:
};

#endif

/**
*    File    : OperationFactory.cpp
*    Author    : Eastsong
*    Date    : 11/3/2008
*    Function: factory of create operation object
*/

#include "OperationFactory.h"
#include "CAdd.h"
#include "CSub.h"
#include "CMul.h"
#include "CDiv.h"

IOperation * OperationFactory::CreateOperation( char op)
{
    IOperation * operation = 0;

    switch(op)
    {
    case '+' :
        operation = new CAdd();
        break;
    case '-':
        operation = new CSub();
        break;
    case '*':
        operation = new CMul();
        break;
    case '/':
        operation = new CDiv();
        break;
   
    default:
   
        break;
    }

    return operation;
}