Gang of Four Structural pattern: Facade
Structural Pattern
A single class that represents an entire subsystem. Provide a unified interface to a set of interfaces in a system. Facade defines a higher-level interface that makes the subsystem easier to use.
To get the code for this example:
git clone https://github.com/spotadev/gangoffour.git
In src/main/java navigate to this package:
com.javaspeak.designpatterns.go4.structural.facade
You can run the code from the main method of:
FacadeApplication
This example application invokes methods on the ShapeFacade. The ShapeFacade provides an API to the outside world. Internally the ShapeFacade calls methods on other internal interfaces: SquareBuilder and TriangleBuilder.
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Text book desciption:
* <ul>
* Facade: A single class that represents an entire subsystem. Provide a unified interface to
* a set of interfaces in a system. Facade defines a higher-level interface that makes the
* subsystem easier to use.
* </ul>
* This example application invokes methods on the ShapeFacade. The ShapeFacade provides an API
* to the outside world. Internally the ShapeFacade calls methods on other internal interfaces:
* SquareBuilder and TriangleBuilder.
* <p>
* @author John Dickerson - 22 Feb 2020
*/
public class FacadeApplication {
/**
* Calls methods on the facade to retrieve shapes and prints them.
*/
public void printShapes() {
ShapeFacade shapeFacade = new ShapeFacadeImpl();
System.out.println( shapeFacade.getSquare() );
System.out.println( shapeFacade.getTriangle() );
}
/**
* Main method
*
* @param args
*/
public static void main( String[] args ) {
FacadeApplication application = new FacadeApplication();
application.printShapes();
}
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Defines an API to the outside world. Internally the implementation of the getSquare() and
* getTriangle() methods will make method calls to internal interfaces.
*
* @author John Dickerson - 23 Feb 2020
*/
public interface ShapeFacade {
/**
* Retrieves a Square
*
* @return a square
*/
public Square getSquare();
/**
* Retrieves a Triangle
*
* @return a triangle
*/
public Triangle getTriangle();
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Implements an API to the outside world. Internally the implementation of the getSquare() and
* getTriangle() methods make method calls to internal interfaces. A squareBuilder is used
* internally to create the square to return and a triangleBuilder is used internally to create
* the triangle to return.
*
* @author John Dickerson - 23 Feb 2020
*/
public class ShapeFacadeImpl implements ShapeFacade {
private SquareBuilder squareBuilder;
private TriangleBuilder triangleBuilder;
/**
* Constructor
*/
public ShapeFacadeImpl() {
squareBuilder = new SquareBuilderImpl();
triangleBuilder = new TriangleBuilderImpl();
}
@Override
public Square getSquare() {
return squareBuilder.buildSquare();
}
@Override
public Triangle getTriangle() {
return triangleBuilder.buildTriangle();
}
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Models a square
*
* @author John Dickerson - 23 Feb 2020
*/
public interface Square {
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Defines a buildSquare method.
*
* @author John Dickerson - 23 Feb 2020
*/
public interface SquareBuilder {
/**
* Builds a Square
*
* @return
* a Square
*/
Square buildSquare();
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Provides an implementation for a buildSquare method
*
* @author John Dickerson - 23 Feb 2020
*/
public class SquareBuilderImpl implements SquareBuilder {
@Override
public Square buildSquare() {
return new SquareImpl();
}
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Provides implementation for a Square
*
* @author John Dickerson - 23 Feb 2020
*/
public class SquareImpl implements Square {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append( "xxxx\n" );
sb.append( "x x\n" );
sb.append( "x x\n" );
sb.append( "xxxx\n" );
return sb.toString();
}
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Models a Triangle
*
* @author John Dickerson - 23 Feb 2020
*
*/
public interface Triangle {
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Defines a buildTriangle method.
*
* @author John Dickerson - 23 Feb 2020
*/
public interface TriangleBuilder {
/**
* Defines a buildTriangle method.
*
* @return
* a Triangle
*/
Triangle buildTriangle();
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Provides an implementation for a buildTriangle method
*
* @author John Dickerson - 23 Feb 2020
*/
public class TriangleBuilderImpl implements TriangleBuilder {
@Override
public Triangle buildTriangle() {
return new TriangleImpl();
}
}
package com.javaspeak.designpatterns.go4.structural.facade;
/**
* Provides implementation for a Triangle
*
* @author John Dickerson - 23 Feb 2020
*/
public class TriangleImpl implements Triangle {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append( " x \n" );
sb.append( " x x \n" );
sb.append( " x x \n" );
sb.append( "xxxxxxx\n" );
return sb.toString();
}
}
Back: Gang of Four
Page Author: JD