Categories
Algorithms & Design Design Pattern

Facade Pattern

Shape.java

public abstract class Shape {

	public Shape() {
		// TODO Auto-generated constructor stub
	}

	public abstract void draw();
}

HexagonShape.java

public class HexagonShape extends Shape {

	public HexagonShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("drawing the hexagon..");
	}

}

PentagonShape.java

public class PentagonShape extends Shape {

	public PentagonShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing the pentagon..");
	}

}

SquareShape.java

public class SquareShape extends Shape {

	public SquareShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing the square..");
	}

}

ShapeMaker.java

public class ShapeMaker {
	private Shape square, pentagon, hexagon;
	
	public ShapeMaker() {
		// TODO Auto-generated constructor stub
		this.square=new SquareShape();
		this.pentagon=new PentagonShape();
		this.hexagon=new HexagonShape();
	}

	public void drawSquare(){
		square.draw();
	}
	
	public void drawPentagon(){
		pentagon.draw();
	}
	
	public void drawHexagon(){
		hexagon.draw();
	}
}

FacadePatternDemo.java

public class FacadePatternDemo {

	public FacadePatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShapeMaker maker = new ShapeMaker();
		
		//get me square shape
		maker.drawSquare();
		
		//get me hexagon
		maker.drawHexagon();
		
		//get me pentagon
		maker.drawPentagon();
	}

}

Output

Leave a comment

Design a site like this with WordPress.com
Get started