Facade: split subsystem into one class per file, sync post code blocks to Java 25, add README mapping
This commit is contained in:
9
02-structural/facade/AudioMixer.java
Normal file
9
02-structural/facade/AudioMixer.java
Normal 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());
|
||||
}
|
||||
}
|
||||
15
02-structural/facade/BitrateReader.java
Normal file
15
02-structural/facade/BitrateReader.java
Normal 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());
|
||||
}
|
||||
}
|
||||
6
02-structural/facade/Codec.java
Normal file
6
02-structural/facade/Codec.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package facade;
|
||||
|
||||
/** Codec — common interface implemented by each concrete compression codec. */
|
||||
interface Codec {
|
||||
String getName();
|
||||
}
|
||||
10
02-structural/facade/CodecFactory.java
Normal file
10
02-structural/facade/CodecFactory.java
Normal 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();
|
||||
}
|
||||
}
|
||||
6
02-structural/facade/MPEG4CompressionCodec.java
Normal file
6
02-structural/facade/MPEG4CompressionCodec.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package facade;
|
||||
|
||||
/** Concrete Codec: MPEG-4 compression. */
|
||||
class MPEG4CompressionCodec implements Codec {
|
||||
@Override public String getName() { return "mpeg4"; }
|
||||
}
|
||||
6
02-structural/facade/OggCompressionCodec.java
Normal file
6
02-structural/facade/OggCompressionCodec.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package facade;
|
||||
|
||||
/** Concrete Codec: Ogg compression. */
|
||||
class OggCompressionCodec implements Codec {
|
||||
@Override public String getName() { return "ogg"; }
|
||||
}
|
||||
35
02-structural/facade/README.md
Normal file
35
02-structural/facade/README.md
Normal 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/
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
23
02-structural/facade/VideoFile.java
Normal file
23
02-structural/facade/VideoFile.java
Normal 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; }
|
||||
}
|
||||
21
README.md
21
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
|
||||
|
||||
Reference in New Issue
Block a user