Wednesday, July 9, 2014

Facade Design Pattern

INTRODUCTION

In real life, we always look for an easy way to do our job by finding someone who can take care of our work.

USECASE

Applying for PASSPORT in India. We look for any agency instead we directly go the concerned office and apply with our details.

And we also what are all the process has been carried out and how they can perform to validate our person details to give the passport. Simply we are submitting the details and look for the delivery of the Passport hard-copy to our address.

The passport office does all the verification process for us and finally approves our passport by some verification.

Here the verification process is acting as an Interface, between me & passport office, which accepts my details and finally returns the result (Passport) what I have required.
Very importantly, the Person will not aware of the back-end process.

IMPLEMENTATION

Lets us create Model Class - Person.
public class Person {

 private String name;
 private String address;
 private String phone;
 private String dob;

 public String getDob() {
   return dob;
 }

 public void setDob(String dob) {
   this.dob = dob;
 }

 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 getPhone() {
   return phone;
 }

 public void setPhone(String phone) {
   this.phone = phone;
 }
}


Create an interface for the background verification process.

public interface Verification {
 boolean verify(String personDetail);
}

Now verify the person’s phone number.

public class PhoneNumberVerification implements Verification {
 @Override
 public boolean verify(String phone) {
   return true;
 }
}

Verify the address.

public class AddressVerification implements Verification {
 @Override
 public boolean verify(String address) {
   return true;
 }
}

Verify the Date of Birth Certificate.

public class DateOfBirthVerification implements Verification {
 @Override
 public boolean verify(String personDetail) {
   return true;
 }
}


Check whether the person involved in any criminal activity by checking with concerned police station to check about any case filed on him.

public class PoliceCaseVerification implements Verification {
 @Override
 public boolean verify(String personDetail) {    
   return true;
 }
}

Now the passport has to verify all the details one by one and finally approve or disapprove the passport.

public class PassportProcess {

 public static boolean verify(Person person) {
   Verification phoneNumber = new PhoneNumberVerification();
   Verification address = new AddressVerification();
   Verification dob = new DateOfBirthVerification();
   Verification policeCase = new PoliceCaseVerification();

   return phoneNumber.verify(person.getPhone())
       && address.verify(person.getAddress())
       && dob.verify(person.getDob())
       && policeCase.verify(person.getName());
 }
}

Now time to test the facade pattern.

public class PassportOffice {

 public static void main(String[] args) {

   // Person Details
   Person person = new Person();
   person.setName("Ranjith");
   person.setAddress("#5, Gandhi Street, T.Nagar, Chennai");
   person.setPhone("9898989898");

   // Validate the person by calling the facade.
   System.out.println(PassportProcess.verify(person) ? "Passport Approved" : "Passport Rejected");
 }
}

OUTPUT


Passport Approved

Hope this example clarifies the Facade Design Pattern. Please share your thoughts in the comment box.
Happy Knowledge Sharing!!!

No comments :

Post a Comment