// Copyright https://mangoroom.cn
// License(MIT)
// Author:mango
// This is factory_patter.h
#ifndef FACTORY_PATTERN_H
#define FACTORY_PATTERN_H
#include<iostream>#include<memory>namespacedsignpattern{// 1. ch:创建一个接口 | en: Create an interface
classShape{public:virtualvoidDraw()=0;};// 2. ch:创建实现接口的实体类 | en: Create an entity class that implements an interface
classRectangle:publicShape{public:voidDraw()override{std::cout<<"Inside Rectangle::draw() method."<<std::endl;}};classSquare:publicShape{public:voidDraw()override{std::cout<<"Inside Square::draw() method."<<std::endl;}};classCircle:publicShape{public:voidDraw()override{std::cout<<"Inside Circle::draw() method."<<std::endl;}};// 3. ch:创建一个工厂,生成基于给定信息的实体类的对象 | en: Create an factory to generate objects for entity classes based on a given information
classShapeFactory{public:// ch: 使用 GetShape 函数获取形状类型的对象 | en: To get an object of a shape type using the GetShape function
std::unique_ptr<Shape>GetShape(std::stringshape_type){if(shape_type.empty()){returnnullptr;}elseif(shape_type=="CIRClE"){returnstd::unique_ptr<Shape>(newCircle());}elseif(shape_type=="RECTANGLE"){returnstd::unique_ptr<Shape>(newRectangle());}elseif(shape_type=="SQUARE"){returnstd::unique_ptr<Shape>(newSquare());}else{returnnullptr;}}};// 4. ch: 使用该工厂,通过传递类型信息来获取实体类的对象。 | en: Use the factory to get objects for the entity class by passing type information.
voidFactoryPatternDemo();}#endif // FACTORY_PATTERN_H
// Copyright https://mangoroom.cn
// License(MIT)
// Author:mango
// This is factory_pattern.cpp
#include"factory_pattern.h"usingnamespacedsignpattern;// 4. ch: 使用该工厂,通过传递类型信息来获取实体类的对象。 | en: Use the factory to get objects for the entity class by passing type information.
voiddsignpattern::FactoryPatternDemo(){std::unique_ptr<ShapeFactory>shape_factory(newShapeFactory());// ch: 获取 Circle 的对象,并调用它的 Draw函数 | en: Gets the object of the Circle and calls its Draw method
std::unique_ptr<Shape>cirle(shape_factory->GetShape("CIRClE"));cirle->Draw();// ch: 获取 Rectangle的对象, 并调用它的 Draw函数 | en: Gets the object of the Rectangle and calls its method
std::unique_ptr<Shape>rectangle(shape_factory->GetShape("RECTANGLE"));rectangle->Draw();// ch: 获取 Square的对象, 并调用它的Draw函数 | en: Gets the object of the Square and calls its method
std::unique_ptr<Shape>square(shape_factory->GetShape("SQUARE"));square->Draw();}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;// Copyright https://mangoroom.cn// License(MIT)// Author:mango// DesignPattern: FactoryPatternDemonamespaceDesignPattern{// 1. ch:创建一个接口 | en: Create an interfacepublicinterfaceShape{voidDraw();}// 2. ch:创建实现接口的实体类 | en: Create an entity class that implements an interfacepublicclassRectangle:Shape{publicvoidDraw(){Console.WriteLine("Inside Rectangle::draw() method.");}}publicclassSquare:Shape{publicvoidDraw(){Console.WriteLine("Inside Square ::draw() method.");}}publicclassCircle:Shape{publicvoidDraw(){Console.WriteLine("Inside Circle ::draw() method.");}}// 3. ch:创建一个工厂,生成基于给定信息的实体类的对象 | en: Create an factory to generate objects for entity classes based on a given informationpublicclassShapeFactory{// ch: 使用 GetShape 函数获取形状类型的对象 | en: To get an object of a shape type using the GetShape functionpublicShapeGetShape(stringshapeType){if(shapeType==null){returnnull;}if(shapeType.Equals("CIRCLE")){returnnewCircle();}elseif(shapeType.Equals("RECTANGLE")){returnnewRectangle();}elseif(shapeType.Equals("SQUARE")){returnnewSquare();}returnnull;}}publicclassFactoryPatternDemo{publicstaticvoidFactoryPatternRun(){ShapeFactoryshapeFactory=newShapeFactory();// ch: 获取 Circle 的对象,并调用它的 Draw函数 | en: Gets the object of the Circle and calls its Draw methodShapecircle=shapeFactory.GetShape("CIRCLE");circle.Draw();// ch: 获取 Rectangle的对象, 并调用它的 Draw函数 | en: Gets the object of the Rectangle and calls its methodShaperectangle=shapeFactory.GetShape("RECTANGLE");rectangle.Draw();// ch: 获取 Square的对象, 并调用它的Draw函数 | en: Gets the object of the Square and calls its methodShapesquare=shapeFactory.GetShape("SQUARE");square.Draw();}}}