Java
Usage examples: The Facade pattern is commonly used in apps written in Java. It’s especially handy when working with complex libraries and APIs.
Here are some Facade examples in core Java libs:
javax.faces.context.FacesContextusesLifeCycle,ViewHandler,NavigationHandlerclasses under the hood, but most clients aren’t aware of that.javax.faces.context.ExternalContextusesServletContext,HttpSession,HttpServletRequest,HttpServletResponseand others inside.
Identification: Facade can be recognized in a class that has a simple interface, but delegates most of the work to other classes. Usually, facades manage the full life cycle of objects they use.
Simple interface for a complex video conversion library
In this example, the Facade simplifies communication with a complex video conversion framework.
The Facade provides a single class with a single method that handles all the complexity of configuring the right classes of the framework and retrieving the result in a correct format.
some_complex_media_library: Complex video conversion library
some_complex_media_library/VideoFile.java
package refactoring_guru.facade.example.some_complex_media_library;
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;
}
}some_complex_media_library/Codec.java
some_complex_media_library/MPEG4CompressionCodec.java
some_complex_media_library/OggCompressionCodec.java
some_complex_media_library/CodecFactory.java
some_complex_media_library/BitrateReader.java
some_complex_media_library/AudioMixer.java
facade
facade/VideoConversionFacade.java: Facade provides simple interface of video conversion
Demo.java: Client code
OutputDemo.txt: Execution result
Last updated