定义:根据提供的数据或参数返回几种可能类中的一种。 

示例:实现计算器功能,要求输入两个数和运算符号,得到结果。 

结构图

设计模式:简单工厂(Simple Factory)

HTML: 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Simple Factory Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtNumA" runat="server" Width="50px"></asp:TextBox>
        <asp:DropDownList ID="ddlOperateType" runat="server">
            <asp:ListItem Selected="True">+</asp:ListItem>
            <asp:ListItem>-</asp:ListItem>
            <asp:ListItem>*</asp:ListItem>
            <asp:ListItem>/</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txtNumB" runat="server" Width="50px"></asp:TextBox>
        <asp:Button ID="btnGetResult" runat="server" Text="=" OnClick="btnGetResult_Click" />
        <asp:TextBox ID="txtResult" runat="server" Width="50px"></asp:TextBox>
        <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
    </div>
    </form>
</body>
</html>
View Code

相关文章: