前言:工厂方法模式名字上和前面第一个模式差不多,那就是简单工厂。其实这个计算器的例子,在前面使用简单工厂实现了一遍了。这次只是和使用相同的例子和工厂方法模式进行对比。在实现工厂方法模式的过程中发现了这个模式的优缺点!
优点:是每一个类或者功能的独立性非常好。实现了可扩展,而不用修改其他的类。
缺点:每增加一个产品就需要增加一个产品工厂的类。
目录
类图:
代码:
form窗体:
//窗体通用声明
Operation oper =new Operation();//实例化运算类
double result = 0;//结果
//给按钮赋值
string NumberA = "";
string NumberB = "";
//区分AB的赋值标签,如果是fals就给A赋值。
//否则给B赋值。
bool flage = false;
//数字按钮
private void button1_Click(object sender, EventArgs e)
{
//判断赋值对象
if (flage)
{
NumberB += 1;
textBox1.Text = NumberB;
}
else
{
NumberA += 1;
textBox1.Text = NumberA;
}
}
//每一个数字按钮的代码,几乎一摸一样。除了代表的数字之外,
//其他的代码完全一样。所以就以1这个数字按钮为例就可以了。在这就不一一列举了!
运算:
private void button12_Click(object sender, EventArgs e)
{
flage = true;//更改赋值标签
//判断是否计算
IFactory operFactory = new AddFactory();//实例化加法工厂
oper = operFactory.CreateOperation();//创建加法运算
}
private void button11_Click(object sender, EventArgs e)
{
flage = true;//更改赋值标签
IFactory operFactory = new SubFactory();
oper = operFactory.CreateOperation();
}
private void button14_Click(object sender, EventArgs e)
{
flage = true;//更改赋值标签
IFactory opertFactory = new MulFactory();
oper = opertFactory.CreateOperation();
}
private void button14_Click(object sender, EventArgs e)
{
flage = true;//更改赋值标签
IFactory opertFactory = new MulFactory();
oper = opertFactory.CreateOperation();
}
private void button10_Click(object sender, EventArgs e)
{
//通过属性,给A和B赋值。
oper.NumberA = double.Parse(NumberA);
oper.NumberB = double.Parse(NumberB);
//计算结果
result = oper.GetResult();
//显示结果
textBox1.Text = result.ToString();
}
类:
运算类
namespace 计算器
{
/// <summary>
/// 运算类
/// </summary>
public class Operation
{
//两个数
private double _numberA = 0;
private double _numberB = 0;
/// <summary>
/// 第一个数
/// </summary>
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
/// <summary>
/// 第二个数
/// </summary>
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
/// <summary>
/// 计算方法
/// </summary>
/// <returns></returns>
public virtual double GetResult()
{
double result = 0;
return result;
}
}
}
namespace 计算器
{
/// <summary>
/// 加法类
/// </summary>
public class OperationAdd:Operation
{
/// <summary>
/// 加法
/// </summary>
/// <returns>计算结果</returns>
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;//返回结果
}//end Getresult
}
}
namespace 计算器
{
/// <summary>
/// 乘法类
/// </summary>
public class OperationMul:Operation
{
/// <summary>
/// 乘法
/// </summary>
/// <returns>结果</returns>
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}//end GetResult
}
}
namespace 计算器
{
public class OpertionDiv:Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB==0)
{
result = 0;
}
else
{
result = NumberA / NumberB;
}//end if
return result;
}//end GetResult
}
}
namespace 计算器
{
/// <summary>
/// 减法
/// </summary>
public class OperationSub:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}//end GetResult
}
}
工厂方法:
namespace 计算器
{
//工厂接口
public interface IFactory
{
Operation CreateOperation();//创建具体运算
}
}
namespace 计算器
{
public class SubFactory:IFactory
{
public Operation CreateOperation()
{
return new OperationSub();//减法
}
}
}
namespace 计算器
{
/// <summary>
/// 加法工厂
/// </summary>
public class AddFactory:IFactory
{
/// <summary>
/// 创建加法运算
/// </summary>
/// <returns></returns>
public Operation CreateOperation()
{
return new OperationAdd();//实例化加法类
}
}
}
namespace 计算器
{
public class MulFactory:IFactory
{
public Operation CreateOperation()
{
return new OperationMul();//乘法
}
}
}
namespace 计算器
{
public class DivFactory:IFactory
{
public Operation CreateOperation()
{
return new OpertionDiv();//除法
}
}
}