Add all 23 GoF design pattern implementations
This commit is contained in:
42
02-structural/decorator/Main.java
Normal file
42
02-structural/decorator/Main.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package decorator;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Decorator Design Pattern Demo ===\n");
|
||||
|
||||
String input = " hello world, badword is here ";
|
||||
System.out.println("Input: \"" + input + "\"\n");
|
||||
|
||||
// Stack 1: just trim
|
||||
TextProcessor trimOnly = new TrimDecorator(new PlainTextProcessor());
|
||||
System.out.println("Trim only: \"" + trimOnly.process(input) + "\"");
|
||||
|
||||
// Stack 2: trim first (innermost), then uppercase (outermost)
|
||||
// Execution order: PlainText → Trim → UpperCase
|
||||
TextProcessor trimThenUpper =
|
||||
new UpperCaseDecorator(
|
||||
new TrimDecorator(
|
||||
new PlainTextProcessor()));
|
||||
System.out.println("Trim + UpperCase: \"" + trimThenUpper.process(input) + "\"");
|
||||
|
||||
// Stack 3: trim, filter profanity, then uppercase
|
||||
TextProcessor full =
|
||||
new UpperCaseDecorator(
|
||||
new ProfanityFilterDecorator(
|
||||
new TrimDecorator(
|
||||
new PlainTextProcessor())));
|
||||
System.out.println("Trim + Filter + Upper: \"" + full.process(input) + "\"");
|
||||
|
||||
// Stack 4: filter THEN trim — different result because order changed
|
||||
TextProcessor filterFirst =
|
||||
new TrimDecorator(
|
||||
new ProfanityFilterDecorator(
|
||||
new PlainTextProcessor()));
|
||||
System.out.println("Filter + Trim: \"" + filterFirst.process(input) + "\"");
|
||||
|
||||
System.out.println("\n--- JDK equivalent ---");
|
||||
System.out.println("new BufferedReader(new InputStreamReader(socket.getInputStream()))");
|
||||
System.out.println("Same pattern: each wrapper adds one behaviour, order matters.");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user