您的位置:首页 > 其它

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

2014-02-08 15:06 543 查看
定义:根据提供的数据或参数返回几种可能类中的一种。

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

结构图

protected void btnGetResult_Click(object sender, EventArgs e)
{
lblMessage.Text = "";

try
{
Operation opeation;
opeation = OperationFactory.CreateOperate(ddlOperateType.SelectedValue);
opeation.NumberA = Convert.ToDouble(txtNumA.Text);
opeation.NumberB = Convert.ToDouble(txtNumB.Text);

double result = opeation.GetResult();

txtResult.Text = result.ToString();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: