記錄大話設計模式學習,大話設計模式pdf分享:鏈接:https://pan.baidu.com/s/11h9x-4RffXydf3GFH5_y7g 提取碼:x5c3 實現簡單計算器的加減乘除運算,做到可維護、可擴展、可復用、且靈活。 可維護舉例:業務人員要求加法功能只允許絕對值相加,這時修改代碼的區 ...
記錄大話設計模式學習,大話設計模式pdf分享:鏈接:https://pan.baidu.com/s/11h9x-4RffXydf3GFH5_y7g 提取碼:x5c3
實現簡單計算器的加減乘除運算,做到可維護、可擴展、可復用、且靈活。
可維護舉例:業務人員要求加法功能只允許絕對值相加,這時修改代碼的區域應只有加法,附近沒有其他運算,避免不小心改到了別的運算。
可擴展舉例:加個開平方根運算,添加代碼的區域附近沒有其他運算。
這個簡單工廠用於返回運算的實例、每當有新的運算加入,工廠里的switch分支也要添加。
using System; namespace ConsoleApp4 { class Program { public static void Main(string[] args) { var operation = OperationFactory.GetOperation("+"); operation.NumberA = Convert.ToDouble(Console.ReadLine()); operation.NumberB = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(operation.GetResult()); Console.ReadLine(); } } //運算基類 public class Operation { private double numberA; public double NumberA { get { return numberA; } set { numberA = value; } } private double numberB; public double NumberB { get { return numberB; } set { numberB = value; } } public virtual double GetResult() { double result = 0; return result; } } public class OperationAdd : Operation { public override double GetResult() { return NumberA + NumberB; } } public class OperationSub : Operation { public override double GetResult() { return NumberA - NumberB; } } public class OperationMul : Operation { public override double GetResult() { return NumberA * NumberB; } } public class OperationDiv : Operation { public override double GetResult() { if (NumberB == 0) { throw new DivideByZeroException(); } return NumberA / NumberB; } } // 簡單工廠 public class OperationFactory { public static Operation GetOperation(string operation) { switch (operation) { case "+":return new OperationAdd(); case "-":return new OperationSub(); case "*":return new OperationMul(); case "/":return new OperationDiv(); default:return new Operation(); } } } }
超市結賬優惠,打折或滿減。
using System; namespace ConsoleApp4 { class Program { public static void Main(string[] args) { var cashSuper = CashFactory.CreateCashSuper("8折"); cashSuper.AcceptCash(900d); } } public abstract class CashSuper { public abstract double AcceptCash(double money); } public class CashNormal : CashSuper { public override double AcceptCash(double money) { return money; } } public class CashRebate : CashSuper { private double rebate; public CashRebate(double rebate) { this.rebate = rebate; } public override double AcceptCash(double money) { return money * rebate; } } public class CashReturn : CashSuper { private double moneyCondition; private double moneyReturn; public CashReturn(double condition,double @return) { moneyCondition = condition; moneyReturn = @return; } public override double AcceptCash(double money) { if (money >= moneyCondition) { return money - ((money / moneyCondition) * moneyReturn); } else { return money; } } } public class CashFactory { public static CashSuper CreateCashSuper(string type) { switch (type) { case "滿300減50":return new CashReturn(300d, 50d); case "8折":return new CashRebate(0.8d); default:return new CashNormal(); } } } }