简单工厂模式
简单工厂模式:一个工厂,多个产品。
产品需要有一个虚基类。通过传入参数,生成具体产品对象,并利用基类指针指向此对象。通过工厂获取此虚基类指针,通过运行时多肽,调用子类实现。
#include <iostream>
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
std::cout << "Product_A" << std::endl;
}
};
class Product_B : public Product
{
public:
void show()
{
std::cout << "Product_B" << std::endl;
}
};
class Product_C : public Product
{
public:
void show()
{
std::cout << "Product_C" << std::endl;
}
};
class Factory
{
public:
Product* Create(int i)
{
switch (i)
{
case 1:
return new Product_A;
case 2:
return new Product_B;
case 3:
return new Product_C;
default:
break;
}
}
};
int main()
{
Factory *factory = new Factory();
factory->Create(1)->show();
factory->Create(2)->show();
factory->Create(3)->show();
return 0;
}
//打印结果为:
Product_A
Product_B
Product_C
工厂方法模式
工厂方法模式:多个工厂,多个产品,每个产品对应于一个工厂。
此时工厂和产品都是通过虚基类的方式构建。对于简单工厂模式,当要增加一个新产品时候,就需要在工厂类中修改代码,具体表现为多加一个参数,来识别新的产品类型。此时违反了对扩展开放,对修改关闭的原则。基于此,工厂方法模式应运而生。当增加一个新产品时,同时增加一个新工厂。增加新工厂属于扩展,不会修改以前工厂类和产品类的任何代码。可以看过多个独立的简单工厂模式构成了工厂方法模式。
#include "stdafx.h"
#include<iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Factory
{
public:
virtual Product* create() = 0;
};
class Factory_A : public Factory
{
public:
Product* create()
{
return new Product_A;
}
};
class Factory_B : public Factory
{
public:
Product* create()
{
return new Product_B;
}
};
int main()
{
Factory_A* productA = new Factory_A();
Factory_B* productB = new Factory_B();
productA->create()->show();
productB->create()->show();
system("pause");
return 0;
}
抽象工厂模式
抽象工厂模式:多个工厂,多个产品,并且每个产品可以包含多个型号。
此时工厂和产品都是通过虚基类的方式构建。每一个工厂类可以生产同一个产品的多个型号。
#include <iostream>
using namespace std;
//定义抽象类
class product1
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A1 :public product1
{
public:
void show(){ cout << "product A1" << endl; }
};
class product_B1 :public product1
{
public:
void show(){ cout << "product B1" << endl; }
};
//定义抽象类
class product2
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A2 :public product2
{
public:
void show(){ cout << "product A2" << endl; }
};
class product_B2 :public product2
{
public:
void show(){ cout << "product B2" << endl; }
};
class Factory
{
public:
virtual product1 *creat1() = 0;
virtual product2 *creat2() = 0;
};
class Factory1 : public Factory
{
public:
product1 *creata(){ return new product_A1(); }
product2 *creatb(){ return new product_B1(); }
};
class Factory2 : public Factory
{
public:
product1 *creata(){ return new product_A2(); }
product2 *creatb(){ return new product_B2(); }
};
int main()
{
Factory *factory1 = new Factory1();
factory1->creata()->show();
factory1->creatb()->show();
Factory *factory2 = new Factory2();
factory2->creata()->show();
factory2->creatb()->show();
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。