Files
design-patterns/02-structural/facade/VideoConversionFacade.java

40 lines
1.5 KiB
Java

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