CodesJava

Easy learning with example program codes

java facade design pattern


Dictionary meaning of façade: The principal front of a building, that faces on to a street or open space.

Java façade design pattern comes under structural design patterns and it is used to hide the complexity of the system by providing a unified and simplified interface to a set of interfaces in a subsystem.

Main advantage of façade design pattern is that it provides loose coupling between client and subsystems.

Example

Shape.java

package com.codesjava;
 
public interface Shape {
	public void createShape();
}

Rectangle.java

package com.codesjava;
 
public class Rectangle implements Shape {	
	@Override
	public void createShape() {
		 System.out.println("Rectangle created.");
	}	
}

Circle.java

package com.codesjava;
 
public class Circle implements Shape {	
	@Override
	public void createShape() {
		 System.out.println("Circle created.");
	}	
}

ShapeFactory.java

package com.codesjava;
 
public class ShapeFactory {
    private Shape circle;
    private Shape rectangle;
 
    public ShapeFactory() {
      circle = new Circle();
      rectangle = new Rectangle();
    }
 
    public void createCircle(){
      circle.createShape();
    }
    public void createRectangle(){
      rectangle.createShape();
    }
}

FacadePatternTest.java

package com.codesjava;
 
public class FacadePatternTest {  
   public static void main(String args[]){	   
	  ShapeFactory shapeFactory = new ShapeFactory();
 
	  shapeFactory.createCircle();
	  shapeFactory.createRectangle();
   }
}

Output

Circle created.
Rectangle created.
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki