PackageCom.hk.ztry;Interfacesourcable { Public voidoperation (); } classSourceImplementssourcable { Public voidoperation () {SYSTEM.OUT.PRINTLN ("The Soure class implements the method in the original interface sourcable"); } }classDecorator1Implementssourcable {Privatesourcable sourcable; PublicDecorator1 (sourcable sourcable) {Super(); This. sourcable=sourcable; } Public voidoperation () {SYSTEM.OUT.PRINTLN ("Before 1th decorator"); Sourcable.operation (); System.out.println ("After 1th adorner"); } } classDecorator2Implementssourcable {Privatesourcable sourcable; PublicDecorator2 (sourcable sourcable) {Super(); This. sourcable=sourcable; } Public voidoperation () {SYSTEM.OUT.PRINTLN ("Before 2nd decorator"); Sourcable.operation (); System.out.println ("After 2nd adorner"); } } Public classTestdecorator { Public Static voidMain (string[] args) {sourcable source=NewSource (); //good magic, interface =new implementation class (); //then the interface, which embodies the function of the realization class,//Interface =new Decoration class (),//then the interface, in the original implementation of the class has been decorated functionsourcable obj =NewDecorator1 (NewDecorator2 (source)); Obj.operation (); } }
Understanding-Adorner Mode-design mode