工厂模式之一 - 简单工厂(Simple Factory)
简单工厂(Simple Factory)模式又可以叫做:静态工厂方法(Static Factory Method),它是类的创建模式。
场景:画图程序,画圆,矩形,三角,等等。
方案:需要客户端由一个指定的工厂方法来创建各种画图类。客户端不需要知道各个画图类的名称,
只需要给工厂方法传入画图类的参数,如:circle, square, triangle, etc,这些参数是固定的。
如果画图类改变,甚至类名改变,我们只需要改变工厂方法即可。客户端可以保持不变。
因为,在实际应用中,客户端可能会有成百上千,如果客户端知道的具体细节越多,改动就越大。
首先,我们可以定义一个画图的接口,如下:
namespace designpattern.cs.SimpleFactory
{
public interface IShape
{
void draw();
void erase();
}
}
{
public interface IShape
{
void draw();
void erase();
}
}
第二步,我们可以建立各个画图类,如下:
a) 圆
using System;
namespace designpattern.cs.SimpleFactory
{
public class Circle:IShape
{
public void draw()
{
Console.WriteLine("Draw Circle ");
}
public void erase()
{
Console.WriteLine("Erase Circle ");
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class Circle:IShape
{
public void draw()
{
Console.WriteLine("Draw Circle ");
}
public void erase()
{
Console.WriteLine("Erase Circle ");
}
}
}
b) 矩形
using System;
namespace designpattern.cs.SimpleFactory
{
public class Rectangle : IShape
{
public void draw()
{
Console.WriteLine("Draw Rectangle ");
}
public void erase()
{
Console.WriteLine("Erase Rectangle ");
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class Rectangle : IShape
{
public void draw()
{
Console.WriteLine("Draw Rectangle ");
}
public void erase()
{
Console.WriteLine("Erase Rectangle ");
}
}
}
c) 三角
using System;
namespace designpattern.cs.SimpleFactory
{
public class Triangle : IShape
{
public void draw()
{
Console.WriteLine("Draw Triangle ");
}
public void erase()
{
Console.WriteLine("Erase Triangle ");
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class Triangle : IShape
{
public void draw()
{
Console.WriteLine("Draw Triangle ");
}
public void erase()
{
Console.WriteLine("Erase Triangle ");
}
}
}
我们要考虑到一种情况就是,当客户端输入一个无效的图形参数的时候,
因此,我们要准备一个未知图形类,如下:
using System;
namespace designpattern.cs.SimpleFactory
{
public class UnknownShape : IShape
{
public void draw()
{
Console.WriteLine("Draw Unknown Shape ");
}
public void erase()
{
Console.WriteLine("Erase Unknown Shape ");
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class UnknownShape : IShape
{
public void draw()
{
Console.WriteLine("Draw Unknown Shape ");
}
public void erase()
{
Console.WriteLine("Erase Unknown Shape ");
}
}
}
接下来,我们便可以构造我们的工厂类了,如下:
using System;
namespace designpattern.cs.SimpleFactory
{
public class ShapeFactory
{
public static IShape Create(string shape)
{
switch (shape.ToUpper())
{
case "CIRCLE":
return new Circle();
case "RECTANGLE":
return new Rectangle();
case "TRIANGLE":
return new Triangle();
default:
return new UnknownShape();
}
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class ShapeFactory
{
public static IShape Create(string shape)
{
switch (shape.ToUpper())
{
case "CIRCLE":
return new Circle();
case "RECTANGLE":
return new Rectangle();
case "TRIANGLE":
return new Triangle();
default:
return new UnknownShape();
}
}
}
}
最后,我们可以构造一个客户端来试试,如下:
using System;
namespace designpattern.cs.SimpleFactory
{
public class ClientDemo1
{
static void Main(string[] args)
{
IShape shape;
//draw circle
shape = ShapeFactory.Create("circle");
shape.draw();
shape.erase();
//draw rectangle
shape = ShapeFactory.Create("rectangle");
shape.draw();
shape.erase();
//draw unknown
shape = ShapeFactory.Create("diamond");
shape.draw();
shape.erase();
Console.ReadLine();
}
}
}
namespace designpattern.cs.SimpleFactory
{
public class ClientDemo1
{
static void Main(string[] args)
{
IShape shape;
//draw circle
shape = ShapeFactory.Create("circle");
shape.draw();
shape.erase();
//draw rectangle
shape = ShapeFactory.Create("rectangle");
shape.draw();
shape.erase();
//draw unknown
shape = ShapeFactory.Create("diamond");
shape.draw();
shape.erase();
Console.ReadLine();
}
}
}
输出结果:
draw circle ...
erase circle ...
draw rectangle ...
erase rectangle ...
draw Unknown Shape ...
erase Unknown Shape ...
技术改变世界