设计模式:模板方法
下面通过代码来模拟一个去银行查询的业务场景。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | public class BankClient { /// <summary> /// 查询 /// </summary> public void Query( int id, string name, string password) { if (CheckUser(id, password)) { double balance = this .QueryBanlance(id); double interest = this .CalculateInterest(balance); this .Show(name,balance,interest); } else { Console.WriteLine( "账号密码错误!" ); } } /// <summary> /// 验证用户 /// </summary> public bool CheckUser( int id, string password) { return true ; } /// <summary> /// 查询余额 /// </summary> public double QueryBanlance( int id) { return new Random().Next(1000,100000); } /// <summary> /// 计算利息 /// </summary> public double CalculateInterest( double balance) { return balance * 0.005; } public void Show( string name, double balance, double interest) { Console.WriteLine($ "尊敬的 {name} 客户,您的账户余额为:{balance} ,利息为:{interest}" ); } } |
上面的代码把整个业务写一个类里面,各种逻辑写成方法。这只是在理想状态下,大家都知道银行的利息计算方法分为:活期和定期,并且他们的利率不同。那么,一般做法就是在CalculateInterest方法中加if判断来处理,如果Show方法也要根据存款类型来输出提示信息,像这样在多个方法中频繁的出现相同逻辑的分支,我们的代码就不应该这么写了,因为不同行为应该分开写。
修改后的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | public abstract class AbstractClient { /// <summary> /// 查询 /// </summary> public void Query( int id, string name, string password) { if (CheckUser(id, password)) { double balance = this .QueryBanlance(id); double interest = this .CalculateInterest(balance); this .Show(name, balance, interest); } else { Console.WriteLine( "账号密码错误!" ); } } /// <summary> /// 验证用户 /// </summary> public bool CheckUser( int id, string password) { return true ; } /// <summary> /// 查询余额 /// </summary> public double QueryBanlance( int id) { return new Random().Next(1000, 100000); } /// <summary> /// 活期/定期 ///选择抽象方法的原因: 不同的子类,一定不同,而且不同 /// </summary> public abstract double CalculateInterest( double balance); //在不同的子类中,大部分相同,个别不同的,这种用虚方法 public virtual void Show( string name, double balance, double interest) { Console.WriteLine($ "尊敬的 {name} 客户,您的账户余额为:{balance} ,利息为:{interest}" ); } } public class ClientRegular:AbstractClient { /// <summary> /// 定期利率计算方法 /// </summary> public override double CalculateInterest( double balance) { return balance * 0.003; } } public class ClientCurrent : AbstractClient { /// <summary> /// 活期利率计算方法 /// </summary> public override double CalculateInterest( double balance) { return balance * 0.002; } } internal class Program { static void Main( string [] args) { AbstractClient client = new ClientCurrent(); client.Query(123, "谭勇君" , "123456" ); Console.ReadKey(); } } |
如上就是模板模式,它就是通过一个抽象类和不同的子类来解决问题,AbstractClient类就相当于模板。
模板模式主要是应对一个业务处理中有多个步骤,然后这些步骤中的某个步骤可能出现不同的情况,我们就把这些不同的步骤写到不同的子类,把固定不变的写到父类。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了