Design Patterns Decorator Pattern

Decorator Pattern

   Without altering its structure, it is possible to dynamically extend the functionality .

   Between the decorator and the decoration must be the same type, that is to have a common superclass .

   Here application implementation inheritance is not a copy, but to achieve the type of match .


Abstract component (Component) Roles:

define an abstract interface to regulate target is ready to receive additional responsibilities.
Specific components (Concrete Component) role:
to achieve abstract components, add some of its responsibilities by decorative role.
Abstract decoration (Decorator) role:
responsibility is to our members decorative objects, there must be a pointer to an internal component object reference
specific decoration (ConcreteDecorator) Role:
related methods abstract decoration implementation, and add additional responsibilities to specific member objects.
1  // abstract construct 
2  interface the Person {
 . 3      void EAT ();
 . 4  }
 . 5  
. 6  // specially constructed 
. 7  class Person_one the implements the Person {
 . 8  
. 9      @Override
 10      public  void EAT () {
 . 11          System.out.println ( "eat large fish " );
 12 is      }
 13 is  }
 14  
15  class Person_two the implements the Person {
 16  
. 17      @Override
 18 is      public  voidEAT () {
 . 19          System.out.println ( "eat fish" );
 20      }
 21  }
 22 is  
23 is  // abstract decorator 
24  abstract  class newPerson the implements the Person {
 25      protected the Person per;
 26 is  
27      public newPerson (per the Person) {
 28          the this .per = per;
 29      }
 30  
31 is      public  void EAT () {
 32          per.eat ();
 33 is      }
 34 is  }
 35  
36  //DETAILED decorator 
37 [  class NewPerson_one the extends newPerson {
 38 is      public NewPerson_one (per the Person) {
 39          Super (per);
 40      }
 41 is  
42 is      void Dinner () {
 43 is          System.out.println ( "whale Luandun" );
 44      }
 45  
46 is      public  void EAT () {
 47          Super .eat ();
 48          Dinner ();
 49      }
 50 }

Guess you like

Origin www.cnblogs.com/loveer/p/11279219.html