设计模式学习之工厂模式2
刚刚学习了设计模式的简单工厂模式,现在又学了工厂模式,把工厂模式的代码写下来!
还是一样先建一个运算类,比简单工厂不同的是,我们在这里再建一个工厂接口,这个接口里面定义了一个方法,返回一个Operation运算类,
/// <summary> /// 运算类 /// </summary> public class Operation { private double numbera;//第一个运算数 public double Numbera { get { return numbera; } set { numbera = value; } } private double Numberb;//第二个运算数 public double Numberb1 { get { return Numberb; } set { Numberb = value; } } public virtual double GetResult() //运算方法,空,在子类中会重写这个方法 { double result = 0; return result; } } /// <summary> /// 工厂接口 /// </summary> public interface Ifactory { Operation Createoperation(); }
然后再建4个运算类,如下,这里就一起建了
1 /// <summary> 2 /// 加法运算类 3 /// </summary> 4 class jiafayunsan:Operation 5 { 6 public override double GetResult() //重写父类中的运算方法 7 { 8 double result = 0; 9 result = base.Numbera + base.Numberb1; //用父类中的这个参数相加 10 return result; 11 } 12 } 13 14 /// <summary> 15 /// 减法类 16 /// </summary> 17 class Operationsub:Operation 18 { 19 public override double GetResult()//重写父类中的运算方法 20 { 21 double result = 0; 22 result = base.Numbera - base.Numberb1;//用父类中的这个参数相减 23 return result; 24 } 25 } 26 27 /// <summary> 28 /// 成法类 29 /// </summary> 30 class OperationMul:Operation 31 { 32 public override double GetResult()//重写父类中的运算方法 33 { 34 double retult = 0; 35 retult = base.Numbera * base.Numberb1;//用父类中的这个参数相成 36 return retult; 37 } 38 } 39 40 /// <summary> 41 /// 除法类 42 /// </summary> 43 class OperationDiv:Operation 44 { 45 46 47 public override double GetResult()//重写父类中的运算方法 48 { 49 double result = 0; 50 if (base.Numbera > 0) 51 { 52 result = base.Numbera / Numberb1;//用父类中的这个参数相除 53 } 54 return result; 55 } 56 }
我把这4个运算写在一起了,为了方便观看,然后我们再建4个工厂类,这4个工厂类都对应上面4个运算类,而且每个工厂类都要继承工厂接口Ifactory,代码如下:
1 /// <summary> 2 /// 除法类工厂 3 /// </summary> 4 public class Divfactory:Ifactory 5 { 6 #region Ifactory 成员 7 8 public Operation Createoperation() 9 { 10 return new OperationDiv(); 11 } 12 13 #endregion 14 } 15 16 /// <summary> 17 /// 减法类工厂 18 /// </summary> 19 public class SubFactory:Ifactory 20 { 21 22 23 #region Ifactory 成员 24 25 public Operation Createoperation() 26 { 27 return new Operationsub(); 28 } 29 30 #endregion 31 } 32 /// <summary> 33 /// 成法类工厂 34 /// </summary> 35 public class Mulfactory:Ifactory 36 { 37 38 #region Ifactory 成员 39 40 public Operation Createoperation() 41 { 42 return new OperationMul(); 43 } 44 45 #endregion 46 } 47 /// <summary> 48 /// 除法类工厂 49 /// </summary> 50 public class Divfactory:Ifactory 51 { 52 #region Ifactory 成员 53 54 public Operation Createoperation() 55 { 56 return new OperationDiv(); 57 } 58 59 #endregion 60 }
前台计算的页面还是一样,也是用ajax的方式提交
1 <head runat="server"> 2 <title></title> 3 <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 4 <script type="text/javascript"> 5 $(function () { 6 7 $("#buttons").click(function () { 8 9 var number1 = document.getElementById("number1").value;//第一个运算数 10 var number2 = document.getElementById("number2").value;//第二个运算数 11 var yunsanfu = document.getElementById("Select1").value;//运算符 12 13 //用Jquery.post方法提交 14 $.post("Handler1.ashx", { numbera: number1, numberb: number2, yunsanfus: yunsanfu }, function (date) { 15 document.getElementById("count").value = date; 16 17 18 }) 19 }) 20 21 }) 22 </script> 23 </head> 24 <body> 25 <form id="form1" runat="server"> 26 <div> 27 <input id="number1" type="text" name="name" value="" /> 28 <select id="Select1"> 29 <option value="+">+</option> 30 <option value="-">-</option> 31 <option value="*">*</option> 32 <option value="/">/</option> 33 </select> 34 <input id="number2" type="text" name="name" value="" /> 35 <input id="buttons" type="button" value="=" /> 36 <input id="count" type="text" name="name" /> 37 </div> 38 <div> 39 40 </div> 41 </form> 42 </body> 43 </html>
后台代码如下:
1 /// <summary> 2 /// Handler1 的摘要说明 3 /// </summary> 4 public class Handler1 : IHttpHandler 5 { 6 7 public void ProcessRequest(HttpContext context) 8 { 9 context.Response.ContentType = "text/plain"; 10 11 12 string numbera = context.Request.Form["numbera"];//第一个数字 13 14 string numberb = context.Request.Form["numberb"];//第二个数字 15 string numberc = context.Request.Form["yunsanfus"]; //运算符 16 Operation oper=null; 17 Ifactory operfactory; 18 if (numberc == "+") 19 { 20 operfactory = new Addfactory(); //实列加法工厂 21 oper = operfactory.Createoperation(); 22 23 } 24 else if (numberc == "-") 25 { 26 operfactory =new SubFactory (); //实列减法工厂 27 oper = operfactory.Createoperation(); 28 } 29 else if (numberc == "*") 30 { 31 operfactory = new Mulfactory();//实列成法工厂 32 oper = operfactory.Createoperation(); 33 } 34 else if (numberc == "/") 35 { 36 operfactory = new Divfactory();//实列除法工厂 37 oper = operfactory.Createoperation(); 38 } 39 40 oper.Numbera = Convert.ToDouble(numbera); 41 oper.Numberb1 = Convert.ToDouble(numberb); 42 43 double result = oper.GetResult(); 44 context.Response.Write(result); 45 46 //Operation oper; 47 //oper = JianDanGongchang.createoperate(numberc);//调用简单运算工厂类,传一个运算符,工厂类会帮你自己计算: 48 49 //oper.Numbera = Convert .ToDouble(numbera); //把一个参数传进来 50 //oper.Numberb1 = Convert .ToDouble(numberb);//把第二个参数传进来 51 52 //double result = oper.GetResult(); //调用运算方法 53 54 //context.Response.Write(result); //返回运算回来的结果,回传给WebForm1.aspx页面。 55 } 56 57 public bool IsReusable 58 { 59 get 60 { 61 return false; 62 } 63 } 64 }
工厂方法和简单工厂主要分别是,简单工厂类中有了所有的逻辑判断,可以根据客户端的选择动态的选择要实列化的类,对于客户端来说,除去了与具体产品的依赖,而工厂方法,在以后要改动的情况下可以不用去改工厂类了,而只用加运算类,加工厂类就行,