Facade: split subsystem into one class per file, sync post code blocks to Java 25, add README mapping

This commit is contained in:
2026-06-24 15:33:22 +05:30
parent d6df1fd013
commit 083beb9780
10 changed files with 131 additions and 62 deletions

View File

@@ -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());
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,6 @@
package facade;
/** Codec — common interface implemented by each concrete compression codec. */
interface Codec {
String getName();
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,6 @@
package facade;
/** Concrete Codec: MPEG-4 compression. */
class MPEG4CompressionCodec implements Codec {
@Override public String getName() { return "mpeg4"; }
}

View File

@@ -0,0 +1,6 @@
package facade;
/** Concrete Codec: Ogg compression. */
class OggCompressionCodec implements Codec {
@Override public String getName() { return "ogg"; }
}

View File

@@ -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/

View File

@@ -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());
}
}

View File

@@ -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; }
}

View File

@@ -249,6 +249,27 @@ javac 02-structural/decorator/*.java -d out/decorator
java -cp out/decorator decorator.Main 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 ## Reference
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides