csharp 大话设计模式:简单工厂模式(C#版)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 大话设计模式:简单工厂模式(C#版)相关的知识,希望对你有一定的参考价值。
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
public virtual double GetResult()
{
double result = 0;
return result;
}
}
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB ==0):
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}
public class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new operationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new operationDiv();
break;
}
return oper;
}
}
/// 客户端代码
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
Opera
以上是关于csharp 大话设计模式:简单工厂模式(C#版)的主要内容,如果未能解决你的问题,请参考以下文章
简单工厂模式--(大话设计模式大鸟和小菜Java版)
大话设计模式-抽象工厂模式(大鸟和小菜Java版)
大话设计模式--简单工厂模式
大话设计模式-工厂方法模式(大鸟和小菜java版)
大话设计模式——简单工厂模式
大话设计模式之简单工厂模式