AbstractFactory模式

 

//Product.h

#ifndef _PRODUCT_H

#define _PRODUCT_H

 

class AbstractProductA

{

public:

        ~AbstractProductA();

protected:

        AbstractProductA();

};

 

class AbstractProductB

{

public:

        ~AbstractProductB();

protected:

        AbstractProductB();

};

 

class ProdcutA1:AbstractProductA

{

public:

        ProdcutA1();

        ~ProductA1();

};

 

class ProductA2:AbstractProductA

{

public:

        ProductA2();

        ~ProductA2();

};

 

class ProductB1:AbstractProductB

{

public:

        ProductB1();

        ~ProductB1();

};

 

class ProductB2:AbstractProductB

{

public:

        ProductB2();

        ~ProductB2();

};

#endif

 

//Product.cpp

#include "Product.h"

#include <iostream>

using namespace std;

 

AbstractFactoryA::AbstractFactoryA()

{}

AbstractFactoryA::~AbstractFactoryA()

{}

AbstractFactoryB::AbstractFactoryB()

{}

AbstractFactoryB::~AbstractFactoryB()

{}

ProdcutA1::ProductA1()

{

        cout << "ProductA1.." << endl;

}

ProductA1::~ProductA1()

{}

ProdcutA2::ProductA2()

{

        cout << "ProductA2.." << endl;

}

ProductA2::~ProductA2()

{}

ProdcutB1::ProductB1()

{

        cout << "ProductB1.." << endl;

}

ProductB1::~ProductB1()

{}

ProdcutB2::ProductB2()

{

        cout << "ProductB2.." << endl;

}

ProductB2::~ProductB2()

{}

 

//AbstractFactory.h

#ifndef _FACTORY_H

#define _FACTORY_H

 

class AbstractProductA;

class AbstractProductB;

 

class AbstractFactory

{

public:

        virtual ~AbstractFactory();

        virtual AbstractProductA * CreateProductA() = 0;

        virtual AbstractProductB * CreateProductB() = 0;

protected:

        AbstractFactory();

};

 

class ConcreteFactory1

{

public:

        ConcreteFactory1();

        ~ConcreteFactory1();

        AbstractProductA * CreateProductA();

        AbstractProductB * CreateProductB();

};

 

class ConcreteFactory2

{

public:

        ConcreteFactory2();

        ~ConcreteFactory2();

        AbstractProductA * CreateProductA();

        AbstractProductB * CreateProductB();

};

#endif

 

//AbstractFactory.cpp

#include "AbstractFactory.h"

#include "Product.h"

#include <iostream>

using namespace std;

 

class AbstractFactory::AbstractFactory()

{}

class AbstractFactory::~AbstractFactory()

{}

class ConcreteFactory1::ConcreteFactory1()

{

}

class ConcreteFactory1::~ConcreteFactory1()

{}

AbstractProductA * class ConcreteFactory1::CreateProductA()

{

        return new ProductA1();

}

AbstractProductB * class ConcreteFactory1::CreateProductB()

{

        return new ProductB1();

}

class ConcreteFactory2::ConcreteFactory2()

{

}

class ConcreteFactory2::~ConcreteFactory2()

{}

AbstractProductA * class ConcreteFactory2::CreateProductA()

{

        return new ProductA2();

}

AbstractProductB * class ConcreteFactory2::CreateProductB()

{

        return new ProductB2();

}

 

//main.cpp

#include "AbstractFactory.h"

#include <iostream>

using namespace std;

 

void main(int argc, char * argv[])

{

        AbstractFactoryA1 *cf1 = new AbstractFactoryA1();

        cf1 -> CreateProductA();

        cf1 -> CreateProductB();

        AbstractFactoryA2 *cf2 = new AbstractFactoryA2();

        cf2 -> CreateProductA();

        cf2 -> CreateProductB();

 

        return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值