factory 简单工厂

#include <iostream>
#include <string>
#include <map>

using namespace std;

class IAnimal  //工厂创建出的接口
{
public:
    virtual int GetNumberOfLegs() const = 0;
    virtual void Speak() = 0;
    virtual void Free() = 0;
};

typedef IAnimal* (__stdcall *CreateAnimalFn)(void);  //创建指针

// IAnimal implementations
class Cat : public IAnimal
{
public:
    int GetNumberOfLegs() const { return 4; }
    void Speak() { cout << "Meow" << endl; }
    void Free() { delete this; }

    static IAnimal * __stdcall Create() { return new Cat(); } //创建函数
};

class Dog : public IAnimal
{
public:
    int GetNumberOfLegs() const { return 4; }
    void Speak() { cout << "Woof" << endl; }
    void Free() { delete this; }

    static IAnimal * __stdcall Create() { return new Dog(); }
};

class Spider : public IAnimal // Yeah it isn’t really an animal…
{
public:
    int GetNumberOfLegs() const { return 8; }
    void Speak() { cout << endl; }
    void Free() { delete this; }

    static IAnimal * __stdcall Create() { return new Spider(); }
};

class Horse : public IAnimal
{
public:
    int GetNumberOfLegs() const { return 4; }
    void Speak() { cout << "A horse is a horse, of course, of course." << endl; }
    void Free() { delete this; }

    static IAnimal * __stdcall Create() { return new Horse(); }
};

// Factory for creating instances of IAnimal
class AnimalFactory
{
private:
    AnimalFactory();
    AnimalFactory(const AnimalFactory &);
    AnimalFactory &operator=(const AnimalFactory &);

    typedef map<string, CreateAnimalFn> FactoryMap;
    FactoryMap m_FactoryMap;
public:
    ~AnimalFactory() { m_FactoryMap.clear(); }

    static AnimalFactory *Get()
    {
        static AnimalFactory instance;
        return &instance;
    }

    void Register(const string &animalName, CreateAnimalFn pfnCreate);
    IAnimal *CreateAnimal(const string &animalName);
};

/* Animal factory constructor.
Register the types of animals here.
*/
AnimalFactory::AnimalFactory()
{
    Register("Horse", &Horse::Create);
    Register("Cat", &Cat::Create);
    Register("Dog", &Dog::Create);
    Register("Spider", &Spider::Create);
}

void AnimalFactory::Register(const string &animalName, CreateAnimalFn pfnCreate)
{
    m_FactoryMap[animalName] = pfnCreate;
}

IAnimal *AnimalFactory::CreateAnimal(const string &animalName)
{
    FactoryMap::iterator it = m_FactoryMap.find(animalName);
    if( it != m_FactoryMap.end() )
        return it->second();  //加()就调用了指向的函数
    return NULL;
}

int main( int argc, char **argv )
{
    IAnimal *pAnimal = NULL;
    string animalName;

    while( pAnimal == NULL )
    {
        cout << "Type the name of an animal or ‘q’ to quit: ";
        cin >> animalName;

        if( animalName == "q" )
            break;

        IAnimal *pAnimal = AnimalFactory::Get()->CreateAnimal(animalName);
        if( pAnimal )
        {
            cout << "Your animal has " << pAnimal->GetNumberOfLegs() << " legs." << endl;
            cout << "Your animal says: ";
            pAnimal->Speak();
        }
        else
        {
            cout << "That animal doesn’t exist in the farm! Choose another!" << endl;
        }
        if( pAnimal )
            pAnimal->Free();
        pAnimal = NULL;
        animalName.clear();
    }
    return 0;
}



#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Operation
{
public:
    virtual ~Operation(){}
    virtual double operate() = 0;
    virtual void free() = 0;
    void setNum1(double value){ m_fNum1 = value; }
    void setNum2(double value){ m_fNum2 = value; }
protected:
    double m_fNum1;
    double m_fNum2;
};

class AddOperation : public Operation
{
public:
    double operate()
    {
        return m_fNum1 + m_fNum2;
    }
    void free()
    {
        delete this;
    }
};

class SubOperation : public Operation
{
public:
    double operate()
    {
        return m_fNum1 - m_fNum2;
    }
    void free()
    {
        delete this;
    }
};

class MultiOperation : public Operation
{
public:
    double operate()
    {
        return m_fNum1 * m_fNum2;
    }
    void free()
    {
        delete this;
    }
};

class DivOperation : public Operation
{
public:
    double operate()
    {
        if (m_fNum2 < 0.000001)
        {
            cout<< "除数不能为零!" <<endl;
            return 0;
        }
        return m_fNum1 / m_fNum2;
    }
    void free()
    {
        delete this;
    }
};
 
class OperationFactory
{
public:
    static OperationFactory *Get()
    {
        static OperationFactory oOperFac;
        return &oOperFac;
    }
    Operation *CreateOperate(char c)
    {
        switch(c)
        {
        case '+':
            return new AddOperation();
        case '-':
            return new SubOperation();
        case '*':
            return new MultiOperation();
        case '/':
            return new DivOperation();
        default:
            break;
        }
    }
private:
    OperationFactory(){}
    OperationFactory(const OperationFactory &);
    OperationFactory & operator=(const OperationFactory&);
};

int main()
{
    OperationFactory *pFac = OperationFactory::Get();
    Operation *oper = pFac->CreateOperate('+');
    oper->setNum1(100);
    oper->setNum2(200);
    cout<<oper->operate()<<endl;
    oper->free();
    oper = pFac->CreateOperate('*');
    oper->setNum1(18);
    oper->setNum2(20);
    cout<<oper->operate()<<endl;
    oper->free();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值