Last active
December 14, 2015 00:09
-
-
Save doubledouble/4996789 to your computer and use it in GitHub Desktop.
Java Decorator example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyInterface | |
| | |
_______|_______ | |
| | | |
Myclass Decorator | |
____|_____ | |
| | | |
DecoratorA DecoratorB | |
class Myclass implements MyInterface { | |
public void print(){ | |
System.out.println("hello"); | |
} | |
} | |
class DecoratorA implements Decorator{ | |
MyInterface myObject; | |
DecoratorA(myInterface myObject){ | |
this.myObject = myObject; | |
} | |
public void print(){ | |
myObject.print(); | |
System.out.print("world!"); | |
} | |
} | |
class DecoratorB implements Decorator{ | |
MyInterface myObject; | |
DecoratorB(myInterface myObject){ | |
this.myObject = myObject; | |
} | |
public void print(){ | |
System.out.print("my"); | |
myObject.print(); | |
} | |
} | |
public static void main(Strings[] arg){ | |
MyInterface a =new DecoratorA(new DecoratorB(new MyClass()); | |
a.print(); | |
} | |
http://aixiangct.blog.163.com/blog/static/91522461201011363751978/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment