前言
工厂方法模式是一种常用的创建型设计模式
,它提供了一种将对象的创建过程封装起来的方法。在工厂方法模式中,将对象的创建过程交给一个工厂类
来完成,而不是在代码中直接调用构造函数来创建对象。这样可以使得代码更加灵活,降低耦合度
,方便后期维护和扩展。
工厂方法模式的核心思想是定义一个工厂接口,该接口包含一个创建产品的方法。具体的产品创建由实现该接口的具体
一、运行效果
实现计算器的加减乘数计算。
二、代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 计算器_工厂方法模式
{
class Program
{
//分析:将+、-、*、/四种运算分为四大类,每种类中的属性和方法类似,因此可使用操作抽象类
//在创建类对象的时候使用工厂方法模式,即建一个工厂抽象类,不同的运算创建新的工厂类,不同的类分在不同的工厂中制造
//这样子的好处是,创建不同类的工厂单独的方式修改,不会引起同类型其他工厂类的使用
static void Main(string[] args)
{
Console.WriteLine("请输入运算符:\r\n");
string operChar = Console.ReadLine();
Operation oper = null; ;
switch (operChar)
{
case "+":
{
FactoryOperationAdd facAdd = new FactoryOperationAdd();
oper = facAdd.creatOperation(operChar);
break;
}
case "-":
{
FactoryOperationSub facSub = new FactoryOperationSub();
oper = facSub.creatOperation(operChar);
break;
}
case "*":
{
FactoryOperationMul facMul = new FactoryOperationMul();
oper = facMul.creatOperation(operChar);
break;
}
case "/":
{
FactoryOperationDiv facDiv = new FactoryOperationDiv();
oper = facDiv.creatOperation(operChar);
break;
}
}
Console.WriteLine("请输入第一个数:\r\n");
oper.NumberA = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入第二个数:\r\n");
oper.NumberB = Convert.ToDouble(Console.ReadLine());
double result = oper.GetResult();
Console.WriteLine("计算结果是:" + oper.NumberA + operChar + oper.NumberB + "=" + result.ToString());
Console.ReadKey();
}
class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
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;
result = NumberA / NumberB;
return result;
}
}
/// <summary>
/// 工厂抽象基类
/// </summary>
abstract class Factory
{
public abstract Operation creatOperation(string oper);
}
class FactoryAdd : Factory
{
public override Operation creatOperation(string oper)
{
OperationAdd operAdd = new OperationAdd();
return operAdd;
}
}
class FactorySub : Factory
{
public override Operation creatOperation(string oper)
{
OperationSub operSub = new OperationSub();
return operSub;
}
}
class FactoryMul : Factory
{
public override Operation creatOperation(string oper)
{
OperationDiv operDiv = new OperationDiv();
return operDiv;
}
}
class FactoryOperationAdd:Factory
{
public override Operation creatOperation(string oper)
{
Operation operation = null;
switch (oper)
{
case "+":
{
operation = new OperationAdd();
break;
}
}
return operation;
}
}
class FactoryOperationSub : Factory
{
public override Operation creatOperation(string oper)
{
Operation operation = null;
switch (oper)
{
case "-":
{
operation = new OperationSub();
break;
}
}
return operation;
}
}
class FactoryOperationMul : Factory
{
public override Operation creatOperation(string oper)
{
Operation operation = null;
switch (oper)
{
case "*":
{
operation = new OperationMul();
break;
}
}
return operation;
}
}
class FactoryOperationDiv : Factory
{
public override Operation creatOperation(string oper)
{
Operation operation = null;
switch (oper)
{
case "/":
{
operation = new OperationDiv();
break;
}
}
return operation;
}
}
}
}
三、简单工厂模式代码对比
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
OperationFactory operF;
Operation oper;
Console.WriteLine("请输入运算符:\r\n");
string operChar=Console.ReadLine();
operF = new OperationFactory();
oper = operF.creatOperation(operChar);
Console.WriteLine("请输入第一个数:\r\n");
oper.NumberA = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入第二个数:\r\n");
oper.NumberB = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("运算结果为:\r\n");
double result = oper.GetResult();
Console.WriteLine(oper.NumberA.ToString() + operChar + oper.NumberB + "=" + result.ToString());
Console.ReadKey();
}
}
class Operation //运算基类
{
//定义两个数的属性
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
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;
result = NumberA / NumberB;
return result;
}
}
/// <summary>
/// 简单工厂创建对象,但一旦一个类的操作方式改变,整个工厂类都要改变,违反高内聚原则
/// </summary>
class OperationFactory //运算对象生产工厂
{
public Operation creatOperation(string operation)
{
Operation oper = null;
switch (operation)
{
case "+":
{
oper = new OperationAdd();
break;
}
case "-":
{
oper = new OperationSub();
break;
}
case "*":
{
oper = new OperationMul();
break;
}
case "/":
{
oper = new OperationDiv();
break;
}
}
return oper;
}
}
}
总结
工厂模式相对于简单工厂模式来说,针对每个产品类都有对应的产品工厂,这样每一类产品变动导致的工厂变动,只在对应的工厂中变动就可以。降低了耦合性,提高了内聚性。