From f2fb3f7e47ffcdebaaf6ea0574b71d52503efb3b Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 4 Oct 2024 14:03:45 +0200 Subject: [PATCH] added course decorator example --- src/learn/simple_decorator/Component.java | 5 +++++ .../simple_decorator/ConcreteComponent.java | 8 ++++++++ .../simple_decorator/ConcreteDecoratorA.java | 13 +++++++++++++ .../simple_decorator/ConcreteDecoratorB.java | 17 +++++++++++++++++ src/learn/simple_decorator/Decorator.java | 14 ++++++++++++++ src/learn/simple_decorator/Main.java | 11 +++++++++++ 6 files changed, 68 insertions(+) create mode 100644 src/learn/simple_decorator/Component.java create mode 100644 src/learn/simple_decorator/ConcreteComponent.java create mode 100644 src/learn/simple_decorator/ConcreteDecoratorA.java create mode 100644 src/learn/simple_decorator/ConcreteDecoratorB.java create mode 100644 src/learn/simple_decorator/Decorator.java create mode 100644 src/learn/simple_decorator/Main.java 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(); + } +}