设计模式:工厂方法模式(Factory Method)
定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类。
工厂方法使一个类的实例化延迟到其子类。
结构图:
示例:
HTML代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="txtNumberA"></asp:TextBox> <asp:DropDownList runat="server" ID="ddlType"> <asp:ListItem Selected="True">+</asp:ListItem> <asp:ListItem>-</asp:ListItem> <asp:ListItem>*</asp:ListItem> <asp:ListItem>/</asp:ListItem> </asp:DropDownList> <asp:TextBox runat="server" ID="txtNumberB"></asp:TextBox> = <asp:TextBox runat="server" ID="txtResult"></asp:TextBox> <asp:Button runat="server" ID="btnCalculate" Text="计算" OnClick="btnCalculate_Click" /> <asp:Label runat="server" ID="lblMsg"></asp:Label> </div> </form> </body> </html>
IFactory.cs:
interface IFactory
{
Operation CreateOpertion();
}
AddFactory.cs:
public class AddFactory : IFactory
{
public Operation CreateOpertion()
{
return new OperateAdd();
}
}
SubFactory.cs:
public class SubFactory : IFactory
{
public Operation CreateOpertion()
{
return new OperateSub();
}
}
MulFactory.cs:
public class MulFactory : IFactory
{
public Operation CreateOpertion()
{
return new OperateMul();
}
}
DivFactory.cs:
public class DivFactory : IFactory
{
public Operation CreateOpertion()
{
return new OperateDiv();
}
}
Operation.cs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Operation { public int NumberA { get; set; } public int NumberB { get; set; } public virtual int GetResult() { return 0; } }
OperateAdd.cs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OperateAdd : Operation { public override int GetResult() { return NumberA + NumberB; } }
OperateSub.cs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OperateSub : Operation { public override int GetResult() { return NumberA - NumberB; } }
OperateMul.cs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OperateMul : Operation { public override int GetResult() { return NumberA * NumberB; } }
OperateDiv.cs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class OperateDiv : Operation { public override int GetResult() { int result = 0; if (NumberB == 0) throw new Exception("被除数为0!"); result = NumberA / NumberB; return result; } }
客户端代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void btnCalculate_Click(object sender, EventArgs e) { IFactory factory = null; string type = ddlType.SelectedValue; switch (type) { case "+": factory = new AddFactory(); break; case "-": factory = new SubFactory(); break; case "*": factory = new MulFactory(); break; case "/": factory = new DivFactory(); break; } try { Operation operation = factory.CreateOpertion(); operation.NumberA = Convert.ToInt32(txtNumberA.Text); operation.NumberB = Convert.ToInt32(txtNumberB.Text); txtResult.Text = operation.GetResult().ToString(); } catch (Exception ex) { lblMsg.Text = ex.Message; } }
简单工厂模式和工厂方法模式比较:
简单工厂:最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相应的类,对于客户端来说,
去除了与具体产品的依赖。(违反了开闭原则)
工厂方法:将必要的判断逻辑放在客户端,这样在增加新的产品类时只需要增加相应的工厂类,而不需要修改原来的类,遵
守了开闭原则。