官术网_书友最值得收藏!

  • Spring 5 Design Patterns
  • Dinesh Rajput
  • 401字
  • 2021-07-08 09:59:39

Implementing the Decorator pattern

Let's look at the following code to demonstrate the Decorator design pattern.

Create a component class:

Following is the Account.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public interface Account { 
       String getTotalBenefits(); 
    } 

Create concrete components classes:

Following is the SavingAccount.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public class SavingAccount implements Account { 
      @Override 
      public String getTotalBenefits() { 
         return "This account has 4% interest rate with per day
$5000 withdrawal limit"; } }

Let's create another concrete class for Account component:

Following is the CurrentAccount.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public class CurrentAccount implements Account { 
      @Override 
      public String getTotalBenefits() { 
         return "There is no withdrawal limit for current account"; 
      } 
    } 

Let's create a Decorator class for Account component. This decorator class apply other run time behavior to the Account component classes.

Following is the AccountDecorator.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public abstract class AccountDecorator implements Account { 
      abstract String applyOtherBenefits(); 
    } 

Let's create a ConcreteDecorator class to implement the AccountDecorator class. Following class SeniorCitizen is extended AccountDecorator class to access other run time behavior such as applyOtherBenefits().

Following is the SeniorCitizen.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public class SeniorCitizen extends AccountDecorator { 
      Account account; 
      public SeniorCitizen(Account account) { 
         super(); 
         this.account = account; 
      } 
      public String getTotalBenefits() { 
         return account.getTotalBenefits() + " other benefits are 
"+applyOtherBenefits(); } String applyOtherBenefits() { return " an medical insurance of up to $1,000 for Senior
Citizen"; } }

Let's create another ConcreteDecorator class to implement the AccountDecorator class. Following class Privilege is extended AccountDecorator class to access other run time behavior such as applyOtherBenefits().

Following is the Privilege.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public class Privilege extends AccountDecorator { 
      Account account; 
      public Privilege(Account account) { 
         this.account = account; 
      } 
      public String getTotalBenefits() { 
         return account.getTotalBenefits() + " other benefits are    
"+applyOtherBenefits(); } String applyOtherBenefits() { return " an accident insurance of up to $1,600 and
an overdraft facility of $84"; } }

Let's now write some test code to see how the Decorator pattern works at runtime:

Following is the DecoratorPatternMain.java file:

    package com.packt.patterninspring.chapter3.decorator.pattern; 
    public class DecoratorPatternMain { 
      public static void main(String[] args) { 
         /*Saving account with no decoration*/ 
         Account basicSavingAccount = new SavingAccount(); 
         System.out.println(basicSavingAccount.getTotalBenefits()); 
         /*Saving account with senior citizen benefits decoration*/ 
         Account seniorCitizenSavingAccount = new SavingAccount(); 
         seniorCitizenSavingAccount = new 
SeniorCitizen(seniorCitizenSavingAccount); System.out.println
(seniorCitizenSavingAccount.getTotalBenefits()); /*Saving account with privilege decoration*/ Account privilegeCitizenSavingAccount = new SavingAccount(); privilegeCitizenSavingAccount = new
Privilege(privilegeCitizenSavingAccount); System.out.println
(privilegeCitizenSavingAccount.getTotalBenefits()); } }

Let's run this demo class and see the following output at the console:

主站蜘蛛池模板: 建阳市| 裕民县| 高要市| 台东市| 田阳县| 海晏县| 嘉鱼县| 张北县| 桐庐县| 鞍山市| 庄河市| 乳山市| 满城县| 东阳市| 乐东| 临邑县| 永年县| 盈江县| 古丈县| 武陟县| 河曲县| 米脂县| 长海县| 麻江县| 阿巴嘎旗| 驻马店市| 安岳县| 庆阳市| 运城市| 隆尧县| 成安县| 高邑县| 新源县| 弥渡县| 吐鲁番市| 大理市| 封开县| 汕尾市| 莲花县| 都安| 澄城县|