1.简单工厂模式
简单工厂模式的一个简单就是,有几个子类负责工作,一个工厂类负责生成子类的对象。
class Program
{
static void Main(string[] args)
{
Operation oper = OperationFactory.CreateOperation(OperationType.OperAdd);
oper.NumberA = 25;
oper.NumberB = 77;
Console.WriteLine(oper.GetResult().ToString());
Console.ReadLine();
}
}
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 OperateionAdd:Operation
{
public override double GetResult()
{
return base.NumberA + base.NumberB;
}
}
class OperationSub:Operation
{
public override double GetResult()
{
return base.NumberA - base.NumberB;
}
}
class OperationMul:Operation
{
public override double GetResult()
{
return base.NumberA * base.NumberB;
}
}
class OperateionDiv:Operation
{
public override double GetResult()
{
double result=0;
if ( base.NumberB == 0 )
{
throw new Exception("被零除错误!");
}
else
{
result = base.NumberA / base.NumberB;
}
return result;
}
}
//运算类的工厂,返回一个运算类的对象,就是创建对象的地方。
class OperationFactory
{
public static Operation CreateOperation(OperationType OType)
{
switch (OType)
{
case OperationType.OperAdd:
return new OperateionAdd();
break;
case OperationType.OperSub:
return new OperationSub();
break;
case OperationType.OperMul:
return new OperationMul();
break;
case OperationType.OperDiv:
return new OperateionDiv();
break;
default:
return new OperateionAdd();
break;
}
}
}
public enum OperationType
{
OperAdd,
OperSub,
OperMul,
OperDiv,
}
{
static void Main(string[] args)
{
Operation oper = OperationFactory.CreateOperation(OperationType.OperAdd);
oper.NumberA = 25;
oper.NumberB = 77;
Console.WriteLine(oper.GetResult().ToString());
Console.ReadLine();
}
}
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 OperateionAdd:Operation
{
public override double GetResult()
{
return base.NumberA + base.NumberB;
}
}
class OperationSub:Operation
{
public override double GetResult()
{
return base.NumberA - base.NumberB;
}
}
class OperationMul:Operation
{
public override double GetResult()
{
return base.NumberA * base.NumberB;
}
}
class OperateionDiv:Operation
{
public override double GetResult()
{
double result=0;
if ( base.NumberB == 0 )
{
throw new Exception("被零除错误!");
}
else
{
result = base.NumberA / base.NumberB;
}
return result;
}
}
//运算类的工厂,返回一个运算类的对象,就是创建对象的地方。
class OperationFactory
{
public static Operation CreateOperation(OperationType OType)
{
switch (OType)
{
case OperationType.OperAdd:
return new OperateionAdd();
break;
case OperationType.OperSub:
return new OperationSub();
break;
case OperationType.OperMul:
return new OperationMul();
break;
case OperationType.OperDiv:
return new OperateionDiv();
break;
default:
return new OperateionAdd();
break;
}
}
}
public enum OperationType
{
OperAdd,
OperSub,
OperMul,
OperDiv,
}
书上有一个例子,用于判断采用何种运种,是使用的"+","-"等符号来判断的,传进来运算符我觉得很奇怪,所以改成了enum,这样比原来好些吗还是不好?
不知道了,知道的说下。