Add all 23 GoF design pattern implementations
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"; }
|
||||
}
|
||||
30
02-structural/facade/Main.java
Normal file
30
02-structural/facade/Main.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package facade;
|
||||
|
||||
/**
|
||||
* Facade Design Pattern — Runnable Demo
|
||||
*
|
||||
* Demonstrates reducing a complex video conversion subsystem
|
||||
* to a single method call via a Facade.
|
||||
*
|
||||
* Run: javac facade/*.java && java facade.Main
|
||||
* Article: https://ankurm.com/facade-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Facade Design Pattern Demo ===\n");
|
||||
|
||||
VideoConversionFacade converter = new VideoConversionFacade();
|
||||
|
||||
System.out.println("-- Client: just one method call --");
|
||||
String result1 = converter.convertVideo("holiday.ogg", "mp4");
|
||||
System.out.println("Output: " + result1);
|
||||
|
||||
System.out.println();
|
||||
String result2 = converter.convertVideo("presentation.mp4", "ogg");
|
||||
System.out.println("Output: " + result2);
|
||||
|
||||
System.out.println("\nClient code: 1 line. Subsystem: 6 classes. Facade hides the complexity.");
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
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/
|
||||
39
02-structural/facade/VideoConversionFacade.java
Normal file
39
02-structural/facade/VideoConversionFacade.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package facade;
|
||||
|
||||
/**
|
||||
* Facade — the single, simple entry point to a complex video subsystem.
|
||||
*
|
||||
* Without this class, clients need to know about CodecFactory,
|
||||
* BitrateReader, AudioMixer, and VideoFile — 4 classes, dozens of methods.
|
||||
* The facade reduces that to ONE method call.
|
||||
*
|
||||
* The subsystem classes still exist and can be used directly
|
||||
* by advanced users who need fine-grained control.
|
||||
*/
|
||||
public class VideoConversionFacade {
|
||||
|
||||
public String convertVideo(String inputFile, String targetFormat) {
|
||||
System.out.println("VideoConversionFacade: starting conversion of " + inputFile);
|
||||
|
||||
// Step 1: open the file and detect codec
|
||||
VideoFile file = new VideoFile(inputFile);
|
||||
Codec sourceCodec = CodecFactory.extract(file);
|
||||
|
||||
// Step 2: prepare destination codec
|
||||
Codec destCodec;
|
||||
if ("mp4".equals(targetFormat)) {
|
||||
destCodec = new MPEG4CompressionCodec();
|
||||
} else {
|
||||
destCodec = new OggCompressionCodec();
|
||||
}
|
||||
|
||||
// Step 3: read, mix audio, encode
|
||||
VideoFile buffer = BitrateReader.read(file, sourceCodec);
|
||||
VideoFile intermediateResult = BitrateReader.convert(buffer, destCodec);
|
||||
VideoFile result = AudioMixer.fix(intermediateResult);
|
||||
|
||||
String outputFilename = inputFile.replaceAll("\\.[^.]+$", "." + targetFormat);
|
||||
System.out.println("VideoConversionFacade: conversion complete -> " + outputFilename);
|
||||
return outputFilename;
|
||||
}
|
||||
}
|
||||
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; }
|
||||
}
|
||||
Reference in New Issue
Block a user