Monday, 20 May 2013

Facade Pattern Example


Facade pattern is one of the structural pattern which is used to hide internal complexity. Here I am giving one example of color filling. You can use following steps to get working example of facede pattern.

1. First create Color interface.
public interface Color {
void fillColor();
}//Color

2. Now create class Redcolor
public class RedColor implements Color {
public void fillColor() {
System.out.print("\nFilling red color");
}//fillColor
}//RedColor

3.Now create class BlueColor
public class BlueColor implements Color {
public void fillColor() {
System.out.print("\nFilling blue color");
}//fillColor
}//BlueColor

4. Now create class GreenColor
public class GreenColor implements Color {
public void fillColor() {
System.out.print("\nFilling green color");
}//fillColor
}//GreenColor

5. Here we go for creating ColorFiller class which is going to hide complexity.
public class ColorFiller {
private Color redColor;
private Color blueColor;
private Color greenColor;

public ColorFiller() {
redColor = new RedColor();
blueColor = new BlueColor();
greenColor = new GreenColor();
}//ColorFiller

public void fillRedColor() {
redColor.fillColor();
}//fillRedColor

public void fillBlueColor() {
blueColor.fillColor();
}//fillBlueColor

public void fillGreenColor() {
greenColor.fillColor();
}//fillGreenColor
}//CollerFiller

6. Here Main class where we have to use this pattern.
public class Main {
public static void main(String args[]) {
ColorFiller cf = new ColorFiller();
cf.fillRedColor();
cf.fillBlueColor();
cf.fillGreenColor();
}//PSVM
}//Main

No comments:

Post a Comment