How to implement Facade pattern in Java?

Photo by Leonard Dahmen on Pexels.com

Facade is one of the easiest patterns to implement.

And if one has not come across the “Gang of Four Design Patterns” they probably already would have used facade pattern.

Facade just provides a single interface to a set of sub systems.

Let me explain.

Let’s say you are writing an application for an eCommerce website.

When a client orders an item , you typically do the following:

  • Add the items in the order to cart
  • Calculate the cost of all items in the cart
  • Debit money from user account
  • Pack all items
  • Deliver items

This is a simplistic algorithm just for the purpose of describing this pattern .

We may have different sub systems to take care of each of this step, let’s say different classes.

A CartManager to manage the cart

A CostCalculator to calculate the cost of the items

A MoneyManager to debit money from user account

An OrderPacker to pack all the items in the order

A DeliveryManager to deliver all the items.

Instead of making the client interact with all these classes to perform the individual steps we provide a common interface , named ECommerceFacade class with a single method orderItems() which in turn interacts with all the sub systems.

Here is the client code :

package structural.facade.pattern;

public class Client {
	
	
	public static void main(String a[]) {
		
		Order order = new Order();
		order.setOrderId("1001");
		
		order.setItems(new String[] {"shoes","socks","bag"});
		
		order.setQuantity(new int[] {2,6,1});
		
		
		
		ECommerceFacade facade = new ECommerceFacade();
		facade.orderItems(order);
		
		
	}

}

We just create an order and order it using the Facade class method orderItems()

Here is the Facade class:

package structural.facade.pattern;

public class ECommerceFacade {

	
	public void orderItems(Order order) {
		
		
		CartManager cartManager = new CartManager();
		cartManager.addToCart(order);
		
		CostCalculator costCalculator = new CostCalculator();
		costCalculator.calculateCost(order);
		
		MoneyManager moneyManager = new MoneyManager();
		moneyManager.debitMoney(order);
		
		OrderPacker packer = new OrderPacker();
		packer.packItems(order);
		
		DeliveryManager deliveryManager = new DeliveryManager();
		deliveryManager.deliverItems(order);
		
	}
}

Here are the individual sub systems:

The CartManager:

package structural.facade.pattern;

public class CartManager {

	
	public void addToCart(Order order) {
		
		System.out.println("Adding item to cart");
		System.out.println("Added item : "+order);
	}
}

The CostCalculator:

package structural.facade.pattern;

public class CostCalculator {

	
	public void calculateCost(Order order) {
		
		System.out.println("Calculating cost for order :"+order.getOrderId());
	}
}

The DeliveryManager:

package structural.facade.pattern;

public class DeliveryManager {
	
	public void deliverItems(Order order) {
		
		System.out.println("Delivering items for the order :"+order.getOrderId());
	}

}

The MoneyManager:

package structural.facade.pattern;

public class MoneyManager {

	public void debitMoney(Order order) {
		
		
		System.out.println("Debiting money for order: "+order.getOrderId());
	}
}

The OrderPacker:

package structural.facade.pattern;

public class OrderPacker {
	
	
	public void packItems(Order order) {
		
		System.out.println("Packing items for order: "+order.getOrderId());
	}

}

That’s it!

That is Facade Design Pattern in action.

Quoting the Gang of Four :

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use

Here is the code :

https://github.com/vijaysrj/designPatternsGoF/tree/master/src/structural/facade/pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s