Skip to content

Instantly share code, notes, and snippets.

@doubledouble
Last active December 14, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doubledouble/4996789 to your computer and use it in GitHub Desktop.
Save doubledouble/4996789 to your computer and use it in GitHub Desktop.
Java Decorator example
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