Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Last active March 5, 2020 11:20
Show Gist options
  • Save pfieffer/4c3177afdd73d93918d94a2d35313153 to your computer and use it in GitHub Desktop.
Save pfieffer/4c3177afdd73d93918d94a2d35313153 to your computer and use it in GitHub Desktop.
FacadePatternDemo.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
public interface Shape {
void draw();
}
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
@pfieffer
Copy link
Author

pfieffer commented Mar 5, 2020

Circle, Rectangle and Square are all implementations of Shape.
The ShapeMaker is the Facade to all the Shapes. FacadePatternDemo class shows how the Facade can be used to draw different shapes.
Read more? : https://www.tutorialspoint.com/design_pattern/facade_pattern.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment