抽象工厂模式为创建一组对象提供了一种解决方案。与工厂方法模式相比,抽象工厂模式中的具体工厂不只是创建一种产品,它负责创建一族产品。抽象工厂模式定义如下:
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,它是一种对象创建型模式。
抽象工厂创建
class Shape //接口
{
public:
Shape()
{
cout << "Shape" << endl;
}
virtual ~Shape()
{
cout << "~Shape" << endl;
}
virtual void draw() = 0;
virtual void erase() = 0;
};
class Circle :public Shape
{
public:
Circle()
{
cout << "Circle" << endl;
}
virtual ~Circle()
{
cout << "~Circle" << endl;
}
void draw()
{
cout << "circle::drap" << endl;
}
void erase()
{
cout << "circle::erase" << endl;
}
};
class Square :public Shape
{
public:
Square()
{
cout << "Square" << endl;
}
virtual ~Square()
{
cout << "~Square" << endl;
}
void draw()
{
cout << "square::drap" << endl;
}
void erase()
{
cout << "square::erase" << endl;
}
};
class ShapeFactory
{
public:
ShapeFactory()
{
cout << "ShapeFactory" << endl;
}
virtual ~ShapeFactory()
{
cout << "~ShapeFactory" << endl;
}
virtual Shape* create() = 0;
public:
static std::map<string, ShapeFactory*> factories;
// key value
// ShapeName
static Shape* createShape(const string& id)
{
if (factories.find(id) != factories.end())
{
return factories[id]->create();
}
else
{
return nullptr;
}
}
};
std::map<string, ShapeFactory*> ShapeFactory::factories;
class CircleFactory :public ShapeFactory
{
public:
CircleFactory()
{
cout << "CircleFactory" << endl;
}
~CircleFactory()
{
cout << "~CircleFactory" << endl;
}
virtual Shape* create()
{
return new Circle();
}
};
class SquareFactory :public ShapeFactory
{
public:
SquareFactory()
{
cout << "SquareFactory" << endl;
}
~SquareFactory()
{
cout << "~SquareFactory" << endl;
}
virtual Shape* create()
{
return new Square();
}
};
class ShapeFactoryInitializer
{
public:
static ShapeFactoryInitializer si;
ShapeFactoryInitializer()
{
ShapeFactory::factories["Circle"] = new CircleFactory();
ShapeFactory::factories["Square"] = new SquareFactory();
}
};
//静态成员初始化
ShapeFactoryInitializer ShapeFactoryInitializer::si;
int main()
{
Shape* shape = ShapeFactory::createShape("Circle");//创建类型
shape->draw();
shape->erase();
shape = ShapeFactory::createShape("Square");
shape->draw();
shape->erase();
return 0;
}
智能指针管理工厂
class Shape //接口
{
public:
Shape()
{
cout << "Shape" << endl;
}
virtual ~Shape()
{
cout << "~Shape" << endl;
}
virtual void draw() = 0;
virtual void erase() = 0;
};
class Circle :public Shape
{
public:
Circle()
{
cout << "Circle" << endl;
}
virtual ~Circle()
{
cout << "~Circle" << endl;
}
void draw()
{
cout << "circle::drap" << endl;
}
void erase()
{
cout << "circle::erase" << endl;
}
};
class Square :public Shape
{
public:
Square()
{
cout << "Square" << endl;
}
virtual ~Square()
{
cout << "~Square" << endl;
}
void draw()
{
cout << "square::drap" << endl;
}
void erase()
{
cout << "square::erase" << endl;
}
};
class ShapeFactory
{
public:
ShapeFactory()
{
cout << "ShapeFactory" << endl;
}
virtual ~ShapeFactory()
{
cout << "~ShapeFactory" << endl;
}
virtual std::shared_ptr<Shape> create() = 0;
public:
static std::map<string, std::shared_ptr<ShapeFactory>> factories;
// key value
// ShapeName
static std::shared_ptr<Shape> createShape(const string& id)
{
if (factories.find(id) != factories.end())
{
return factories[id]->create();
}
else
{
return nullptr;
}
}
};
std::map<string, std::shared_ptr<ShapeFactory>> ShapeFactory::factories;
class CircleFactory :public ShapeFactory
{
public:
CircleFactory()
{
cout << "CircleFactory" << endl;
}
~CircleFactory()
{
cout << "~CircleFactory" << endl;
}
virtual std::shared_ptr<Shape> create()
{
return std::make_shared<Circle>();
}
};
class SquareFactory :public ShapeFactory
{
public:
SquareFactory()
{
cout << "SquareFactory" << endl;
}
~SquareFactory()
{
cout << "~SquareFactory" << endl;
}
virtual std::shared_ptr<Shape> create()
{
return std::make_shared<Square>();
}
};
class ShapeFactoryInitializer
{
public:
static ShapeFactoryInitializer si;
ShapeFactoryInitializer()
{
ShapeFactory::factories["Circle"] = std::make_shared<CircleFactory>();
ShapeFactory::factories["Square"] = std::make_shared<SquareFactory>();
}
};
//静态成员初始化
ShapeFactoryInitializer ShapeFactoryInitializer::si;
int main()
{
std::shared_ptr<Shape> shape = ShapeFactory::createShape("Circle");//创建类型
shape->draw();
shape->erase();
shape = ShapeFactory::createShape("Square");
shape->draw();
shape->erase();
return 0;
}