工厂模式
本文以下内容为芒果参考菜鸟教程-工厂模式,实现了工厂模式的c++版本以及c#版本,关于工厂模式更多的介绍可以前往菜鸟教程学习。
注:图片来源菜鸟教程
c++实现
// 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>
namespace dsignpattern
{
// 1. ch:创建一个接口 | en: Create an interface
class Shape
{
public:
virtual void Draw() = 0;
};
// 2. ch:创建实现接口的实体类 | en: Create an entity class that implements an interface
class Rectangle : public Shape
{
public:
void Draw() override
{
std::cout << "Inside Rectangle::draw() method." << std::endl;
}
};
class Square : public Shape
{
public:
void Draw() override
{
std::cout << "Inside Square::draw() method." << std::endl;
}
};
class Circle : public Shape
{
public:
void Draw() 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
class ShapeFactory
{
public:
// ch: 使用 GetShape 函数获取形状类型的对象 | en: To get an object of a shape type using the GetShape function
std::unique_ptr<Shape> GetShape(std::string shape_type)
{
if (shape_type.empty())
{
return nullptr;
}
else if (shape_type == "CIRClE")
{
return std::unique_ptr<Shape>(new Circle());
}
else if (shape_type == "RECTANGLE")
{
return std::unique_ptr<Shape>(new Rectangle());
}
else if (shape_type == "SQUARE")
{
return std::unique_ptr<Shape>(new Square());
}
else
{
return nullptr;
}
}
};
// 4. ch: 使用该工厂,通过传递类型信息来获取实体类的对象。 | en: Use the factory to get objects for the entity class by passing type information.
void FactoryPatternDemo();
}
#endif // FACTORY_PATTERN_H
// Copyright https://mangoroom.cn
// License(MIT)
// Author:mango
// This is factory_pattern.cpp
#include"factory_pattern.h"
using namespace dsignpattern;
// 4. ch: 使用该工厂,通过传递类型信息来获取实体类的对象。 | en: Use the factory to get objects for the entity class by passing type information.
void dsignpattern::FactoryPatternDemo()
{
std::unique_ptr<ShapeFactory> shape_factory(new ShapeFactory());
// 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();
}
运行结果
Inside Circle ::draw() method.
Inside Rectangle::draw() method.
Inside Square ::draw() method.
c#实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Copyright https://mangoroom.cn
// License(MIT)
// Author:mango
// DesignPattern: FactoryPatternDemo
namespace DesignPattern
{
// 1. ch:创建一个接口 | en: Create an interface
public interface Shape
{
void Draw();
}
// 2. ch:创建实现接口的实体类 | en: Create an entity class that implements an interface
public class Rectangle : Shape
{
public void Draw()
{
Console.WriteLine("Inside Rectangle::draw() method.");
}
}
public class Square : Shape
{
public void Draw()
{
Console.WriteLine("Inside Square ::draw() method.");
}
}
public class Circle : Shape
{
public void Draw()
{
Console.WriteLine("Inside Circle ::draw() method.");
}
}
// 3. ch:创建一个工厂,生成基于给定信息的实体类的对象 | en: Create an factory to generate objects for entity classes based on a given information
public class ShapeFactory
{
// ch: 使用 GetShape 函数获取形状类型的对象 | en: To get an object of a shape type using the GetShape function
public Shape GetShape(string shapeType)
{
if(shapeType == null)
{
return null;
}
if (shapeType.Equals("CIRCLE"))
{
return new Circle();
}
else if (shapeType.Equals("RECTANGLE"))
{
return new Rectangle();
}
else if (shapeType.Equals("SQUARE"))
{
return new Square();
}
return null;
}
}
public class FactoryPatternDemo
{
public static void FactoryPatternRun()
{
ShapeFactory shapeFactory = new ShapeFactory();
// ch: 获取 Circle 的对象,并调用它的 Draw函数 | en: Gets the object of the Circle and calls its Draw method
Shape circle = shapeFactory.GetShape("CIRCLE");
circle.Draw();
// ch: 获取 Rectangle的对象, 并调用它的 Draw函数 | en: Gets the object of the Rectangle and calls its method
Shape rectangle = shapeFactory.GetShape("RECTANGLE");
rectangle.Draw();
// ch: 获取 Square的对象, 并调用它的Draw函数 | en: Gets the object of the Square and calls its method
Shape square = shapeFactory.GetShape("SQUARE");
square.Draw();
}
}
}
运行结果
Inside Circle ::draw() method.
Inside Rectangle::draw() method.
Inside Square ::draw() method.