File tree Expand file tree Collapse file tree 5 files changed +22
-6
lines changed 
src/main/java/com/iluwatar Expand file tree Collapse file tree 5 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 11package  com .iluwatar ;
22
3+ /** 
4+  *  
5+  * Decorator pattern is more flexible alternative to 
6+  * subclassing. The decorator class implements the same 
7+  * interface as the target and uses composition to 
8+  * "decorate" calls to the target. 
9+  * 
10+  */ 
311public  class  App  
412{
513    public  static  void  main ( String [] args  )
614    {
715
816    	System .out .println ("A simple looking troll approaches." );
9-     	Troll  troll  = new  Troll ();
17+     	Hostile  troll  = new  Troll ();
1018    	troll .attack ();
1119    	troll .fleeBattle ();
1220
1321    	System .out .println ("\n A smart looking troll surprises you." );
14-     	Troll  smart  = new  SmartTroll (new  Troll ());
22+     	Hostile  smart  = new  SmartTroll (new  Troll ());
1523    	smart .attack ();
1624    	smart .fleeBattle ();
1725    }
Original file line number Diff line number Diff line change 1+ package  com .iluwatar ;
2+ 
3+ public  interface  Hostile  {
4+ 
5+ 	void  attack ();
6+ 	void  fleeBattle ();
7+ 	
8+ }
Original file line number Diff line number Diff line change 11package  com .iluwatar ;
22
3- public  class  SmartTroll  extends   Troll  {
3+ public  class  SmartTroll  implements   Hostile  {
44
5- 	private  Troll  decorated ;
5+ 	private  Hostile  decorated ;
66
7- 	public  SmartTroll (Troll  decorated ) {
7+ 	public  SmartTroll (Hostile  decorated ) {
88		this .decorated  = decorated ;
99	}
1010
Original file line number Diff line number Diff line change 11package  com .iluwatar ;
22
3- public  class  Troll  {
3+ public  class  Troll  implements   Hostile   {
44
55	public  void  attack () {
66		System .out .println ("The troll swings at you with a club!" );
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments