定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类。
工厂方法使一个类的实例化延迟到其子类。
结构图:
示例:
HTML代码:
<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>