简单工厂模式
实现代码:
#include <iostream>
using namespace std;
enum ProductType {PA, PB};
class Product
{
public:
Product()
{
cout << "Product ..." << endl;
}
virtual void show() = 0;
};
class ProductA : public Product
{
public:
ProductA()
{
cout << "ProductA ..." << endl;
}
void show()
{
cout << "show A ..." << endl;
}
};
class ProductB : public Product
{
public:
ProductB()
{
cout << "ProductB ..." << endl;
}
void show()
{
cout << "show B ..." << endl;
}
};
class Factory
{
public:
Factory()
{
cout << "Factory ..." << endl;
}
//
Product *createProduct(enum ProductType type)
{
if (type == PA)
{
return new ProductA();
}
else if (type == PB)
{
return new ProductB();
}
else
{
return NULL;
}
}
};
int main()
{
Factory *factory = new Factory();
Product *productA = factory->createProduct(PA);
productA->show();
Product *productB = factory->createProduct(PB);
productB->show();
return 0;
}
UML类图:
工厂方法模式
实现代码:
#include <iostream>
using namespace std;
enum ProductType {PA, PB};
class Product
{
public:
Product()
{
cout << "Product ..." << endl;
}
virtual void show(void) = 0;
};
class ProductA : public Product
{
public:
ProductA()
{
cout << "ProductA ..." << endl;
}
void show(void)
{
cout << "show A ..." << endl;
}
};
class ProductB : public Product
{
public:
ProductB()
{
cout << "ProductB ..." << endl;
}
void show(void)
{
cout << "show B ..." << endl;
}
};
class Factory
{
public:
Factory()
{
cout << "Factory ..." << endl;
}
virtual Product *createProduct(void) = 0;
};
class FactoryA : public Factory
{
public:
FactoryA()
{
cout << "FactoryA ..." << endl;
}
Product *createProduct(void)
{
return new ProductA();
}
};
class FactoryB : public Factory
{
public:
FactoryB()
{
cout << "FactoryB ..." << endl;
}
Product *createProduct(void)
{
return new ProductB();
}
};
int main()
{
Factory *factoryA = new FactoryA();
Product *productA = factoryA->createProduct();
productA->show();
Factory *factoryB = new FactoryB();
Product *productB = factoryB->createProduct();
productB->show();
return 0;
}
UML类图:
抽象工厂模式
实现代码:
#include <iostream>
using namespace std;
enum ProductType {PAX, PAY, PBX, PBY};
class ProductA
{
public:
ProductA()
{
cout << "ProductA ..." << endl;
}
virtual void show(void) = 0;
};
class ProductAX : public ProductA
{
public:
ProductAX()
{
cout << "ProductAX ..." << endl;
}
void show(void)
{
cout << "show AX ..." << endl;
}
};
class ProductAY : public ProductA
{
public:
ProductAY()
{
cout << "ProductAY ..." << endl;
}
void show(void)
{
cout << "show AY ..." << endl;
}
};
class ProductB
{
public:
ProductB()
{
cout << "ProductB ..." << endl;
}
virtual void show(void) = 0;
};
class ProductBX : public ProductB
{
public:
ProductBX()
{
cout << "ProductBX ..." << endl;
}
void show(void)
{
cout << "show BX ..." << endl;
}
};
class ProductBY : public ProductB
{
public:
ProductBY()
{
cout << "ProductBY ..." << endl;
}
void show(void)
{
cout << "show BY ..." << endl;
}
};
class Factory
{
public:
Factory()
{
cout << "Factory ..." << endl;
}
virtual ProductA *createProductA(void) = 0;
virtual ProductB *createProductB(void) = 0;
};
class FactoryX : public Factory
{
public:
FactoryX()
{
cout << "FactoryX ..." << endl;
}
ProductA *createProductA(void)
{
return new ProductAX();
}
ProductB *createProductB(void)
{
return new ProductBX();
}
};
class FactoryY : public Factory
{
public:
FactoryY()
{
cout << "FactoryY ..." << endl;
}
ProductA *createProductA(void)
{
return new ProductAY();
}
ProductB *createProductB(void)
{
return new ProductBY();
}
};
int main()
{
Factory *factoryX = new FactoryX();
ProductA *productAX = factoryX->createProductA();
productAX->show();
ProductB *productBX = factoryX->createProductB();
productBX->show();
Factory *factoryY = new FactoryY();
ProductA *productAY = factoryY->createProductA();
productAY->show();
ProductB *productBY = factoryY->createProductB();
productBY->show();
return 0;
}
UML类图: