Facade Pattern

Facade Pattern Java Design Pattern Tutorial
Facade Pattern: defines a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.

Example

CarServiceCenter.java
package com.facadepattern.example;

public class CarServiceCenter {
 Engine engine;
 Exterior exterior;
 Interior interior;
 Tyres tyres;
 
 public CarServiceCenter(Engine engine, Exterior exterior, Interior interior,Tyres tyres) {
  this.engine = engine;
  this.exterior = exterior;
  this.interior = interior;
  this.tyres = tyres;
 }
 
 public void serviceCar(){
  System.out.println("Starting your Car Service");
  engine.changeOil();
  interior.clean();
  exterior.wash();
  tyres.checkTyres();
  exterior.polish();
  System.out.println("Completed your Car Service");
 }
}
Engine.java
package com.facadepattern.example;

public class Engine {
 public void changeOil(){
  System.out.println("Changing Engine Oil");
 }
}
Exterior.java
package com.facadepattern.example;

public class Exterior {
 public void wash(){
  System.out.println("Washing the Exterior");
 }
 
 public void polish(){
  System.out.println("Applying the polish");
 }
} 
Interior.java
package com.facadepattern.example;

public class Interior {
 public void clean(){
  System.out.println("Cleaning the Interior");
 }
} 
Tyres.java
package com.facadepattern.example;

public class Tyres {
 public void checkTyres(){
  System.out.println("checking the Tyres for wear");
 }
}
CarServiceTestDrive.java
package com.facadepattern.example;

public class CarServiceTestDrive {
 public static void main(String[] args) {
  CarServiceCenter serviceCenter = new CarServiceCenter(new Engine(), new Exterior(), new Interior(), new Tyres());
  serviceCenter.serviceCar();
 }
}



Home

No comments:

Post a Comment