From 083beb97801e0ea484eb774ad6c8be8e47904486 Mon Sep 17 00:00:00 2001 From: Ankur Date: Wed, 24 Jun 2026 15:33:22 +0530 Subject: [PATCH] Facade: split subsystem into one class per file, sync post code blocks to Java 25, add README mapping --- 02-structural/facade/AudioMixer.java | 9 +++ 02-structural/facade/BitrateReader.java | 15 +++++ 02-structural/facade/Codec.java | 6 ++ 02-structural/facade/CodecFactory.java | 10 +++ .../facade/MPEG4CompressionCodec.java | 6 ++ 02-structural/facade/OggCompressionCodec.java | 6 ++ 02-structural/facade/README.md | 35 +++++++++++ 02-structural/facade/Subsystems.java | 62 ------------------- 02-structural/facade/VideoFile.java | 23 +++++++ README.md | 21 +++++++ 10 files changed, 131 insertions(+), 62 deletions(-) create mode 100644 02-structural/facade/AudioMixer.java create mode 100644 02-structural/facade/BitrateReader.java create mode 100644 02-structural/facade/Codec.java create mode 100644 02-structural/facade/CodecFactory.java create mode 100644 02-structural/facade/MPEG4CompressionCodec.java create mode 100644 02-structural/facade/OggCompressionCodec.java create mode 100644 02-structural/facade/README.md delete mode 100644 02-structural/facade/Subsystems.java create mode 100644 02-structural/facade/VideoFile.java diff --git a/02-structural/facade/AudioMixer.java b/02-structural/facade/AudioMixer.java new file mode 100644 index 0000000..cd53159 --- /dev/null +++ b/02-structural/facade/AudioMixer.java @@ -0,0 +1,9 @@ +package facade; + +/** AudioMixer — normalises audio tracks after conversion. */ +class AudioMixer { + static VideoFile fix(VideoFile result) { + System.out.println(" AudioMixer: fixing audio tracks"); + return new VideoFile(result.getFilename()); + } +} diff --git a/02-structural/facade/BitrateReader.java b/02-structural/facade/BitrateReader.java new file mode 100644 index 0000000..2e2d6f8 --- /dev/null +++ b/02-structural/facade/BitrateReader.java @@ -0,0 +1,15 @@ +package facade; + +/** BitrateReader — reads the video buffer and converts it between codecs. */ +class BitrateReader { + static VideoFile read(VideoFile file, Codec codec) { + System.out.println(" BitrateReader: reading " + file.getFilename() + + " with codec " + codec.getName()); + return new VideoFile(file.getFilename()); + } + + static VideoFile convert(VideoFile buffer, Codec codec) { + System.out.println(" BitrateReader: converting to " + codec.getName()); + return new VideoFile(buffer.getFilename()); + } +} diff --git a/02-structural/facade/Codec.java b/02-structural/facade/Codec.java new file mode 100644 index 0000000..29748fc --- /dev/null +++ b/02-structural/facade/Codec.java @@ -0,0 +1,6 @@ +package facade; + +/** Codec — common interface implemented by each concrete compression codec. */ +interface Codec { + String getName(); +} diff --git a/02-structural/facade/CodecFactory.java b/02-structural/facade/CodecFactory.java new file mode 100644 index 0000000..3dd1d90 --- /dev/null +++ b/02-structural/facade/CodecFactory.java @@ -0,0 +1,10 @@ +package facade; + +/** CodecFactory — inspects a VideoFile and returns the matching Codec. */ +class CodecFactory { + static Codec extract(VideoFile file) { + System.out.println(" CodecFactory: extracting codec from " + file.getFilename()); + if ("mpeg4".equals(file.getCodecType())) return new MPEG4CompressionCodec(); + return new OggCompressionCodec(); + } +} diff --git a/02-structural/facade/MPEG4CompressionCodec.java b/02-structural/facade/MPEG4CompressionCodec.java new file mode 100644 index 0000000..d16d664 --- /dev/null +++ b/02-structural/facade/MPEG4CompressionCodec.java @@ -0,0 +1,6 @@ +package facade; + +/** Concrete Codec: MPEG-4 compression. */ +class MPEG4CompressionCodec implements Codec { + @Override public String getName() { return "mpeg4"; } +} diff --git a/02-structural/facade/OggCompressionCodec.java b/02-structural/facade/OggCompressionCodec.java new file mode 100644 index 0000000..c2abc0d --- /dev/null +++ b/02-structural/facade/OggCompressionCodec.java @@ -0,0 +1,6 @@ +package facade; + +/** Concrete Codec: Ogg compression. */ +class OggCompressionCodec implements Codec { + @Override public String getName() { return "ogg"; } +} diff --git a/02-structural/facade/README.md b/02-structural/facade/README.md new file mode 100644 index 0000000..0e32ae9 --- /dev/null +++ b/02-structural/facade/README.md @@ -0,0 +1,35 @@ +# Facade Design Pattern — Java Example + +**Pattern:** Structural → Facade +**Article:** https://ankurm.com/facade-design-pattern-java/ + +## What this example shows + +A video-conversion subsystem (`VideoFile`, `Codec`, `MPEG4CompressionCodec`, `OggCompressionCodec`, `CodecFactory`, `BitrateReader`, `AudioMixer`) is wrapped behind one class, `VideoConversionFacade`, that exposes a single `convertVideo()` method. Callers never touch the six subsystem classes directly, but they remain public and usable on their own for advanced use cases. + +## How to run + +```bash +javac facade/*.java -d out/facade +java -cp out/facade facade.Main +``` + +Requires Java 25. + +## Post Section ↔ File Mapping + +| Post Section | File(s) | +|---|---| +| The Subsystem (Complex, But Unchanged) — VideoFile | `VideoFile.java` | +| The Subsystem (Complex, But Unchanged) — Codec interface | `Codec.java` | +| The Subsystem (Complex, But Unchanged) — Concrete Codecs | `MPEG4CompressionCodec.java`, `OggCompressionCodec.java` | +| The Subsystem (Complex, But Unchanged) — CodecFactory | `CodecFactory.java` | +| The Subsystem (Complex, But Unchanged) — BitrateReader | `BitrateReader.java` | +| The Subsystem (Complex, But Unchanged) — AudioMixer | `AudioMixer.java` | +| The Facade | `VideoConversionFacade.java` | +| Client Code: One Line | `Main.java` | + +Note: the SLF4J `LoggerFactory`, JDBC `DriverManager`, and Spring `JdbcTemplate` snippets under "Facade in the JDK and Real Frameworks" are illustrative only — they are not part of this repository's runnable example. + +Article: https://ankurm.com/facade-design-pattern-java/ +All patterns: https://ankurm.com/design-patterns-java/ diff --git a/02-structural/facade/Subsystems.java b/02-structural/facade/Subsystems.java deleted file mode 100644 index 7072e5c..0000000 --- a/02-structural/facade/Subsystems.java +++ /dev/null @@ -1,62 +0,0 @@ -package facade; - -/** - * Complex subsystem classes — these are what the Facade hides. - * Each class has its own complex API; clients shouldn't need to know all of them. - */ - -class VideoFile { - private final String filename; - private final String codecType; - - VideoFile(String filename) { - this(filename, filename.endsWith(".mp4") ? "mpeg4" : "ogg"); - } - - VideoFile(String filename, String codec) { - this.filename = filename; - this.codecType = codec; - System.out.println(" VideoFile: " + filename + " [codec: " + codecType + "]"); - } - - public String getFilename() { return filename; } - public String getCodecType() { return codecType; } -} - -interface Codec { String getName(); } - -class MPEG4CompressionCodec implements Codec { - @Override public String getName() { return "mpeg4"; } -} - -class OggCompressionCodec implements Codec { - @Override public String getName() { return "ogg"; } -} - -class CodecFactory { - static Codec extract(VideoFile file) { - System.out.println(" CodecFactory: extracting codec from " + file.getFilename()); - if ("mpeg4".equals(file.getCodecType())) return new MPEG4CompressionCodec(); - return new OggCompressionCodec(); - } -} - -class BitrateReader { - static VideoFile read(VideoFile file, Codec codec) { - System.out.println(" BitrateReader: reading " + file.getFilename() - + " with codec " + codec.getName()); - return new VideoFile(file.getFilename()); - } - - static VideoFile convert(VideoFile buffer, Codec codec) { - System.out.println(" BitrateReader: converting to " + codec.getName()); - return new VideoFile(buffer.getFilename()); - } -} - -class AudioMixer { - static VideoFile fix(VideoFile result) { - System.out.println(" AudioMixer: fixing audio tracks"); - return new VideoFile(result.getFilename()); - } -} diff --git a/02-structural/facade/VideoFile.java b/02-structural/facade/VideoFile.java new file mode 100644 index 0000000..f3d07ab --- /dev/null +++ b/02-structural/facade/VideoFile.java @@ -0,0 +1,23 @@ +package facade; + +/** + * Complex subsystem classes — these are what the Facade hides. + * Each class has its own complex API; clients shouldn't need to know all of them. + */ +class VideoFile { + private final String filename; + private final String codecType; + + VideoFile(String filename) { + this(filename, filename.endsWith(".mp4") ? "mpeg4" : "ogg"); + } + + VideoFile(String filename, String codec) { + this.filename = filename; + this.codecType = codec; + System.out.println(" VideoFile: " + filename + " [codec: " + codecType + "]"); + } + + public String getFilename() { return filename; } + public String getCodecType() { return codecType; } +} diff --git a/README.md b/README.md index 8a45a09..e877130 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,27 @@ javac 02-structural/decorator/*.java -d out/decorator java -cp out/decorator decorator.Main ``` +### Facade (`02-structural/facade/`) + +| Post Section | File(s) | +|---|---| +| The Subsystem (Complex, But Unchanged) — VideoFile | `VideoFile.java` | +| The Subsystem (Complex, But Unchanged) — Codec interface | `Codec.java` | +| The Subsystem (Complex, But Unchanged) — Concrete Codecs | `MPEG4CompressionCodec.java`, `OggCompressionCodec.java` | +| The Subsystem (Complex, But Unchanged) — CodecFactory | `CodecFactory.java` | +| The Subsystem (Complex, But Unchanged) — BitrateReader | `BitrateReader.java` | +| The Subsystem (Complex, But Unchanged) — AudioMixer | `AudioMixer.java` | +| The Facade | `VideoConversionFacade.java` | +| Client Code: One Line | `Main.java` | + +Note: the SLF4J `LoggerFactory`, JDBC `DriverManager`, and Spring `JdbcTemplate` snippets under "Facade in the JDK and Real Frameworks" are illustrative only — they are not part of this repository's runnable example. + +Run it: +```bash +javac 02-structural/facade/*.java -d out/facade +java -cp out/facade facade.Main +``` + ## Reference - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides