Sync Decorator to Java 25: H6 file labels, fix Main.java JDK-output mismatch, README mapping
This commit is contained in:
@@ -1,35 +1,24 @@
|
||||
package decorator;
|
||||
|
||||
/**
|
||||
* Decorator Design Pattern — Runnable Demo
|
||||
*
|
||||
* Shows how text processors can be stacked like Java IO streams.
|
||||
* Each decorator adds one behaviour; the order of wrapping matters.
|
||||
*
|
||||
* Run: javac decorator/*.java && java decorator.Main
|
||||
* Article: https://ankurm.com/decorator-design-pattern-java/
|
||||
*/
|
||||
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 + "\"");
|
||||
System.out.println();
|
||||
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, then upper case
|
||||
// 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 upper case
|
||||
// Stack 3: trim, filter profanity, then uppercase
|
||||
TextProcessor full =
|
||||
new UpperCaseDecorator(
|
||||
new ProfanityFilterDecorator(
|
||||
@@ -37,15 +26,15 @@ public class Main {
|
||||
new PlainTextProcessor())));
|
||||
System.out.println("Trim + Filter + Upper: \"" + full.process(input) + "\"");
|
||||
|
||||
// Stack 4: different order — filter then trim (order matters!)
|
||||
// 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();
|
||||
System.out.println("JDK parallel: new BufferedReader(new InputStreamReader(socket.getInputStream()))");
|
||||
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