Abstract Factory模式示例(使用Locki类库)

类别:编程语言 点击:0 评论:0 推荐:

本文试图借助Loki类库中的Singleton组件和AbstractFactory组件来应用Abstract Factory模式。

#include <iostream>
#include "AbstractFactory.h"
#include "Singleton.h"
using namespace std;
using namespace Loki;

// Abstract products A and B
struct A {};
struct B {};
// Concrete products A1 and B1
struct A1: public A
{
    A1() { cout << "ctor A1" << endl; }
};
struct A2: public A
{
    A2() { cout << "ctor A2" << endl; }
};
// Concrete products A2 and B2
struct B1: public B
{
    B1() { cout << "ctor B1" << endl; }
};
struct B2: public B
{
    B2() { cout << "ctor B2" << endl; }
};

// Traditional method begins
class AbstractFactory1
{
public:
    virtual A* CreateA() = 0;
    virtual B* CreateB() = 0;
};

class ConcreteFactory1 : public AbstractFactory1
{
public:
    virtual A* CreateA() { return new A1(); }
    virtual B* CreateB() { return new B1(); }

    static ConcreteFactory1& Instance() {
        static ConcreteFactory1 instance;
        return instance;
    }
protected:
    ConcreteFactory1() {}
private:
    ConcreteFactory1(const ConcreteFactory1&);
    ConcreteFactory1& operator=(const ConcreteFactory1&);
};

class ConcreteFactory2 : public AbstractFactory1
{
public:
    virtual A* CreateA() { return new A2(); }
    virtual B* CreateB() { return new B2(); }

    static ConcreteFactory2& Instance() {
        static ConcreteFactory2 instance;
        return instance;
    }
protected:
    ConcreteFactory2() {}
private:
    ConcreteFactory2(const ConcreteFactory2&);
    ConcreteFactory2& operator=(const ConcreteFactory2&);
};
// Traditional method ends

// Loki method begins
typedef AbstractFactory<TYPELIST_2(A, B)> AF;
typedef SingletonHolder<ConcreteFactory<AF, OpNewFactoryUnit, TYPELIST_2(A1, B1)> > SCF1;
typedef SingletonHolder<ConcreteFactory<AF, OpNewFactoryUnit, TYPELIST_2(A2, B2)> > SCF2;
// Loki method ends

void main()
{
    cout << "Traditional method:" << endl;
    // Create all concrete products using traditional method
    A *pTA1 = ConcreteFactory1::Instance().CreateA();
    B *pTB1 = ConcreteFactory1::Instance().CreateB();
    A *pTA2 = ConcreteFactory2::Instance().CreateA();
    B *pTB2 = ConcreteFactory2::Instance().CreateB();
    cout << "Loki method:" << endl;
    // Create all concrete products using Loki method
    A *pLA1 = SCF1::Instance().Create<A>();
    B *pLB1 = SCF1::Instance().Create<B>();
    A *pLA2 = SCF2::Instance().Create<A>();
    B *pLB2 = SCF2::Instance().Create<B>();
}

本文地址:http://com.8s8s.com/it/it24439.htm