public abstract class AbstractLoan {//贷款抽象类abstract void processLoan();}public class PersonalLoan extends AbstractLoan {//个人贷款实现类 @Overridepublic void processLoan() {this.checkEligibility();this.fillOutApplication();this.approveLoan();this.submitDocuments(); }private void checkEligibility() { System.out.println("Checking eligibility criteria for personal loan"); }private void fillOutApplication() { System.out.println("Filling out loan application form"); }private void approveLoan() { System.out.println("Personal loan approved"); }private void submitDocuments() { System.out.println("Submitting required documents for verification"); }}public class BusinessLoan extends AbstractLoan{//企业贷款实现类 @Overridepublic void processLoan() {this.checkEligibility();this.fillOutApplication();this.approveLoan();this.submitDocuments(); }private void checkEligibility() { System.out.println("Checking eligibility criteria for business loan"); }private void fillOutApplication() { System.out.println("Filling out loan application form"); }private void approveLoan() { System.out.println("Business loan approved"); }private void submitDocuments() { System.out.println("Submitting required documents for verification"); }}public class Client {//调用方代码public static void main(String[] args) {// 提交个人贷款申请 System.out.println("\nProcessing Personal Loan Application:"); AbstractLoan personal = new PersonalLoan(); personal.processLoan();//提交企业贷款申请 System.out.println("\nProcessing Business Loan Application:"); AbstractLoan business = new BusinessLoan(); business.processLoan(); }}
在现实中贷款这种业务,不论是个人贷款还是企业贷款,通常都会制定明确的步骤。上面的代码实现中,主要问题在于抽象类并未将现实中的贷款步骤体现出来,导致在子类中可以随意发挥,造成代码与现实情况的割裂,代码的可读性和可维护性变差。结构模板方法模式的示例代码实现如下。public abstract class AbstractClass {// 模板方法public final void templateMethod() { PrimitiveOperation1(); PrimitiveOperation2();// ... 其他步骤 ... }// 抽象方法,由子类实现protected abstract void PrimitiveOperation1();// 抽象方法,由子类实现protected abstract void PrimitiveOperation2();}public class ConcreteClassA extends AbstractClass{@Overrideprotected void PrimitiveOperation1() { System.out.println("ConcreteClassA 执行步骤 1"); }@Overrideprotected void PrimitiveOperation2() { System.out.println("ConcreteClassA 执行步骤 2"); }}public class ConcreteClassB extends AbstractClass{@Overrideprotected void PrimitiveOperation1() { System.out.println("ConcreteClassB 执行步骤 1"); }@Overrideprotected void PrimitiveOperation2() { System.out.println("ConcreteClassB 执行步骤 2"); }}public class Client {public static void main(String[] args) { AbstractClass abstractClassA = new ConcreteClassA(); abstractClassA.templateMethod(); // 输出:ConcreteClassA 执行步骤 1, ConcreteClassA 执行步骤 2 AbstractClass abstractClassB = new ConcreteClassB(); abstractClassB.templateMethod(); // 输出:ConcreteClassB 执行步骤 1, ConcreteClassB 执行步骤 2 }}
通过模板方法结构和示例代码,可以发现AbstractClass中定义了一个算法的步骤框架结构,实现类按此结构编写即可。模式应用后案例上面的贷款案例,在应用模板方法模式之后的代码实现如下。public abstract class AbstractLoan {//贷款抽象类public final void processLoan() {this.checkEligibility();//1-检查资格this.fillOutApplication();//2-填写申请表this.submitDocuments();//3-提交文件this.approveLoan();//4-批准贷款 }protected abstract void checkEligibility();protected void fillOutApplication() { System.out.println("Filling out loan application form"); }protected void submitDocuments() { System.out.println("Submitting required documents for verification"); }protected abstract void approveLoan();}public class PersonalLoan extends AbstractLoan {//个人贷款实现类@Overrideprotected void checkEligibility() { System.out.println("Checking eligibility criteria for personal loan"); }@Overrideprotected void approveLoan() { System.out.println("Personal loan approved"); }}public class BusinessLoan extends AbstractLoan {//企业贷款实现类@Overrideprotected void checkEligibility() { System.out.println("Checking eligibility criteria for business loan"); }@Overrideprotected void approveLoan(){ System.out.println("Business loan approved"); }}public class Client {//调用方代码public static void main(String[] args) {// 提交个人贷款申请 System.out.println("\nProcessing Personal Loan Application:"); AbstractLoan personal = new PersonalLoan(); personal.processLoan();//提交企业贷款申请 System.out.println("\nProcessing Business Loan Application:"); AbstractLoan business = new BusinessLoan(); business.processLoan(); }}
在上面的代码实现中,在贷款抽象类中明确定义了实现的步骤,这样个人贷款和企业贷款实现类都遵循一样的代码结构即可,代码非常简洁清晰。并且,对于个人贷款和企业贷款可以复用的逻辑,也可以提取到抽象类中实现,代码复用性变好。适用场景模板方法模式适用于以下场景:1、当多个类中共享相同的算法框架或流程,并且只需通过继承和重写来改变特定步骤时;2、当有一系列算法步骤,但其中某些步骤可能会根据具体情况而变化时,可以使用模板方法模式。将不变的部分抽象成父类的模板方法,而可变部分由子类实现;3、模板方法常见于各种软件开发场景中如Junit等框架设计、工作流引擎等。模式可能存在的困惑困惑1:模板方法模式和策略模式似乎都涉及算法,两者之间有何不同?两者相同的部分在于算法部分都使用一个算法家族类实现。然而,模板方法关注的是如何组织整个算法结构,子类只需重写部分特定步骤即可。策略模式则关注于封装各种不同的算法并使之可以互相替换,子类中实现的是完整的算法。本质在现实场景中,很多事情都是有明确的步骤。对于编程来说,代码要尽可能体现现实中问题的结构。模板方法模式是针对此类需求的一种良好解决机制。(图片来源网络,侵删)
0 评论