ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 模板方法模式深度解析(二) 3 模板方法模式应用实例 下面通过一个应用实例来进一步学习和理解模板方法模式。 1. 实例说明 某软件公司欲为某银行的业务支撑系统开发一个利息计算模块,利息计算流程如下: (1) 系统根据账号和密码验证用户信息,如果用户信息错误,系统显示出错提示; (2) 如果用户信息正确,则根据用户类型的不同使用不同的利息计算公式计算利息(如活期账户和定期账户具有不同的利息计算公式); (3) 系统显示利息。 试使用模板方法模式设计该利息计算模块。 2. 实例类图 通过分析,本实例结构图如图3所示。 ![](http://img.my.csdn.net/uploads/201212/15/1355577976_2992.jpg) 图3 银行利息计算模块结构图 在图3中,Account充当抽象类角色,CurrentAccount和SavingAccount充当具体子类角色。 3. 实例代码 (1) Account:账户类,充当抽象类。 ``` //Account.cs using System; namespace TemplateMethodSample { abstract class Account { //基本方法——具体方法 public bool Validate(string account, string password) { Console.WriteLine("账号:{0}", account); Console.WriteLine("密码:{0}", password); //模拟登录 if (account.Equals("张无忌") && password.Equals("123456")) { return true; } else { return false; } } //基本方法——抽象方法 public abstract void CalculateInterest(); //基本方法——具体方法 public void Display() { Console.WriteLine("显示利息!"); } //模板方法 public void Handle(string account, string password) { if (!Validate(account,password)) { Console.WriteLine("账户或密码错误!"); return; } CalculateInterest(); Display(); } } } ``` (2) CurrentAccount:活期账户类,充当具体子类。 ``` //CurrentAccount.cs using System; namespace TemplateMethodSample { class CurrentAccount : Account { //覆盖父类的抽象基本方法 public override void CalculateInterest() { Console.WriteLine("按活期利率计算利息!"); } } } ``` (3) SavingAccount:定期账户类,充当具体子类。 ``` //SavingAccount.cs using System; namespace TemplateMethodSample { class SavingAccount : Account { //覆盖父类的抽象基本方法 public override void CalculateInterest() { Console.WriteLine("按定期利率计算利息!"); } } } ``` (4) 配置文件App.config,在配置文件中存储了具体子类的类名。 ``` <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="subClass" value="TemplateMethodSample.CurrentAccount"/> </appSettings> </configuration> ``` (5) Program:客户端测试类 ``` //Program.cs using System; using System.Configuration; using System.Reflection; namespace TemplateMethodSample { class Program { static void Main(string[] args) { Account account; //读取配置文件 string subClassStr = ConfigurationManager.AppSettings["subClass"]; //反射生成对象 account = (Account)Assembly.Load("TemplateMethodSample").CreateInstance(subClassStr); account.Handle("张无忌", "123456"); Console.Read(); } } } ``` 4. 结果及分析 编译并运行程序,输出结果如下: ``` 账号:张无忌 密码:123456 按活期利率计算利息! 显示利息! ``` 如果需要更换具体子类,无须修改源代码,只需修改配置文件App.config,例如将活期账户(CurrentAccount)改为定期账户(Saving Account),只需将存储在配置文件中的具体子类CurrentAccount改为SavingAccount,如下代码所示: ``` <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="subClass" value="TemplateMethodSample.SavingAccount"/> </appSettings> </configuration> ``` 重新运行客户端程序,输出结果如下: ``` 账号:张无忌 密码:123456 按定期利率计算利息! 显示利息! ``` > 如果需要增加新的具体子类(新的账户类型),原有代码均无须修改,完全符合开闭原则。