简单工厂模式是解决了一个灵活通过方法封装 new 对象的操作,也就是解决对象创建问题。
下面的计算器实例,演示了通过方法根据条件利用多态的特性建立需要的业务对象执行对象特定的任务。
(在这里将全部引用大话设计模式中的例题代码和内容,劳动属于作者。)
书中有一个类图非常好,一目了然的说明类图的含义和标记 12页
逻辑组件(现在电脑粘贴乱码,所以粘贴黑白代码)
using System;
using System.Collections.Generic;
using System.Text;
namespace OperationLibrary
{
/// <summary>
/// 运算类,计算所用的父类
/// </summary>
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;
/// <summary>
/// 数字A
/// </summary>
public double NumberA
{
get
{
return _numberA;
}
set
{
_numberA = value;
}
}
/// <summary>
/// 数字B
/// </summary>
public double NumberB
{
get
{
return _numberB;
}
set
{
_numberB = value;
}
}
/// <summary>
/// 得到运算结果,虚方法,子类重写
/// </summary>
/// <returns></returns>
public virtual double GetResult()
{
double result = 0;
return result;
}
/// <summary>
/// 检查输入的字符串是否准确
/// </summary>
/// <param name="currentNumber">文本框中显示的数字</param>
/// <param name="inputString">刚点击的数字</param>
/// <returns></returns>
public static string checkNumberInput(string currentNumber, string inputString)
{
string result = "";
if (inputString == ".")
{
//输入的是小数点
if (currentNumber.IndexOf(".") < 0)
{
//如果之前没有输入值,要在前面加上一个0
if (currentNumber.Length == 0)
result = "0" + inputString;
else
result = currentNumber + inputString;
}
}
else if (currentNumber == "0")
{
result = inputString;
}
else
{
//累加两个值
result = currentNumber + inputString;
}
return result;
}
}
#region 计算的子类
/// <summary>
/// 加法类
/// </summary>
class OperationAdd : Operation
{
//重写计算方法
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
/// <summary>
/// 减法类
/// </summary>
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
/// <summary>
/// 乘法类
/// </summary>
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
/// <summary>
/// 除法类
/// </summary>
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}
/// <summary>
/// 平方类
/// </summary>
class OperationSqr : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberB * NumberB;
return result;
}
}
/// <summary>
/// 平方根类
/// </summary>
class OperationSqrt : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB < 0)
throw new Exception("负数不能开平方根。");
result = Math.Sqrt(NumberB);
return result;
}
}
/// <summary>
/// 相反数类
/// </summary>
class OperationReverse : Operation
{
public override double GetResult()
{
double result = 0;
result = -NumberB;
return result;
}
}
#endregion
/// <summary>
/// 运算类工厂
/// </summary>
public class OperationFactory
{
//根据参数判断要建立返回的那个类。
public static Operation createOperate(string 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;
}
case "sqr":
{
oper = new OperationSqr();
break;
}
case "sqrt":
{
oper = new OperationSqrt();
break;
}
case "+/-":
{
oper = new OperationReverse();
break;
}
}
return oper;
}
}
}
Form界面使用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OperationLibrary;
namespace 计算器
{
/// <summary>
/// 面向对象编程
/// 1、加运算类
/// 2、加运算子类
/// 3、加运算类工厂
/// 4、更改Form1
public partial class Form1 : Form
{
bool bOperate = false;
Operation oper;//计算类
public Form1()
{
InitializeComponent();
}
private void button0_Click(object sender, EventArgs e)
{
//判断文本框有没有值,有值就清空
if (bOperate)
{
txtShow.Text = "";
bOperate = false;
}
string number = ((Button)sender).Text; //获得按钮文本,也就是数字
txtShow.Text = Operation.checkNumberInput(txtShow.Text, number);
}
private void buttonClear_Click(object sender, EventArgs e)
{
txtShow.Text = "";
}
/// <summary>
/// "+"按钮的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAdd_Click(object sender, EventArgs e)
{
if (txtShow.Text != "")
{
oper = OperationFactory.createOperate(((Button)sender).Text);
oper.NumberA = Convert.ToDouble(txtShow.Text);
bOperate = true;
}
}
/// <summary>
/// “=” 按钮的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonEqual_Click(object sender, EventArgs e)
{
if (txtShow.Text != "")
{
if (((Button)sender).Text != "=")
{
oper = OperationFactory.createOperate(((Button)sender).Text);
}
oper.NumberB = Convert.ToDouble(txtShow.Text);
txtShow.Text = oper.GetResult().ToString();
bOperate = true;
}
}
}
}
控制台界面
using System;
using System.Collections.Generic;
using System.Text;
using OperationLibrary;
namespace 计算器控制台
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("请输入数字A:");
string strNumberA = Console.ReadLine();
Console.Write("请选择运算符号(+、-、*、/):");
string strOperate = Console.ReadLine();
Console.Write("请输入数字B:");
string strNumberB = Console.ReadLine();
string strResult = "";
Operation oper;
oper = OperationFactory.createOperate(strOperate);
oper.NumberA = Convert.ToDouble(strNumberA);
oper.NumberB = Convert.ToDouble(strNumberB);
strResult = oper.GetResult().ToString();
Console.WriteLine("结果是:" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("您的输入有错:" + ex.Message);
}
}
}
}