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

  • Spring 5 Design Patterns
  • Dinesh Rajput
  • 335字
  • 2021-07-08 09:59:38

Sample implementation of the Composite design pattern

In the following example, I am implementing an Account interface, which can be either a SavingAccount and CurrentAccount or a composition of several accounts. I have a CompositeBankAccount class, which acts as a composite pattern actor class. Let's look at the following code for this example.

Create an Account interface that will be treated as a component:

    public interface Account { 
      void accountType(); 
    } 

Create a SavingAccount class and CurrentAccount class as an implementation of the component and that will also be treated as a leaf:

Following is the SavingAccount.java file:

    public class SavingAccount implements Account{ 
      @Override 
      public void accountType() { 
        System.out.println("SAVING ACCOUNT"); 
      } 
    } 

Following is the CurrentAccount.java file:

    public class CurrentAccount implements Account { 
      @Override 
      public void accountType() { 
         System.out.println("CURRENT ACCOUNT"); 
      } 
    } 

Create a CompositeBankAccount class that will be treated as a Composite and implements the Account interface:

Following is the CompositeBankAccount.java file:

     package com.packt.patterninspring.chapter3.composite.pattern; 
     import java.util.ArrayList; 
     import java.util.List; 
     import com.packt.patterninspring.chapter3.model.Account; 
     public class CompositeBankAccount implements Account { 
       //Collection of child accounts. 
       private List<Account> childAccounts = new ArrayList<Account>(); 
       @Override 
       public void accountType() { 
         for (Account account : childAccounts) { 
               account.accountType(); 
         } 
       } 
       //Adds the account to the composition. 
          public void add(Account account) { 
            childAccounts.add(account); 
          } 
          //Removes the account from the composition. 
          public void remove(Account account) { 
            childAccounts.remove(account); 
         } 
       } 

Create a CompositePatternMain class that will also be treated as a Client:

Following is the CompositePatternMain.java file:

    package com.packt.patterninspring.chapter3.composite.pattern; 
    import com.packt.patterninspring.chapter3.model.CurrentAccount; 
    import com.packt.patterninspring.chapter3.model.SavingAccount; 
    public class CompositePatternMain { 
      public static void main(String[] args) { 
         //Saving Accounts 
         SavingAccount savingAccount1 = new SavingAccount(); 
         SavingAccount savingAccount2 = new SavingAccount(); 
         //Current Account 
         CurrentAccount currentAccount1 = new CurrentAccount(); 
         CurrentAccount currentAccount2 = new CurrentAccount(); 
         //Composite Bank Account 
         CompositeBankAccount compositeBankAccount1 = new
CompositeBankAccount(); CompositeBankAccount compositeBankAccount2 = new
CompositeBankAccount(); CompositeBankAccount compositeBankAccount = new
CompositeBankAccount(); //Composing the bank accounts compositeBankAccount1.add(savingAccount1); compositeBankAccount1.add(currentAccount1); compositeBankAccount2.add(currentAccount2); compositeBankAccount2.add(savingAccount2); compositeBankAccount.add(compositeBankAccount2); compositeBankAccount.add(compositeBankAccount1); compositeBankAccount.accountType(); } }

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

Now that we have discussed the composite design pattern, let's turn to the decorator design pattern.

主站蜘蛛池模板: 望都县| 吉林市| 云林县| 郎溪县| 调兵山市| 福建省| 保康县| 舞阳县| 康保县| 五家渠市| 北流市| 通州区| 林西县| 丹棱县| 莲花县| 龙南县| 乳山市| 临城县| 鱼台县| 江都市| 尚志市| 巴林左旗| 边坝县| 昌黎县| 江西省| 阳曲县| 绥芬河市| 屏东市| 喀喇| 虞城县| 文昌市| 五大连池市| 徐汇区| 屏南县| 开化县| 齐齐哈尔市| 友谊县| 南丰县| 周口市| 高邑县| 南通市|