C++ 抽象工厂


抽象工厂模式为创建一组对象提供了一种解决方案。与工厂方法模式相比,抽象工厂模式中的具体工厂不只是创建一种产品,它负责创建一族产品。抽象工厂模式定义如下:

抽象工厂模式(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;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值