Design Pattern Clear View Part - 1

Design Pattern Clear View Part - 1

When we come to design patten. The following design patterns are making bit confusion of their usage.

(1) Facade Design Pattern

(2) Mediator Design Pattern

In this article, we will clearly understand their usage.

(1) Facade Design Pattern

  • It is a Structural Design Pattern.
  • It provides a simplified interface to a complex subsystem.
  • It hides complexity behind a higher-level API

Real World Example:

  1. Plug Socket - Behind a plug socket there are wires and connections that most people don't understand. But on top of the plug socket everyone know how to use.
  2. Operating System - When we click on button to open up an app on your computer, we don't see all the work the operating system is doing in the background.

When should we use the Facade Pattern in Object Oriented Design?

  • Consider any one of the object oriented app, that might have lots of classes that do lots of complicated stuff under the hood. If someone wanted to use the app, that might have to interact with lots of different classes that do lots of different bits of functionality. A facade is an object that deal with different classes functionality but the client has only interact with one object called facade.

(2) Mediator Design Pattern

  • It is a Behavioral Design Pattern.
  • The mediator pattern restrict direct communication between objects. This forces objects to collaborate via a mediator object, as a result, reduce chaotic dependencies.
  • it's ensure objects hides state from other objects.

Real World Example:

  1. Restaurant - The Customer is at their table and they need to communicate what they want to eat. The chefs are in the kitchen and they need to know what do cook. The owner of the restaurant needs to be collected money from the customer. in this scenario, a waiter or waitress is like a mediator who has handling all of these interactions.
  2. Air Traffic Controller - in airport where airplanes are waiting and moving on various runways. Pilots can talk to each other and try to figure out how to avoid collisions to their own, but this will be an impossible task due to the complexity, of the problem. This is why we need a mediator such as an Air Traffic Controller to centrally and effectively manage the communication among individual pilots.

When should we use the mediator pattern in Object Oriented Design?

  • When there are too many objects interacting with each other, individually, the complexity of the code increase dynamically, and its maintainability also decreases. In particular, a simple change in one object can have a huge ripple effect on the rest the code. We call this phenomenon tight coupling among objects. A mediator is an object that manage all the relationship between objects and it sit in between from one object to another object.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了