tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Design Patterns > Java Design Patterns > Facade : House Builder Example

Facade : House Builder Example 

Facade pattern comes under Structural design pattern. It provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Behaviour & Advantages

  • Simplifies the subsystem usage.
  • Acts as entry point to the subsystem.
  • Shields clients from subsystem components.
  • The interaction and communication among subsystems is hidden from client.
  • Facade
    Provides a simplified API to interact with subsystems. It knows how to handle a request and manages request processing with subsystems.
  • Subsystem classes
    Complex set of classes designed to perform a specific task.
  • Client
    One who uses Facade to interact with subsystems.

This example shows a HouseBuilder class which shield the House Owner or client from directly interacting with subsystems such as designers, government bodies, material supplier etc.

HouseBuilder and subsystem classes are shown below,

File Name  :  
com/bethecoder/tutorials/dp/facade/HouseBuilder.java 
   
package com.bethecoder.tutorials.dp.facade;

/**
 * House builder makes the task easier for owner by interacting
 * with the subsystems. House owner doesn't required to know
 * anything about sub systems.
 *  
 * The three core subsystems the facade is 
 * going to interact with is listing below,
 */
import com.bethecoder.tutorials.dp.facade.core.HouseMaterialSupplier;
import com.bethecoder.tutorials.dp.facade.core.Workers;

import com.bethecoder.tutorials.dp.facade.design.CivilEngineer;
import com.bethecoder.tutorials.dp.facade.design.InteriorDesigner;

import com.bethecoder.tutorials.dp.facade.gov.CurrentSupplier;
import com.bethecoder.tutorials.dp.facade.gov.WaterSupplier;

public class HouseBuilder {

  private CivilEngineer civilEngineer;
  private Workers workers;
  private HouseMaterialSupplier houseMaterialSupplier;
  private WaterSupplier waterSupplier;
  private CurrentSupplier currentSupplier;
  private InteriorDesigner interiorDesigner;
  
  public HouseBuilder() {
    civilEngineer = new CivilEngineer();
    workers = new Workers();
    houseMaterialSupplier = new HouseMaterialSupplier()
    waterSupplier = new WaterSupplier();
    currentSupplier = new CurrentSupplier();
    interiorDesigner = new InteriorDesigner();
  }
  
  public void buildNewHouse() {
    civilEngineer.getHousePlan();
    civilEngineer.explainHousePlan(workers);
    workers.startWork("DAILY""8AM""6PM");
    houseMaterialSupplier.getRawMaterial(500000);
    waterSupplier.getWarterConnection();
    currentSupplier.getPowerSupply();
    interiorDesigner.designInteriors();
    System.out.println("House contruction completed");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/design/CivilEngineer.java 
   
package com.bethecoder.tutorials.dp.facade.design;

import com.bethecoder.tutorials.dp.facade.core.Workers;

public class CivilEngineer {

  public void getHousePlan() {
    System.out.println("House plan completed by Civil Engineer");
  }
  
  public void explainHousePlan(Workers workers) {
    System.out.println("Civil Engineer explained plan to Workers");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/core/HouseMaterialSupplier.java 
   
package com.bethecoder.tutorials.dp.facade.core;

public class HouseMaterialSupplier {

  public void getRawMaterial(int payment) {
    System.out.println("Payed Rs." + payment + " and received house contruction material");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/core/Workers.java 
   
package com.bethecoder.tutorials.dp.facade.core;

public class Workers {

  public void startWork(String interval, String startTime, String endTime) {
    System.out.println("Workers started working " 
        interval + " from " + startTime + " to " + endTime);
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/gov/CurrentSupplier.java 
   
package com.bethecoder.tutorials.dp.facade.gov;

public class CurrentSupplier {

  public void getPowerSupply() {
    System.out.println("Got power supply");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/gov/WaterSupplier.java 
   
package com.bethecoder.tutorials.dp.facade.gov;

public class WaterSupplier {

  public void getWarterConnection() {
    System.out.println("Got new water connection");
  }
}
   

File Name  :  
com/bethecoder/tutorials/dp/facade/design/InteriorDesigner.java 
   
package com.bethecoder.tutorials.dp.facade.design;

public class InteriorDesigner {

  public void designInteriors() {
    System.out.println("House interior design completed");
  }
}
   

Facade usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/facade/Test.java 
   
package com.bethecoder.tutorials.dp.facade;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {

    HouseBuilder facade = new HouseBuilder();
    facade.buildNewHouse();
  }

}
   

It gives the following output,
House plan completed by Civil Engineer
Civil Engineer explained plan to Workers
Workers started working DAILY from 8AM to 6PM
Payed Rs.500000 and received house contruction material
Got new water connection
Got power supply
House interior design completed
House contruction completed


                    Print

   



Books Recommendation

 
 
bl  br