工厂模式是创建型模式之一,其主要功能都是帮助我们把对象的实例化部分分离出来,降低系统中代码耦合度,增强了系统的扩展性,并将对象的创建过程延迟到子类进行
简单工厂模式实现加减乘除运算,工厂类OperationFactory 根据传入的运算符字符("+"、"-" ..),自动选择实例化的运算类并返回,实现计算:
// 运算基类(抽象产品类):
// --------------------
public class Operation
{
public double NumberA { get; set; }
public double NumberB { get; set; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
// 运算符派生类(具体产品类):
// ------------------------
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("除数不可为 0");
result = NumberA / NumberB;
return result;
}
}
// 工厂类:
// ------
public class OperationFactory
{
public static Operation CreateOperate(string operate)
{
//根据传入的参数operate选择实例化的类
Operation oper = null;
switch (operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
}
return oper;
}
}
// 客户端:
// ------
class Test
{
static void Main(string[] args)
{
//创建运算基类的对象,传入字符串到工厂类,返回对应实类的实例对象
Operation oper;
oper = OperationFactory.CreateOperate("+");
oper.NumberA = 4; oper.NumberB = 2;
double result = oper.GetResult();
Console.WriteLine(result); // 6
oper = OperationFactory.CreateOperate("-");
oper.NumberA = 4; oper.NumberB = 2;
result = oper.GetResult();
Console.WriteLine(result); // 2
oper = OperationFactory.CreateOperate("/");
oper.NumberA = 4; oper.NumberB = 2;
result = oper.GetResult();
Console.WriteLine(result); // 2
}
}
对上节简单工厂方法的项目示例做修改,实现工厂方法模式:抽象产品类、各运算类保持不变,添加抽象工厂接口,并由该接口派生出各类计算方法的工厂类,客户端通过工厂类即可实现计算功能
#region 上述的运算基类(抽象产品类) + 运算符派生类(具体产品类)
//...
#endregion
// 抽象工厂接口:
// -----------
interface IFactory
{
Operation CreateOperation();
}
// 各运算工厂类:
// -----------
// 加法工厂
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
// 减法工厂
class SubFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
// 乘法工厂
class MulFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationMul();
}
}
// 除法工厂
class DivFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
// 客户端:
// ------
class Test
{
static void Main(string[] args)
{
//用户选择产品对应的工厂进行生产
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation();
oper.NumberA = 4; oper.NumberB = 2;
double result = oper.GetResult(); //6
}
}
采用抽象工厂模式实现苹果、戴尔电脑的生产工厂:创建两个抽象产品接口(显示器、主机),根据该接口实现具体产品类(苹果显示器、苹果主机、戴尔..);创建抽象工厂接口(电脑工厂),根据该接口实现具体工厂类(苹果电脑工厂、戴尔电脑工厂);最终在客户端进行工厂创建、生产产品
// 抽象产品类:
// ----------
// 抽象产品 - 显示器
interface IDisplay
{
void Show();
}
// 抽象产品 - 主机
interface IMainFrame
{
void Open();
}
// 具体产品类:
// ----------
// 具体产品 - 苹果显示器
class AppleDisplay : IDisplay
{
public void Show()
{
Console.WriteLine("苹果显示器正常工作!");
}
}
//具体产品 - 戴尔显示器
class DellDisplay : IDisplay
{
public void Show()
{
Console.WriteLine("戴尔显示器正常工作!");
}
}
// 具体产品 - 苹果主机
class AppleMainFrame : IMainFrame
{
public void Open()
{
Console.WriteLine("苹果主机启动成功!");
}
}
// 具体产品 - 戴尔主机
class DellMainFrame : IMainFrame
{
public void Open()
{
Console.WriteLine("戴尔主机启动成功!");
}
}
// 抽象工厂类:
// ----------
// 抽象电脑工厂
interface IComputerFactory
{
// 生产一个显示器
IDisplay ProduceADisplay();
// 生产一个主机
IMainFrame ProduceAMainFrame();
}
// 具体工厂类:
// ----------
// 具体工厂 - 苹果电脑工厂
class AppleComputerFactory:IComputerFactory
{
public IDisplay ProduceADisplay()
{
return new AppleDisplay();
}
public IMainFrame ProduceAMainFrame()
{
return new AppleMainFrame();
}
}
// 具体工厂 - 戴尔电脑工厂
class DellComputerFactory : IComputerFactory
{
public IDisplay ProduceADisplay()
{
return new DellDisplay();
}
public IMainFrame ProduceAMainFrame()
{
return new DellMainFrame();
}
}
// 客户端:
// ------
class Client
{
static void Main()
{
//创建苹果电脑工厂
IComputerFactory MyAppleFactory = new AppleComputerFactory();
//生产苹果显示器
IDisplay AppleDisplay = MyAppleFactory.ProduceADisplay();
AppleDisplay.Show(); //苹果显示器正常工作!
//创建戴尔电脑工厂
IComputerFactory MyDellFactory = new DellComputerFactory();
//生产戴尔主机
IMainFrame DellMainFrame = MyDellFactory.ProduceAMainFrame();
DellMainFrame.Open(); //戴尔主机启动成功!
}
}
原文:https://www.cnblogs.com/SouthBegonia/p/11967904.html