作为一名程序员,必要的总结是不可以缺少的。计划从现在开始到明年年底将《大话设计模式》这本书中所列举的设计模式都纪录一遍用于以后工作的提升!
简单工厂之计算器
写这个功能之前,我需要整理一下写代码的基本思路:写程序必须要以能复用,易于维护为原则,一定要使用面向对象的三大特征 封装继承和多态。引用《大话设计模式》中的一个例子 在活字印刷出来之前,印刷的模板都是一次性使用的,因此一本书的成本太高,这样大规模使用是很难实现的。如果放到当今这个以流量为王的时代绝对会被淘汰。 代码
-
public class Operation
-
{
-
private double _numbA = 0;
-
private double _numbB = 0;
-
public double _NumbA
-
{
-
get
-
{
-
return _numbA;
-
}
-
set
-
{
-
_numbA = value;
-
}
-
}
-
public double _NumbB
-
{
-
get
-
{
-
return _numbB;
-
}
-
set
-
{
-
_numbB = value;
-
}
-
}
-
public virtual double getResult()
-
{
-
double result = 0;
-
return result;
-
}
-
}
//上边是抽象出来其他运算类需要继承的父类。
//下边写具体实现
//加法类
- public class OperationAdd : Operation
-
{
-
public override double getResult()
-
{
-
double result = 0;
-
result = _NumbA + _NumbB;
-
return result;
-
}
-
}
//减法类
- public class OperationSub : Operation
-
{
-
public override double getResult()
-
{
-
double result = 0;
-
result = _NumbA - _NumbB;
-
return result;
-
}
-
}
//乘法类
- public class OperationMulti : Operation
-
{
-
public override double getResult()
-
{
-
double result = 0;
-
result = _NumbA * _NumbB;
-
return result;
-
}
-
}
//除法类
- public class OperationDivde : Operation
-
{
-
public override double getResult()
-
{
-
double result = 0;
-
if (_NumbB == 0)
-
{
-
throw new Exception("除数不能为零!!");
-
}
-
result = _NumbA / _NumbB;
-
return result;
-
}
-
}
//后台类已经写好了,前台控制台程序也好,webform程序也好都可以使用。我是以asp.net webform配合ajax来实现的。 //前台数据通过ajax传递到后台一般处理性程序(ashx)文件,数据处理后将数据传递到前台。 //在这里需要添加一个ashx类的父类,通过反射的方式简化调用程序。
-
public class Parent : IHttpHandler
-
{
-
private string m_flag;
-
public string Flag
-
{
-
get { return m_flag; }
-
set { m_flag = value; }
-
}
-
public void ProcessRequest(HttpContext context)
-
{
-
if (context.Request["flag"] != null)
-
m_flag = context.Request["flag"].ToString();
-
var methodName = context.Request["flag"];
-
Type objType = this.GetType();
-
MethodInfo method = objType.GetMethod(methodName == null ? "" : methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-
if (method != null)
-
{
-
HttpContext[] objParams = new HttpContext[] { context };
-
method.Invoke(this, objParams);
-
}
-
else
-
{
-
Other(context);
-
}
-
}
-
public virtual void Other(HttpContext context)
-
{
-
}
-
public bool IsReusable
-
{
-
get
-
{
-
return false;
-
}
-
}
-
}
//创建调用类,调用类中的代码
-
public class Caculate : Parent
-
{
-
public void NumOperate(HttpContext context)
-
{
-
var expression = context.Request["expression"];//这里是将用户的表达式获取过来
-
var operateFlag = context.Request["operateFlag"].ToCharArray();
-
var ExpreArry = expression.Split(operateFlag);//然后通过用户输入的运算符来分割
-
Operation operate = null;
-
switch (operateFlag[0])
-
{
-
case '+':
-
operate = new OperationAdd();
-
break;
-
case '-':
-
operate = new OperationSub();
-
break;
-
case '*':
-
operate = new OperationMulti();
-
break;
-
case '/':
-
operate = new OperationDivde();
-
break;
-
}
-
operate._NumbA = Convert.ToDouble(ExpreArry[0]);//不论是加减乘除都可以使用同一种方式来调用
-
operate._NumbB = Convert.ToDouble(ExpreArry[1]);
-
var result = operate.getResult();
-
context.Response.Write(result);
-
context.Response.End();
-
}
-
}
//后台调用完毕,下边开始写前台调用方式。首先需要引用jQuery文件,然后就可以使用jQuery中的方法了。在这里不展示引用(太简单)
//前台HTML布局 使用的是table布局
- <body>
-
<table id="caclator">
-
<tr>
-
<td colspan="5">
-
<input type="text" id="txtExpression" />
-
</td>
-
</tr>
-
<tr>
-
<td>
-
<input type="button" value="MC" id="btnMC" />
-
</td>
-
<td>
-
<input type="button" value="MR" id="btnMR" />
-
</td>
-
<td>
-
<input type="button" value="MS" id="btnMS" />
-
</td>
-
<td>
-
<input type="button" value="M+" id="btnMAdd" />
-
</td>
-
<td>
-
<input type="button" value="M-" id="btnMSub" />
-
</td>
-
</tr>
-
<tr>
-
<td>
-
<input type="button" value="←" id="btnBackSpace" />
-
</td>
-
<td>
-
<input type="button" value="CE" id="btnCE" />
-
</td>
-
<td>
-
<input type="button" value="C" id="btnC" />
-
</td>
-
<td>
-
<input type="button" value="±" id="Button4" />
-
</td>
-
<td>
-
<input type="button" value="√" id="Button5" />
-
</td>
-
</tr>
-
<tr>
-
<td>
-
<input type="button" value="7" id="btnNumSeven" />
-
</td>
-
<td>
-
<input type="button" value="8" id="btnNumEight" />
-
</td>
-
<td>
-
<input type="button" value="9" id="btnNumNine" />
-
</td>
-
<td>
-
<input type="button" value="/" id="btnOperDivide" />
-
</td>
-
<td>
-
<input type="button" value="%" id="Button10" />
-
</td>
-
</tr>
-
<tr>
-
<td>
-
<input type="button" value="4" id="btnNumFour" />
-
</td>
-
<td>
-
<input type="button" value="5" id="btnNumFive" />
-
</td>
-
<td>
-
<input type="button" value="6" id="btnNumSix" />
-
</td>
-
<td>
-
<input type="button" value="*" id="btnOperMulti" />
-
</td>
-
<td>
-
<input type="button" value="1/x" id="Button12" />
-
</td>
-
</tr>
-
<tr>
-
<td>
-
<input type="button" value="1" id="btnNumOne" />
-
</td>
-
<td>
-
<input type="button" value="2" id="btnNumTwo" />
-
</td>
-
<td>
-
<input type="button" value="3" id="btnNumThree" />
-
</td>
-
<td>
-
<input type="button" value="-" id="btnOperSub" />
-
</td>
-
<td rowspan="2">
-
<input type="button" value="=" id="btnEques" />
-
</td>
-
</tr>
-
<tr>
-
<td colspan="2">
-
<input type="button" value="0" id="btnNumZero" />
-
</td>
-
<td>
-
<input type="button" value="." id="btnPoint" />
-
</td>
-
<td>
-
<input type="button" value="+" id="btnOperAdd" />
-
</td>
-
</tr>
-
</table>
- </body>
//仅仅实现了+-*/最基本的算法,因此没有涉及到的按钮id名称就没修改
//以下是css样式设置
- <style type="text/css">
-
input[type='button']
-
{
-
height: 25px;
-
width: 35px;
-
}
-
input[type='button'][value='0']
-
{
-
height: 25px;
-
width: 72px;
-
}
-
input[type='button'][value='=']
-
{
-
height: 52px;
-
width: 35px;
-
}
-
input[type='text']
-
{
-
width: 185px;
-
}
-
</style>
//运行图仿照win7中计算器样式
//最后添加jQuery代码
-
<script type="text/javascript">
-
var expression = ""; //存放用户表达式
-
var operateFlag = ""; //存放用户操作 +-*/
-
$(function () {
-
$("#txtExpression").val('');
-
$("#caclator").offset({ top: 300, left: 900 });
-
$.each($("input"), function (i, j) {
-
if (j.id.indexOf("Num") > -1) {
-
$("#" + j.id).click(function () {
-
expression += this.value;
-
$("#txtExpression").val(expression);
-
})
-
}
-
})
-
$.each($("input"), function (i, j) {
-
if (j.id.indexOf("Oper") > -1) {
-
$("#" + j.id).click(function () {
-
expression += this.value;
-
$("#txtExpression").val(expression);
-
if (this.value != ".") {
-
operateFlag = "";
-
operateFlag = this.value;
-
}
-
})
-
}
-
})
-
$("#btnEques").click(function () {
-
var url = "/Handler/Caculate.ashx?t=" + new Date();
-
$.ajax({
-
type: "POST",
-
url: url,
-
data: { flag: "NumOperate", expression: expression, operateFlag: operateFlag },
-
async: false,
-
cache: false,
-
success: function (msg) {
-
expression = "";
-
$("#txtExpression").val(msg);
-
},
-
error: function (msg) {
-
if (msg.status == 500) {
-
alert("错误,不允许的操作!");//如果除数为0,那么就会弹出提示信息
-
}
-
}
-
})
-
})
-
})
-
</script>