diff --git a/src/learn/simple_decorator/Component.java b/src/learn/simple_decorator/Component.java new file mode 100644 index 0000000..718ede7 --- /dev/null +++ b/src/learn/simple_decorator/Component.java @@ -0,0 +1,5 @@ +package learn.simple_decorator; + +public interface Component { + void operation(); +} diff --git a/src/learn/simple_decorator/ConcreteComponent.java b/src/learn/simple_decorator/ConcreteComponent.java new file mode 100644 index 0000000..e0edc3b --- /dev/null +++ b/src/learn/simple_decorator/ConcreteComponent.java @@ -0,0 +1,8 @@ +package learn.simple_decorator; + +public class ConcreteComponent implements Component { + @Override + public void operation() { + System.out.println("Concrete operation"); + } +} diff --git a/src/learn/simple_decorator/ConcreteDecoratorA.java b/src/learn/simple_decorator/ConcreteDecoratorA.java new file mode 100644 index 0000000..58c015a --- /dev/null +++ b/src/learn/simple_decorator/ConcreteDecoratorA.java @@ -0,0 +1,13 @@ +package learn.simple_decorator; + +public class ConcreteDecoratorA extends Decorator { + public ConcreteDecoratorA(Component component) { + super(component); + } + + @Override + public void operation() { + System.out.println("I am adding some new responsibilities here."); + super.operation(); + } +} diff --git a/src/learn/simple_decorator/ConcreteDecoratorB.java b/src/learn/simple_decorator/ConcreteDecoratorB.java new file mode 100644 index 0000000..20c1472 --- /dev/null +++ b/src/learn/simple_decorator/ConcreteDecoratorB.java @@ -0,0 +1,17 @@ +package learn.simple_decorator; + +public class ConcreteDecoratorB extends Decorator { + public ConcreteDecoratorB(Component component) { + super(component); + } + + @Override + public void operation() { + addedBehavior(); + super.operation(); + } + + private void addedBehavior() { + System.out.println("I am adding some new behavior here."); + } +} diff --git a/src/learn/simple_decorator/Decorator.java b/src/learn/simple_decorator/Decorator.java new file mode 100644 index 0000000..4be0f2e --- /dev/null +++ b/src/learn/simple_decorator/Decorator.java @@ -0,0 +1,14 @@ +package learn.simple_decorator; + +public abstract class Decorator implements Component { + private Component component; + + public Decorator(Component component) { + this.component = component; + } + + @Override + public void operation() { + component.operation(); + } +} diff --git a/src/learn/simple_decorator/Main.java b/src/learn/simple_decorator/Main.java new file mode 100644 index 0000000..4cd82dd --- /dev/null +++ b/src/learn/simple_decorator/Main.java @@ -0,0 +1,11 @@ +package learn.simple_decorator; + +public class Main { + public static void main(String[] args) { + Component component = new ConcreteComponent(); + component = new ConcreteDecoratorB(component); + component = new ConcreteDecoratorA(component); + + component.operation(); + } +}