做为一名程序员,必要的总结是不能够缺乏的。计划从如今开始到明年年末将《大话设计模式》这本书中所列举的设计模式都纪录一遍用于之后工做的提高!javascript
简单工厂之计算器css
写这个功能以前,我须要整理一下写代码的基本思路:写程序必需要以能复用,易于维护为原则,必定要使用面向对象的三大特征 封装继承和多态。引用《大话设计模式》中的一个例子 在活字印刷出来以前,印刷的模板都是一次性使用的,所以一本书的成本过高,这样大规模使用是很难实现的。若是放到当今这个以流量为王的时代绝对会被淘汰。 代码java
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;
}
}
//上边是抽象出来其余运算类须要继承的父类。web
//下边写具体实现
//加法类ajax
{
public override double getResult()
{
double result = 0;
result = _NumbA + _NumbB;
return result;
}
}
//减法类算法
{
public override double getResult()
{
double result = 0;
result = _NumbA - _NumbB;
return result;
}
}
//乘法类express
{
public override double getResult()
{
double result = 0;
result = _NumbA * _NumbB;
return result;
}
}
//除法类设计模式
{
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类的父类,经过反射的方式简化调用程序。asp.net
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布局
<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>
//仅仅实现了+-*/最基本的算法,所以没有涉及到的按钮id名称就没修改
//如下是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>