Abstruct Pattern
AbstructProduct.h
#ifndef _ABSTRUCTPRODUCT_H_
#define _ABSTRUCTPRODUCT_H_
class abstructproductA {
public:
virtual ~abstructproductA();
protected:
abstructproductA();
private:
};
class abstructproductB {
public:
virtual ~abstructproductB();
protected:
abstructproductB();
private:
};
class productA1 : public abstructproductA {
public:
productA1();
~productA1();
protected:
private:
};
class productA2 : public abstructproductA {
public:
productA2();
~productA2();
protected:
private:
};
class productB1 : public abstructproductB {
public:
productB1();
~productB1();
protected:
private:
};
class productB2 : public abstructproductB {
public:
productB2();
~productB2();
protected:
private:
};
#endif
#include"abstructProduct.h"
#include<iostream>
using namespace std;
abstructproductA::abstructproductA() {
}
abstructproductA::~abstructproductA() {
}
abstructproductB::abstructproductB() {
}
abstructproductB::~abstructproductB() {
}
productA1::productA1() {
cout<<"productA1..."<<endl;
}
productA1::~productA1() {
}
productA2::productA2() {
cout<<"productA2..."<<endl;
}
productA2::~productA2() {
}
productB1::productB1() {
cout<<"productB1..."<<endl;
}
productB1::~productB1() {
}
productB2::productB2() {
cout<<"productB2..."<<endl;
}
productB2::~productB2() {
}
AbstructFactory.h
#ifndef _ABSTRUCTFACTORY_H_
#define _ABSTRUCTFACTORY_H_
#include"abstructProduct.h"
class abstructFactory{
public:
virtual ~abstructFactory();
virtual abstructproductA* createProductA() = 0;
virtual abstructproductB* createProductB() = 0;
protected:
abstructFactory(/* args */);
private:
/* data */
};
class concreteFactory1 : public abstructFactory {
public:
concreteFactory1();
~concreteFactory1();
abstructproductA* createProductA();
abstructproductB* createProductB();
protected:
private:
};
class concreteFactory2 : public abstructFactory {
public:
concreteFactory2();
~concreteFactory2();
abstructproductA* createProductA();
abstructproductB* createProductB();
protected:
private:
};
#endif
#include"abstructFactory.h"
#include<iostream>
using namespace std;
abstructFactory::abstructFactory(/* args */)
{
}
abstructFactory::~abstructFactory()
{
}
concreteFactory1::concreteFactory1() {
}
concreteFactory1::~concreteFactory1() {
}
abstructproductA* concreteFactory1::createProductA() {
return new productA1();
}
abstructproductB* concreteFactory1::createProductB() {
return new productB1();
}
concreteFactory2::concreteFactory2() {
}
concreteFactory2::~concreteFactory2() {
}
abstructproductA* concreteFactory2::createProductA() {
return new productA2();
}
abstructproductB* concreteFactory2::createProductB() {
return new productB2();
}
main.cpp
#include"abstructProduct.h"
#include"abstructFactory.h"
#include<iostream>
using namespace std;
int main()
{
abstructFactory *cf1 = new concreteFactory1();
abstructFactory *cf2 = new concreteFactory2();
cf1->createProductA();
cf1->createProductB();
cf2->createProductA();
cf2->createProductB();
return 0;
}
运行结果:
极大简化了代码,假设在打游戏,怪物分为初级、中级、高级,那么一个工厂就相当于一级(其中随机生成不同种类怪物(也就是上面的productA,productB))。