単純工場/工場メソッド/抽象工場
たんじゅんこうじょう
プラントメソッド
ちゅうしょうこうじょう
// 1 ( ): 。
// :1. , 。2. , 。
// : , , ( )。
class AbstractProdect {//
public:
virtual void Play() = 0;
};
class AbstractProdectA:public AbstractProdect{
public:
AbstractProdectA() {
cout << "AbstractProdectA()" << endl;
}
~AbstractProdectA() {
cout << "~AbstractProdectA()" << endl;
}
void Play() { cout << "I am AbstractProdectA" << endl; }
};
class AbstractProdectB :public AbstractProdect {
public:
AbstractProdectB() {
cout << "AbstractProdectB()" << endl;
}
~AbstractProdectB() {
cout << "~AbstractProdectB()" << endl;
}
void Play() { cout << "I am AbstractProdectB" << endl; }
};
class AbstractFactory {//
public:
AbstractFactory() {};
~AbstractFactory() {};
virtual AbstractProdect* CreateProdect(int) = 0;
};
class Factory1 :public AbstractFactory {
public:
Factory1() { cout << "Factory1()" << endl; }
~Factory1() { cout << "~Factory1()" << endl; }
AbstractProdect* CreateProdect(int chose)
{
AbstractProdect* pd;//
switch (chose)
{
case 1:pd = new AbstractProdectA(); break;
case 2:pd = new AbstractProdectB(); break;
default:pd = NULL; break;
}
return pd;
}
};
プラントメソッド
/*
: 。
、 、 。 。
、 ( )。 、 。 。
:1. , 。2. , 。
, , , 。 。
*/
class Product {// ( )
public:
Product() {}
~Product() {}
virtual void ProBehave() = 0;
};
class productA :public Product {
public:
productA() {}
~productA() {}
void ProBehave() { cout << "productA " << endl; }
};
class productB :public Product {
public:
productB() {}
~productB() {}
void ProBehave() { cout << "productB " << endl; }
};
class productC :public Product {
public:
productC() {}
~productC() {}
void ProBehave() { cout << "productC " << endl; }
};
class Factory {// ( )
public:
Factory() {}
~Factory() {}
virtual Product* CreateProduct() = 0;//
};
class factoryA :public Factory {
public:
factoryA() {}
~factoryA() {}
Product* CreateProduct() { return new productA(); }
};
class factoryB :public Factory {
public:
factoryB() {}
~factoryB() {}
Product* CreateProduct() { return new productB(); }
};
class factoryC :public Factory {
public:
factoryC() {}
~factoryC() {}
Product* CreateProduct() { return new productC(); }
};
ちゅうしょうこうじょう
/*
: + ( )
。
*/
class ProductA
{
public:
ProductA()
{}
virtual ~ProductA()
{}
public:
virtual void operation() = 0;
};
class ProductA1 : public ProductA
{
public:
ProductA1()
{}
virtual ~ProductA1()
{}
public:
virtual void operation()
{
cout << "ProductA1" << endl;
}
};
class ProductA2 : public ProductA
{
public:
ProductA2()
{}
virtual ~ProductA2()
{}
public:
virtual void operation()
{
cout << "ProductA2" << endl;
}
};
class ProductB
{
public:
ProductB()
{}
virtual ~ProductB()
{}
public:
virtual void operation() = 0;
};
class ProductB1 : public ProductB
{
public:
ProductB1()
{}
virtual ~ProductB1()
{}
public:
virtual void operation()
{
cout << "ProductB1" << endl;
}
};
class ProductB2 : public ProductB
{
public:
ProductB2()
{}
virtual ~ProductB2()
{}
public:
virtual void operation()
{
cout << "ProductB2" << endl;
}
};
class AbstractFactory
{
public:
AbstractFactory()
{}
virtual ~AbstractFactory()
{}
public:
virtual ProductA* createProductA() = 0;
virtual ProductB* createProductB() = 0;
};
class Factory1 : public AbstractFactory
{
public:
Factory1()
{}
~Factory1()
{}
public:
ProductA* createProductA()
{
return new ProductA1();
}
ProductB* createProductB()
{
return new ProductB1();
}
};
class Factory2 : public AbstractFactory
{
public:
Factory2()
{}
~Factory2()
{}
public:
ProductA* createProductA()
{
return new ProductA2();
}
ProductB* createProductB()
{
return new ProductB2();
}
};