本篇内容为博主个人在学习《大话设计模式》(程杰 著)过程中的测试随笔,在此给各位编程新手作为学习参考,毕竟网上深入学习的资料对于瓶颈期的新人大多不太友好,我会尽量详细的展示并解释每一个细节,也欢迎大家对我的不足给予批评指正。
本篇作者以简易计算器程序作为案例,向我们展示了如何通过简单工厂模式来处理低耦合代码的灵活实例化相应业务类的实例,让我们领略真正面向对象编程的思维。
运算业务类基类(BaseOperation)定义了两个运算值(NumberA和NumberB)和一个获取结果的虚方法(GetResult())。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
//运算业务类基类
class BaseOperation
{
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; }
}
///
/// virtual修饰符表示该方法为虚方法,可以被子类重写
/// 此处用于给各具体计算业务子类(加减乘除等)重写计算过程
///
///
public virtual double GetResult()
{
double Result = 0;
return Result;
}
}
}
继承运算业务类基类(BaseOperation),重写基类方法GetResult(),实现对两个运算值(NumberA和NumberB)的加法运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
///
/// 加法运算子类,继承BaseOperation运算业务基类
///
class OperationAdd : BaseOperation
{
///
/// 重写基类运算方法 override修饰符表示重写基类方法
///
///
public override double GetResult()
{
double Result = 0;
Result = NumberA + NumberB;
return Result;
}
}
}
继承运算业务类基类(BaseOperation),重写基类方法GetResult(),实现对两个运算值(NumberA和NumberB)的减法运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
///
/// 减法运算子类,继承BaseOperation运算业务基类
///
class OperationSub : BaseOperation
{
///
/// 重写基类运算方法 override修饰符表示重写基类方法
///
///
public override double GetResult()
{
double Result = 0;
Result = NumberA - NumberB;
return Result;
}
}
}
继承运算业务类基类(BaseOperation),重写基类方法GetResult(),实现对两个运算值(NumberA和NumberB)的乘法运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
///
/// 乘法运算子类,继承BaseOperation运算业务基类
///
class OperationMul : BaseOperation
{
///
/// 重写基类运算方法 override修饰符表示重写基类方法
///
///
public override double GetResult()
{
double Result = 0;
Result = NumberA * NumberB;
return Result;
}
}
}
继承运算业务类基类(BaseOperation),重写基类方法GetResult(),实现对两个运算值(NumberA和NumberB)的除法运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
///
/// 除法运算子类,继承BaseOperation运算业务基类
///
class OperationDiv : BaseOperation
{
///
/// 重写基类运算方法 override修饰符表示重写基类方法
///
///
public override double GetResult()
{
double Result = 0;
Result = NumberA / NumberB;
return Result;
}
}
}
工厂类定义了一个实例化运算业务类的方法,通过传入的运算符字符串,返回相应运算业务类的实例化对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyFactoryTest.Business
{
class OperationFactory
{
///
/// 根据运算符实例化相应的运算业务实例
///
///
///
public static BaseOperation InstantiationOperation(string oOperator)
{
BaseOperation oOperationInstantiation = null;
switch (oOperator)
{
case "+":
oOperationInstantiation = new OperationAdd();
break;
case "-":
oOperationInstantiation = new OperationSub();
break;
case "*":
oOperationInstantiation = new OperationMul();
break;
case "/":
oOperationInstantiation = new OperationDiv();
break;
}
return oOperationInstantiation;
}
}
}
Winfrom后台代码
using EasyFactoryTest.Business;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EasyFactoryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_count_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txt_NumberA.Text.Trim()) || string.IsNullOrEmpty(txt_NumberB.Text.Trim()))
{
MessageBox.Show("请输入计算值!");
return;
}
if (cbx_Operate.SelectedItem == null)
{
MessageBox.Show("请选择运算!");
return;
}
//通过工厂类获取对应运算业务实例
BaseOperation oOperationInstantiation = OperationFactory.InstantiationOperation(cbx_Operate.SelectedItem.ToString());
//给输入赋值
oOperationInstantiation.NumberA = double.Parse(txt_NumberA.Text.Trim());
oOperationInstantiation.NumberB = double.Parse(txt_NumberB.Text.Trim());
//输出结果
txt_Result.Text = oOperationInstantiation.GetResult().ToString();
}
}
}
以上便是整个简单工厂模式案例代码,学习设计模式是面向对象编程的必由之路,可惜自己并没有早一点醒悟,于是在工作两年多的今天,我依然只是在公司开发框架套路下复制粘贴的小菜鸡,作为一个靠软件开发吃饭的人来说,最恐怖的莫过于原地踏步,安于现状。当我产生想要自己写点小项目的想法时,竟发现自己离开了公司框架连基本的运用常用ORM框架操作数据库都举步维艰,于是痛下决心踏实学习,希望能够弥补缺失的那些基础知识。也希望后继的小猿们能够脚踏实地、踏实学习,对于编程方面的基础知识务必自己亲自通过代码一一体会一遍。