Application Scenarios for Adorner mode:
1, need to extend the functionality of a class.
2, dynamic for an object to add functionality, but also can be dynamically revoked. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )
Source interface:
1 Public Interface sourceable {23 void method (); 4 }
SOURCE class:
1 Public class Implements sourceable {2 @Override3public void Method () {4 System.out.println ("Source.method () ..."); 5 }6 }
Adorner class:
1 Public classDecoratorImplementssourceable {2 3 Privatesourceable Source;4 PublicDecorator (sourceable source) {5 This. Source =Source;6 }7 8 @Override9 Public voidmethod () {TenSystem.out.println ("Decorator decorate 1"); One Source.method (); ASystem.out.println ("Decorator decorate 2"); - } - the Public Static voidMain (string[] args) { -sourceable Source =NewSource (); -sourceable sourceable =NewDecorator (source); - Sourceable.method (); + } -}
Decorator mode for Java design patterns