Let's create a package called
some_complex_media_library.
Inside of this package we add an
interface we will call
Codec.
Now let's add a class we call
VideoFile.
The code for **VideoFile** will be:
public class VideoFile {
private String name;
private String codecType;
public VideoFile(String name) {
this.name = name;
this.codecType = name.substring(name.indexOf(".") + 1);
}
public String getCodecType() {
return codecType;
}
public String getName() {
return name;
}
}
Now let's create a couple of classes that will *implement*
Codec,
MPEG4CompressionCodec and
OggCompressionCodec.
public class MPEG4CompressionCodec implements Codec {
public String type = "mp4";
}
And for
OggCompressionCodec we have:
public class OggCompressionCodec implements Codec {
public String type = "ogg";
}
Let also add a **CodecFactory** class.
public class CodecFactory {
public static Codec extract(VideoFile file) {
String type = file.getCodecType();
if (type.equals("mp4")) {
System.out.println("CodecFactory: extracting mpeg audio...");
return new MPEG4CompressionCodec();
}
else {
System.out.println("CodecFactory: extracting ogg audio...");
return new OggCompressionCodec();
}
}
}
![Facade](/UMLs/images/Facade/Facade-4.png)
I also want to add a
BitrateReader class.
public class BitrateReader {
public static VideoFile read(VideoFile file, Codec codec) {
System.out.println("BitrateReader: reading file...");
return file;
}
public static VideoFile convert(VideoFile buffer, Codec codec) {
System.out.println("BitrateReader: writing file...");
return buffer;
}
}
Let me add another class I call **AudioMixer**.
import java.io.File;
public class AudioMixer {
public File fix(VideoFile result){
System.out.println("AudioMixer: fixing audio...");
return new File("tmp");
}
}
Lastly we'll add a class we call **VideoConversionFacade**
import java.io.File;
public class VideoConversionFacade {
public File convertVideo(String fileName, String format) {
System.out.println("VideoConversionFacade: conversion started.");
VideoFile file = new VideoFile(fileName);
Codec sourceCodec = CodecFactory.extract(file);
Codec destinationCodec;
if (format.equals("mp4")) {
destinationCodec = new OggCompressionCodec();
} else {
destinationCodec = new MPEG4CompressionCodec();
}
VideoFile buffer = BitrateReader.read(file, sourceCodec);
VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec);
File result = (new AudioMixer()).fix(intermediateResult);
System.out.println("VideoConversionFacade: conversion completed.");
return result;
}
}
Now let's create a
facade packabge.
Inside this package we add a
VideoConversionFacade class:
import java.io.File;
public class VideoConversionFacade {
public File convertVideo(String fileName, String format) {
System.out.println("VideoConversionFacade: conversion started.");
VideoFile file = new VideoFile(fileName);
Codec sourceCodec = CodecFactory.extract(file);
Codec destinationCodec;
if (format.equals("mp4")) {
destinationCodec = new OggCompressionCodec();
} else {
destinationCodec = new MPEG4CompressionCodec();
}
VideoFile buffer = BitrateReader.read(file, sourceCodec);
VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec);
File result = (new AudioMixer()).fix(intermediateResult);
System.out.println("VideoConversionFacade: conversion completed.");
return result;
}
}
Now let's put this all together in a
Demo class which will have a
main method.
import java.io.File;
public class Demo {
public static void main(String[] args) {
VideoConversionFacade converter = new VideoConversionFacade();
File mp4Video = converter.convertVideo("youtubevideo.ogg", "mp4");
// ...
}
}
Let's compile this an run it, we should get:
VideoConversionFacade: conversion started.
CodecFactory: extracting ogg audio...
BitrateReader: reading file...
BitrateReader: writing file...
AudioMixer: fixing audio...
VideoConversionFacade: conversion completed.
The Ray Code is AWESOME!!!
[Wikipedia](https://en.wikipedia.org/wiki/Decorator_pattern)
----------------------------------------------------------------------------------------------------
Find Ray on:
[facebook](https://www.facebook.com/TheRayCode/)
[youtube](https://www.youtube.com/user/AndradeRay/)
[The Ray Code](https://www.RayAndrade.com)
[Ray Andrade](https://www.RayAndrade.org)
No comments:
Post a Comment