added course decorator example
This commit is contained in:
parent
075524c606
commit
f2fb3f7e47
5
src/learn/simple_decorator/Component.java
Normal file
5
src/learn/simple_decorator/Component.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public interface Component {
|
||||||
|
void operation();
|
||||||
|
}
|
8
src/learn/simple_decorator/ConcreteComponent.java
Normal file
8
src/learn/simple_decorator/ConcreteComponent.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public class ConcreteComponent implements Component {
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
System.out.println("Concrete operation");
|
||||||
|
}
|
||||||
|
}
|
13
src/learn/simple_decorator/ConcreteDecoratorA.java
Normal file
13
src/learn/simple_decorator/ConcreteDecoratorA.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
17
src/learn/simple_decorator/ConcreteDecoratorB.java
Normal file
17
src/learn/simple_decorator/ConcreteDecoratorB.java
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
14
src/learn/simple_decorator/Decorator.java
Normal file
14
src/learn/simple_decorator/Decorator.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
11
src/learn/simple_decorator/Main.java
Normal file
11
src/learn/simple_decorator/Main.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user