Facade design pattern in java | Java @ Desk
Home » , » Facade design pattern in java

Facade design pattern in java

Facade is design pattern in java that helps to re design a poorly structured API into a well defined API. It provides a simpler interface to the client, but internally it interacts with complex components and get a job done for the client. It hides a complexity behind exposing a simpler interface to the client.

Consider a scenario, of an Employee Hiring consultancy that collects all the information of an employee going to join in some company. The information includes personal details, last employment details, last drawn salay, expected salary, desired location.
Now, lets say, there are different complex interfaces, one that returns employee personal information, one give employee past experience details and so on.
So, in this case, if a client need a complete list of information of one particular employe, there needs to give a call to lots of other interface.

Here comes the importance of facade design patterns which creates a facade interface which contains a simplified interface, but internally it makes a call to all other complex interface to get collection of all employee details.



Employee.java - POJO for basic properties of employee

package com.designpatterns.facade;

public class Employee {

    // Personal details
    private int id;

    private String name;

    private String address;

    private String fatherName;

    private String maritalStatus;

    private String motherName;

    // Employment details
    private double yearsOfExperience;

    private double lastDrawnSalary;

    private String lastCompanyName;

    private boolean bgvDone; // Background verification done during hiring

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public String getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public String getMotherName() {
        return motherName;
    }

    public void setMotherName(String motherName) {
        this.motherName = motherName;
    }

    public boolean isBgvDone() {
        return bgvDone;
    }

    public void setBgvDone(boolean bgvDone) {
        this.bgvDone = bgvDone;
    }

    public double getYearsOfExperience() {
        return yearsOfExperience;
    }

    public void setYearsOfExperience(double yearsOfExperience) {
        this.yearsOfExperience = yearsOfExperience;
    }

    public double getLastDrawnSalary() {
        return lastDrawnSalary;
    }

    public void setLastDrawnSalary(double lastDrawnSalary) {
        this.lastDrawnSalary = lastDrawnSalary;
    }

    public String getLastCompanyName() {
        return lastCompanyName;
    }

    public void setLastCompanyName(String lastCompanyName) {
        this.lastCompanyName = lastCompanyName;
    }

    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Employee Personal Details : \n\n");
        stringBuffer.append("Employee Id : " + this.getId());
        stringBuffer.append("\nEmployee Name : " + this.getName());
        stringBuffer.append("\nEmployee Address : " + this.getAddress());
        stringBuffer.append("\nEmployee Father Name : " + this.getFatherName());
        stringBuffer.append("\nEmployee Mother Name : " + this.getMotherName());
        stringBuffer.append("\nEmployee Marital Status : " + this.getMaritalStatus());

        stringBuffer.append("\n\nEmployee Experience Details : \n\n");
        stringBuffer.append("\nEmployee Years of experience : " + this.getYearsOfExperience());
        stringBuffer.append("\nEmployee Last drawn salary : " + this.getLastDrawnSalary());
        stringBuffer.append("\nEmployee last company name : " + this.getLastCompanyName());
        if (this.isBgvDone())
            stringBuffer.append("\nEmployee Background verification done : Yes");
        else
            stringBuffer.append("\nEmployee Background verification done : No");
        return stringBuffer.toString();
    }

}

EmployeeExperience.java - Java class fetch the experience details of an employee

package com.designpatterns.facade;

public class EmployeeExperience {

    public void getExperieceDetails(Employee employee) {
        
        // Get the employee ID from employee object and perform DB operation to get past
        // experience details from DB table
        
        employee.setYearsOfExperience(5.2);
        employee.setLastDrawnSalary(45000);
        employee.setLastCompanyName("Cognizant");
        employee.setBgvDone(true);
    }
}
EmployeePersonalDetails.java - Java class fetch the personal details of an employee

package com.designpatterns.facade;

public class EmployeePersonalDetails {

    public void getPersonalDetails(Employee employee) {
        
        // Get the employee ID from employee object and perform DB operation to get personal
        // details from DB table
        
        employee.setName("Manish");
        employee.setAddress("Mumbai");
        employee.setFatherName("Sanjay");
        employee.setMaritalStatus("Married");
        employee.setMotherName("Asha");
        employee.setName("Kumar");
    }
}
EmployeeFacade.java - Facade class that will be exposed to client. Internally it performs complex operations

package com.designpatterns.facade;

public class EmployeeFacade {

    public void getEmployeeDetails(Employee employee) {
        new EmployeePersonalDetails().getPersonalDetails(employee);
        new EmployeeExperience().getExperieceDetails(employee);
    }
}

FacadeMain.java - Java class to test the facade implementation

package com.designpatterns.facade;

public class FacadeMain {

    public static void main(String args[]) {
        EmployeeFacade employeeFacade = new EmployeeFacade();
        Employee employee = new Employee();
        employee.setId(128);
        employeeFacade.getEmployeeDetails(employee);
        System.out.println(employee.toString());
    }
}





2 comments: