feat(hw-03): implement Decorator pattern
This commit is contained in:
@@ -6,6 +6,7 @@ package org.example;
|
|||||||
import org.example.adapter.*;
|
import org.example.adapter.*;
|
||||||
import org.example.builder.*;
|
import org.example.builder.*;
|
||||||
import org.example.chain.*;
|
import org.example.chain.*;
|
||||||
|
import org.example.decorator.*;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -16,6 +17,7 @@ public class App {
|
|||||||
checkAdapterPattern();
|
checkAdapterPattern();
|
||||||
checkBuilderPattern();
|
checkBuilderPattern();
|
||||||
checkChainPattern();
|
checkChainPattern();
|
||||||
|
checkDecoratorPattern();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkAdapterPattern() {
|
private static void checkAdapterPattern() {
|
||||||
@@ -39,4 +41,11 @@ public class App {
|
|||||||
handler.handle(42);
|
handler.handle(42);
|
||||||
handler.handle("Hello");
|
handler.handle("Hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkDecoratorPattern() {
|
||||||
|
Component component = new ConcreteComponent();
|
||||||
|
Component decorator = new ConcreteDecorator(component);
|
||||||
|
|
||||||
|
decorator.operation();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package org.example.decorator;
|
||||||
|
|
||||||
|
/** Component */
|
||||||
|
public interface Component {
|
||||||
|
void operation();
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package org.example.decorator;
|
||||||
|
|
||||||
|
/** Concrete Component */
|
||||||
|
public class ConcreteComponent implements Component {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
System.out.println("ConcreteComponent выполняет операцию");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.example.decorator;
|
||||||
|
|
||||||
|
/** Concrete Decorator */
|
||||||
|
public class ConcreteDecorator extends Decorator {
|
||||||
|
|
||||||
|
public ConcreteDecorator(Component component) {
|
||||||
|
super(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
component.operation();
|
||||||
|
System.out.println("ConcreteDecorator добавляет поведение");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.decorator;
|
||||||
|
|
||||||
|
/** Decorator */
|
||||||
|
public abstract class Decorator implements Component {
|
||||||
|
|
||||||
|
protected Decorator(Component component) {
|
||||||
|
this.component = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
component.operation();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final Component component;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user