Created
January 26, 2018 11:00
-
-
Save slmanju/37453bb169fc773eab2eed479782d83c to your computer and use it in GitHub Desktop.
Decorator design pattern with Java
This file contains hidden or 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
interface Component { | |
void doSomething(); | |
} |
This file contains hidden or 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
public class ComponentA implements Component { | |
public ComponentA() { | |
} | |
@Override | |
public void doSomething() { | |
System.out.println("i'm doing simple things"); | |
} | |
} |
This file contains hidden or 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
public abstract class Decorator implements Component { | |
private Component component; | |
public Decorator(Component component) { | |
this.component = component; | |
} | |
@Override | |
public void doSomething() { | |
this.component.doSomething(); | |
} | |
} |
This file contains hidden or 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
public class DecoratorA extends Decorator { | |
public DecoratorA(Component component) { | |
super(component); | |
} | |
public void doSomething() { | |
super.doSomething(); | |
System.out.println("i'll add A"); | |
} | |
} |
This file contains hidden or 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
public class DecoratorB extends Decorator { | |
public DecoratorB(Component component) { | |
super(component); | |
} | |
public void doSomething() { | |
super.doSomething(); | |
System.out.println("i'll add B"); | |
} | |
} |
This file contains hidden or 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
public class DecoratorDemo { | |
public static void main(String[] args) { | |
Component component = new ComponentA(); | |
component.doSomething(); | |
System.out.println("\n----------------------------\n"); | |
Component decoratedComponent = new DecoratorA(component); | |
decoratedComponent.doSomething(); | |
System.out.println("\n----------------------------\n"); | |
decoratedComponent = new DecoratorB(component); | |
decoratedComponent.doSomething(); | |
System.out.println("\n----------------------------\n"); | |
decoratedComponent = new DecoratorB(new DecoratorA(component)); | |
decoratedComponent.doSomething(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment