added course decorator example

This commit is contained in:
Louis Heredero 2024-10-04 14:03:45 +02:00
parent 075524c606
commit f2fb3f7e47
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package learn.simple_decorator;
public interface Component {
void operation();
}

View File

@ -0,0 +1,8 @@
package learn.simple_decorator;
public class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("Concrete operation");
}
}

View File

@ -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();
}
}

View File

@ -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.");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}