- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Facade Pattern
Facade pattern hides the complexities of the system and provides an interface to the cpent using which the cpent can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.
This pattern involves a single class which provides simppfied methods required by cpent and delegates calls to methods of existing system classes.
Implementation
We are going to create a Shape interface and concrete classes implementing the Shape interface. A facade class ShapeMaker is defined as a next step.
ShapeMaker class uses the concrete classes to delegate user calls to these classes. FacadePatternDemo, our demo class, will use ShapeMaker class to show the results.
Step 1
Create an interface.
Shape.java
pubpc interface Shape { void draw(); }
Step 2
Create concrete classes implementing the same interface.
Rectangle.java
pubpc class Rectangle implements Shape { @Override pubpc void draw() { System.out.println("Rectangle::draw()"); } }
Square.java
pubpc class Square implements Shape { @Override pubpc void draw() { System.out.println("Square::draw()"); } }
Circle.java
pubpc class Circle implements Shape { @Override pubpc void draw() { System.out.println("Circle::draw()"); } }
Step 3
Create a facade class.
ShapeMaker.java
pubpc class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; pubpc ShapeMaker() { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } pubpc void drawCircle(){ circle.draw(); } pubpc void drawRectangle(){ rectangle.draw(); } pubpc void drawSquare(){ square.draw(); } }
Step 4
Use the facade to draw various types of shapes.
FacadePatternDemo.java
pubpc class FacadePatternDemo { pubpc static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); } }
Step 5
Verify the output.
Circle::draw() Rectangle::draw() Square::draw()Advertisements