Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
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 ===");
|
||||
}
|
||||
}
|
||||
62
02-structural/facade/Subsystems.java
Normal file
62
02-structural/facade/Subsystems.java
Normal file
@@ -0,0 +1,62 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user