首页 > 代码库 > 简单工厂模式c#
简单工厂模式c#
简单工厂模式c#上简单例子
namespace simplefactory
{public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)//用户层次
{
Operation operation = OperationFactory.createOperate(this.cbbOperation.SelectedIndex);//连接到简单工厂获取算法
operation.NumberA = Convert.ToDouble(this.txtNumberOne.Text);//对简单工厂得来的算法进行赋值
operation.NumberB = Convert.ToDouble(this.txtNumberTwo.Text);
this.txtResult.Text = operation.getResult().ToString();//获取算法结果
}
}class OperationFactory//简单工厂层次
{
public static Operation createOperate(int operate)//工厂内分加法和减法两种
{
Operation oper = null;
switch (operate)
{
case 0:
{
oper = new OperationAdd();//加法
break;
}
case 1:
{
oper = new OperationSub();//减法
break;
}
}
return oper;
}
}
public abstract class Operation//对加减法算法时行抽象,以方便统一赋值NumberA、NumberB及计算结果getResult()
{
public double NumberA { get; set; }
public double NumberB { get; set; }
public abstract double getResult();
}
class OperationAdd : Operation//加法算法继承抽象算法
{
public override double getResult()
{
return NumberA + NumberB;
}
}
class OperationSub : Operation//减法算法继承抽象算法
{
public override double getResult()
{
return NumberA - NumberB;
}
}
}
简单工厂模式c#
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。