工厂模式属于创建型模式
基本原理:使用一个工厂类统一生产各种产品。
主要流程:
1.创建产品的基类,便于统一返回创建的产品
2.创建各种产品,继承基类,注意多态
3.创建工厂类,对每种产品进行区分创建。
4.在使用时要先实例化工厂类,在调用期内生产的方法。
优点:
1.一个调用者想创建一个对象,只要知道其名称就可以了。
2.扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。
3.屏蔽产品的具体实现,调用者只关心产品的接口。
缺点:
1.每新增一种类型的产品都要增加一个具体的产品类和工厂类。
#include <iostream>
#include <string>
using namespace std;
//创建一个种类
class Shape
{
public:
//
virtual void draw() = 0;
};
class A : public Shape
{
public:
void draw()
{
cout<<"生产了一个A"<<endl;
}
};
class B : public Shape
{
public:
void draw()
{
cout<<"生产了一个B"<<endl;
}
};
class C : public Shape
{
public:
void draw()
{
cout<<"生产了一个C"<<endl;
}
};
//创建一个工厂
class ShapeFactory
{
public:
Shape * getShape(string shapeType)
{
if(shapeType == "A")
{
return new A();
}
else if(shapeType == "B")
{
return new B();
}
else if(shapeType == "C")
{
return new C();
}
else
{
return NULL;
}
}
};
int main()
{
ShapeFactory *factory = new ShapeFactory();
Shape *shapeA = factory->getShape("A");
shapeA->draw();
return 0;
}
抽象工厂模式其实是工厂模式的封装,就是在工厂模式上边在包装一层,建立一个工厂的生成器。使用时先生成工厂,再使用该工厂的功能。
#include <iostream>
#include <string>
using namespace std;
//创建一个种类
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw()
{
cout<<" Rectangle::draw "<<endl;
}
};
class Circle : public Shape
{
public:
void draw()
{
cout<<" Circle::draw "<<endl;
}
};
class Square : public Shape
{
public:
void draw()
{
cout<<" Square::draw "<<endl;
}
};
//创建另一种种类
class Color
{
public:
virtual void fill() = 0;
};
class Red : public Color
{
public:
void fill()
{
cout<<" Red::fill "<<endl;
}
};
class Green : public Color
{
public:
void fill()
{
cout<<" Green::fill "<<endl;
}
};
class Blue : public Color
{
public:
void fill()
{
cout<<" Blue::fill "<<endl;
}
};
//创建抽象工厂基类
class AbstractFactory
{
public:
virtual Color * getColor(string color) = 0;
virtual Shape * getShape(string shape) = 0;
};
//创建种类工厂,扩展AbstractFactory的工厂类
class ShapeFactory : public AbstractFactory
{
public:
Shape * getShape(string shapeType)
{
if(shapeType == "Rectangle")
{
return new Rectangle();
}
else if(shapeType == "Square")
{
return new Square();
}
else if(shapeType == "Circle")
{
return new Circle();
}
else
{
return NULL;
}
}
Color * getColor(string color)
{
return NULL;
}
};
class ColorFactory : public AbstractFactory
{
public:
Shape * getShape(string shapeType)
{
return NULL;
}
Color * getColor(string colorType)
{
if(colorType == "Red")
{
return new Red();
}
else if(colorType == "Green")
{
return new Green();
}
else if(colorType == "Blue")
{
return new Blue();
}
else
{
return NULL;
}
}
};
//创建工厂生成器,用来生成工厂
class FactoryProducer
{
public:
AbstractFactory * getFactory(string factoryType)
{
if(factoryType == "shape")
{
return new ShapeFactory();
}
else if (factoryType == "color")
{
return new ColorFactory();
}
else
{
return NULL;
}
}
};
int main()
{
FactoryProducer * factoryProducer = new FactoryProducer();
AbstractFactory * shapeFactory = factoryProducer->getFactory("shape");
Shape * shape = shapeFactory->getShape("Circle");
shape->draw();
return 0;
}