大话设计-简单工厂
程序员文章站
2022-06-19 10:29:09
记录大话设计模式学习,大话设计模式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(); } } } }