夜光序言:
人生如果错了方向,停下就是进步,倘若执迷不悟,一味固执,不光是浪费了自己的时候,更会自食其果,苦不堪言。
正文:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 案例分析
{
/// <summary>
/// 定义一个抽象类 Operation
/// </summary>
abstract class Operation
{
//我们先定义两个变量
public float strNumberA;
public float strNumberB;
public abstract float GetResult();
}
//派生一个加法
class AddOp : Operation
{
public override float GetResult()
{
return strNumberA + strNumberB;
}
}
//派生一个减法
class SubOp : Operation
{
public override float GetResult()
{
return strNumberA - strNumberB;
}
}
//派生一个乘法
class MulOp : Operation
{
public override float GetResult()
{
return strNumberA * strNumberB;
}
}
//派生一个除法
class DivOp : Operation
{
public override float GetResult()
{
//除法,会有一个除以0的特殊情况
if (strNumberB == 0)
throw new DivideByZeroException();
return strNumberA / strNumberB;
}
}
//这边定义一个抽象工厂
class OperationFactory
{
public static Operation CreateOperation(string op)
{
switch (op)
{
case "+":
return new AddOp();
case "-":
return new SubOp();
case "*":
return new MulOp();
case "/":
return new DivOp();
default:
throw new ArgumentOutOfRangeException(); //抛出一个异常
/*return new Operation(); //这个抽象类 new不出来*/
}
}
}
class Program
{
static void Main(string[] args)
{
float numberA, numberB, result = 0; //结果result给一个初始值0
string sop;
Console.Write("a=");
numberA = float.Parse(Console.ReadLine());
Console.Write("b=");
numberB = float.Parse(Console.ReadLine());
Console.Write("op= (+、-、*、/)");
sop = Console.ReadLine();
Operation op = OperationFactory.CreateOperation(sop);
op.strNumberA = numberA;
op.strNumberB = numberB;
result = op.GetResult();
Console.WriteLine("result={0}", result);
Console.ReadLine();
}
}
}
单一职责原则
开放封闭原则
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 案例分析
{
//抽象工厂模式,更加牛逼
/// <summary>
/// 定义一个抽象类 Operation
/// </summary>
abstract class Operation
{
//我们先定义两个变量
public float strNumberA;
public float strNumberB;
public abstract float GetResult();
}
//派生一个加法
class AddOp : Operation
{
public override float GetResult()
{
return strNumberA + strNumberB;
}
}
//派生一个减法
class SubOp : Operation
{
public override float GetResult()
{
return strNumberA - strNumberB;
}
}
//派生一个乘法
class MulOp : Operation
{
public override float GetResult()
{
return strNumberA * strNumberB;
}
}
//派生一个除法
class DivOp : Operation
{
public override float GetResult()
{
//除法,会有一个除以0的特殊情况
if (strNumberB == 0)
throw new DivideByZeroException();
return strNumberA / strNumberB;
}
}
/// <summary>
/// mod运算
/// </summary>
class ModOp : Operation
{
public override float GetResult()
{
//除法,会有一个除以0的特殊情况
if (strNumberB == 0)
throw new DivideByZeroException();
return strNumberA % strNumberB;
}
}
//这边定义一个抽象工厂
class OperationFactory
{
public static Operation CreateOperation(string op)
{
switch (op)
{
case "+":
return new AddOp();
case "-":
return new SubOp();
case "*":
return new MulOp();
case "/":
return new DivOp();
case "%":
return new ModOp();
default:
throw new ArgumentOutOfRangeException(); //抛出一个异常
/*return new Operation(); //这个抽象类 new不出来*/
}
}
}
//我们写一个抽象工厂
abstract class IOperationFactory
{
public abstract Operation CreateOperation();
}
/// <summary>
/// 加法
/// </summary>
class AddFactory : IOperationFactory //继承一下
{
public override Operation CreateOperation()
{
return new AddOp();
}
}
/// <summary>
/// 减法
/// </summary>
class SubFactory : IOperationFactory
{
public override Operation CreateOperation()
{
return new SubOp();
}
}
/// <summary>
/// 乘法
/// </summary>
class MulFactory : IOperationFactory
{
public override Operation CreateOperation()
{
return new MulOp();
}
}
/// <summary>
/// 除法
/// </summary>
class DivFactory : IOperationFactory
{
public override Operation CreateOperation()
{
return new DivOp();
}
}
class Program
{
//在这下面的都是客户代码
static void Main(string[] args)
{
float numberA, numberB, result = 0; //结果result给一个初始值0
string sop;
Console.Write("a=");
numberA = float.Parse(Console.ReadLine());
Console.Write("b=");
numberB = float.Parse(Console.ReadLine());
Console.Write("op= (+、-、*、/)");
sop = Console.ReadLine();
#region 简单工厂模式,夜光
/* try
{
Operation op = OperationFactory.CreateOperation(sop);
op.strNumberA = numberA;
op.strNumberB = numberB;
result = op.GetResult();
}
catch(Exception e)
{
Console.WriteLine("报错信息:" + e.Message);
}*/
#endregion
#region 抽象工厂模式 夜光
try
{
//我们调用一下抽象工厂
IOperationFactory factory = null;
switch (sop)
{
case "+":
factory = new AddFactory();
break;
case "-":
factory = new SubFactory();
break;
case "*":
factory = new MulFactory();
break;
case "/":
factory = new DivFactory();
break;
}
Operation op = factory.CreateOperation(); // → new AddOp
op.strNumberA = numberA;
op.strNumberB = numberB;
result = op.GetResult();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
#endregion
Console.WriteLine("result={0}", result);
Console.ReadLine();
}
}
}