Facade Pattern in Java

Facade Pattern is a Structural Pattern in which, a set of interfaces is gathered into only one interface that Client can work easily and simply. Facade Pattern helps us to wrap complexities of components inside and lose coupling between Client and subSystems.

I. OVERVIEW

Facade Pattern defines a higher interface/object – Facade – that wraps some lower interfaces/objects. Whenever Client wants to make a chain of complex actions which require many methods and interaction, it just call one simple Facade method, all operation can be done inside that method.
facade-pattern-overview
You can see that Facade object contains other interfaces/objects (A, B, C) and manages their operation by doWork() method.

II. PRACTICE

1. Project Overview

Assume that we have some operations such as: register, consultation, healthcare that a Customer will be served when applying for our company’s Customer Services.

With Facade Pattern, Client only need to create Customer Information, then calls FacadeService setServiceforCustomer() method. All works containing register information, Consultation Service and HealhCare Service will be processed behind.
facade-pattern-demo

2. Step by Step

2.1- Create Customer class:


package com.javasampleapproach.facade.pattern;

public class Customer {

	private String name;

	public Customer(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "Customer [name=" + name + "]";
	}
}

2.2- Create ICustomerService interface:


package com.javasampleapproach.facade.pattern;

public interface ICustomerService {

	public void serve(String name);
}

2.3- Create implementations of ICustomerService interface:

Consultation.java


package com.javasampleapproach.facade.pattern;

public class Consultation implements ICustomerService {

	@Override
	public void serve(String name) {
		System.out.println("Consultation for customer: " + name);
	}
}

HealthCare.java


package com.javasampleapproach.facade.pattern;

public class HealthCare implements ICustomerService {

	@Override
	public void serve(String name) {
		System.out.println("Healthcare for customer: " + name);
	}
}

2.4- Create FacadeService class:


package com.javasampleapproach.facade.pattern;

public class FacadeService {

	private ICustomerService consultation;
	private ICustomerService healthcare;

	public FacadeService() {
		consultation = new Consultation();
		healthcare = new HealthCare();
	}
	
	public void setServiceforCustomer(Customer customer) {
		registerCustomer(customer.getName());
		consultation.serve(customer.getName());
		healthcare.serve(customer.getName());
	}
	
	private void registerCustomer(String name) {
		System.out.println("register for customer: " + name);
	}
}

2.5- Create Client Test Class:


package com.javasampleapproach.facade;

import com.javasampleapproach.facade.pattern.Customer;
import com.javasampleapproach.facade.pattern.FacadeService;

public class MainApp {

	public static void main(String[] args) {
		
		Customer customer = new Customer("Jack Smith");
		
		FacadeService service = new FacadeService();
		service.setServiceforCustomer(customer);
	}
}

2.6- Run the code, the console window shows:


register for customer: Jack Smith
Consultation for customer: Jack Smith
Healthcare for customer: Jack Smith

III. SOURCE CODE

FacadePattern



By grokonez | March 17, 2017.

Last updated on May 3, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*