工厂方法模式
定义:定义一个创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。
UML类图:
C++实现:
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void Draw() = 0;
virtual ~Shape() = default;
};
class Circle :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Circle!" << endl;
}
virtual ~Circle() {}
};
class Sphere :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Sphere!" << endl;
}
virtual ~Sphere() {}
};
class Cube :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Cube!" << endl;
}
virtual ~Cube() {}
};
class ShapeFactory
{
public:
Shape* getShape(string shapeType)
{
if (shapeType == "")
{
return nullptr;
}
if (shapeType == "Circle")
{
return new Circle();
}
if (shapeType == "Sphere")
{
return new Sphere();
}
if (shapeType == "Cube")
{
return new Cube();
}
return nullptr;
}
~ShapeFactory() {};
};
int main()
{
ShapeFactory* shapeFactory = new ShapeFactory();
Shape* shape1 = shapeFactory->getShape("Circle");
shape1->Draw();
Shape* shape2 = shapeFactory->getShape("Sphere");
shape2->Draw();
Shape* shape3 = shapeFactory->getShape("Cube");
shape3->Draw();
return 0;
}
C#实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;
namespace ConsoleApp1
{
public interface Shape
{
void Draw();
}
public class Circle:Shape
{
public void Draw()
{
WriteLine("Draw Circle!");
}
}
public class Sphere : Shape
{
public void Draw()
{
WriteLine("Draw Sphere!");
}
}
public class Cube : Shape
{
public void Draw()
{
WriteLine("Draw Cube!");
}
}
public class ShapeFactory
{
public Shape getShape(String shapeType)
{
if (shapeType == null)
{
return null;
}
if(shapeType.Equals("Circle"))
{
return new Circle();
}
if(shapeType.Equals("Sphere"))
{
return new Sphere();
}
if(shapeType.Equals("Cube"))
{
return new Cube();
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape("Circle");
shape1.Draw();
Shape shape2 = shapeFactory.getShape("Sphere");
shape2.Draw();
Shape shape3 = shapeFactory.getShape("Cube");
shape3.Draw();
}
}
}
抽象工厂方模式
定义:为创建一组相关或者相互依赖的对象提供一个接口,而无需指定他们的具体类。(又称为工厂的工厂)
UML类图:
C++实现:
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void Draw() = 0;
virtual ~Shape() = default;
};
class Color
{
public:
virtual void Draw() = 0;
virtual ~Color() = default;
};
class AbstractFactory
{
virtual Shape* getShape(string shapeType) { return nullptr; }
virtual Color* getColor(string colorType) { return nullptr; }
public:
virtual ~AbstractFactory() = default;
};
class Circle :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Circle!" << endl;
}
virtual ~Circle(){}
};
class Sphere :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Sphere!" << endl;
}
virtual ~Sphere() {}
};
class Cube :public Shape
{
public:
// 通过 Shape 继承
virtual void Draw() override
{
cout << "Draw Cube!" << endl;
}
virtual ~Cube() {}
};
class ShapeFactory :public AbstractFactory
{
public:
virtual Shape* getShape(string shapeType) override
{
if (shapeType == "")
{
return nullptr;
}
if (shapeType == "Circle")
{
return new Circle();
}
if (shapeType == "Sphere")
{
return new Sphere();
}
if (shapeType == "Cube")
{
return new Cube();
}
return nullptr;
}
virtual ~ShapeFactory() {};
};
class Red :public Color
{
public:
virtual void Draw()
{
cout << "Fill Red!!!" << endl;
}
virtual ~Red() {}
};
class Green :public Color
{
public:
virtual void Draw()
{
cout << "Fill Green!!!" << endl;
}
virtual ~Green() {}
};
class Blue :public Color
{
public:
virtual void Draw()
{
cout << "Fill Blue!!!" << endl;
}
virtual ~Blue() {}
};
class ColorFactory :public AbstractFactory
{
public:
virtual Color* getColor(string colorType) override
{
if (colorType == "")
return nullptr;
if (colorType == "Red")
return new Red();
if (colorType == "Green")
return new Green();
if (colorType == "Blue")
return new Blue();
return nullptr;
}
virtual ~ColorFactory() {}
};
class FactoryProducer
{
public:
static AbstractFactory* getFactory(string abstractType)
{
if (abstractType == "")
return nullptr;
if (abstractType == "SHAPE")
return new ShapeFactory();
if (abstractType == "COLOR")
return new ColorFactory();
return nullptr;
}
~FactoryProducer() {}
};
int main()
{
FactoryProducer* factoryProducer = new FactoryProducer();
ShapeFactory* shapeFactory = dynamic_cast<ShapeFactory*>(factoryProducer->getFactory("SHAPE"));
Shape* shape1 = shapeFactory->getShape("Circle");
shape1->Draw();
ColorFactory* colorFactory = dynamic_cast<ColorFactory*>(factoryProducer->getFactory("COLOR"));
Color* color1 = colorFactory->getColor("Red"); color1->Draw();
return 0;
}
C#实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static System.Console;
namespace ConsoleApp1
{
public interface Shape
{
void Draw();
}
public interface Color
{
void Draw();
}
public abstract class AbstractFactory
{
public abstract Shape getShape(String shapeType);
public abstract Color getColor(String colorType);
}
public class Circle:Shape
{
public void Draw()
{
WriteLine("Draw Circle!");
}
}
public class Sphere : Shape
{
public void Draw()
{
WriteLine("Draw Sphere!");
}
}
public class Cube : Shape
{
public void Draw()
{
WriteLine("Draw Cube!");
}
}
public class ShapeFactory:AbstractFactory
{
public override Shape getShape(String shapeType)
{
if (shapeType == null)
{
return null;
}
if(shapeType.Equals("Circle"))
{
return new Circle();
}
if(shapeType.Equals("Sphere"))
{
return new Sphere();
}
if(shapeType.Equals("Cube"))
{
return new Cube();
}
return null;
}
public override Color getColor(string colorType)
{
return null;
}
}
public class Red : Color
{
public void Draw()
{
WriteLine("Fill Red!!!");
}
}
public class Green : Color
{
public void Draw()
{
WriteLine("Fill Green!!!");
}
}
public class Blue : Color
{
public void Draw()
{
WriteLine("Fill Blue!!!");
}
}
class ColorFactory:AbstractFactory
{
public override Shape getShape(String shapeType)
{
return null;
}
public override Color getColor(String colorType)
{
if (colorType == null)
return null;
if(colorType.Equals("Red"))
{
return new Red();
}
if(colorType.Equals("Green"))
{
return new Green();
}
if(colorType.Equals("Blue"))
{
return new Blue();
}
return null;
}
}
public class FactoryProducer
{
public static AbstractFactory getAbstractFactory(String abstractType)
{
if (abstractType == "")
return null;
if(abstractType.Equals("SHAPE"))
{
return new ShapeFactory();
}
if(abstractType.Equals("COLOR"))
{
return new ColorFactory();
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
AbstractFactory shapeFactory = FactoryProducer.getAbstractFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("Circle");
shape1.Draw();
AbstractFactory colorFactory = FactoryProducer.getAbstractFactory("COLOR");
Color color1 = colorFactory.getColor("Red");
color1.Draw();
}
}
}