开通博客那么多天,第一次写博客,希望大家多多提意见,最近浏览了伍迷家园的『小菜编程成长记(五 体会简单工厂模式的美妙)』文章,看到了关于简单工厂模式的讲解,心中颇为心动,故此自己也编写了一个简单的计算器,当然了利用了文章里讲的“工厂模式”,好了先上图,看看样式!
只是实现了简单的元算而已,其中不足望各位看客海涵!好了,接着我们来看看代码
首先是运算具体实现的方法类,我将其命名为:“Mainmethod” 类;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Computer
{
abstract class Mainmethod
{
/// <summary>
/// 抽象方法,计算方法
/// </summary>
/// <returns>返回为double类型</returns>
public abstract double GetResult(double number1, double number2);
}
/// <summary>
/// 加法类
/// </summary>
class Oprationadd : Mainmethod
{
public override double GetResult(double number1, double number2)
{
double result = 0;
result = number1 + number2;
return result;
}
}
/// <summary>
/// 减法类
/// </summary>
class Oprationminus : Mainmethod
{
public override double GetResult(double number1, double number2)
{
double result = 0;
result = number1 - number2;
return result;
}
}
/// <summary>
/// 乘法类
/// </summary>
class Oprationride : Mainmethod
{
public override double GetResult(double number1, double number2)
{
double result = 0;
result = number1 * number2;
return result;
}
}
/// <summary>
/// 除法类
/// </summary>
class Oprationexcept : Mainmethod
{
public override double GetResult(double number1, double number2)
{
double result = 0;
result = number1 / number2;
return result;
}
}
}
Mainmethod类,我们说过了它其实就是一个运算类,主要有一个抽象的方法
GetResult(double number1,double number2),用于得到运算结果,然后将“加、减、乘、除都写成它的子类,然后重写了 GetResult(double number1,double number2) 方法,这样做就是为了达到程序可扩展性强的特点,如果想增加一个算法,直接写一个子类,重写父类方法就行了。
其次就是工厂模式中最为重要的“工厂”的代码了,因为这段代码就是解决运算时具体实例化出“加、减、乘、除”四个对象中的哪个对象的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Computer
{
class Operation
{
/// <summary>
/// 运算符工厂
/// </summary>
/// <param name="methodation"></param>
/// <returns>通过返回的对象实现具体对象的实例化操作</returns>
public static Mainmethod createoperate(string methodation)
{
Mainmethod method = null;
switch (methodation)
{
case "+":
{
method = new Oprationadd();
break;
}
case "-":
{
method = new Oprationminus();
break;
}
case "*":
{
method = new Oprationride();
break;
}
case "/":
{
method = new Oprationexcept();
break;
}
}
return method;
}
}
}
最后就是页面的代码了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Computer
{
public partial class Computer : Form
{
private Button btn;
public bool clearDisplay = true;
Mainmethod num;
public double num1;
public double num2;
public double result;
public Computer()
{
InitializeComponent();
}
private void CommonMethod(object sender, EventArgs e)
{
txtresult.Text = "";
btn = (Button)sender;
if (clearDisplay)
{
txtresult.Text = "";
clearDisplay = false;
}
txtresult.Text = txtresult.Text + btn.Text;
}
private void Common_click(object sender, EventArgs e)
{
btn=(Button)sender;
num = Operation.createoperate(btn.Text);
num1 =double .Parse (txtresult.Text);
txtresult.Text = "";
}
private void btnEqual_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtresult.Text);
result = num.GetResult(num1, num2);
txtresult.Text = result.ToString();
}
private void btncanel_Click(object sender, EventArgs e)
{
txtresult.Text = "0";
}
private void Computer_Load(object sender, EventArgs e)
{
txtresult.Text = "0";
}
}
}
当然了,如果想增加一个运算,不仅要增加相应的运算子类,还要在运算类工厂中增加switch的case分支。
第一次发表博文,其中略有不足之处,再次望各位看客们海涵,多提意见!