Design pattern "Facade"

Let's learn what is the "Facade" design pattern ๐Ÿ•น๏ธ๐ŸŽ๏ธ๐Ÿ’จ
Wednesday, July 10, 2024

Real world example

Imagine a car as a complex system. It has many parts like the engine, transmission, brakes, and so on. Each of these parts has its own subsystems and complexities. For example, the engine itself consists of pistons, a crankshaft, spark plugs, and many other components.

Now, as a driver, you donโ€™t need to know how all these parts and subsystems work. You just need to know how to operate the car, which is done through a simplified interface - the carโ€™s controls like the steering wheel, pedals, and yes, the ignition key.

Here, the carโ€™s controls act as a Facade. When you turn the key to start the car (or push the start button in modern cars), there are a lot of things happening under the hood. The battery sends a current to the ignition system, the spark plugs fire, the pistons start moving, and so on. But as a driver, you donโ€™t see any of this complexity. You just turn the key, and the car starts. This is the essence of the Facade Design Pattern - providing a simple interface to a complex system.

So, in software terms, the Facade Pattern is like the ignition key. It hides the complexity of the system and provides a simple interface to the client. The client doesnโ€™t need to know whatโ€™s happening behind the scenes; they just need to interact with the facade to get the job done. This makes the system easier to use and understand.

In plain words

Facade pattern provides a simplified interface to a complex subsystem.

Wikipedia definition

A facade is an object that provides a simplified interface to a larger body of code, such as a class library.

Programmatic example

// Complex subsystem components
class Engine {
public void start() {
System.out.println("Engine started");
}
public void stop() {
System.out.println("Engine stopped");
}
}
class Transmission {
public void engage() {
System.out.println("Transmission engaged");
}
public void disengage() {
System.out.println("Transmission disengaged");
}
}
class Brakes {
public void check() {
System.out.println("Brakes checked");
}
public void apply() {
System.out.println("Brakes applied");
}
}
// Facade
class Car {
private Engine engine;
private Transmission transmission;
private Brakes brakes;
public Car() {
this.engine = new Engine();
this.transmission = new Transmission();
this.brakes = new Brakes();
}
// Complex operations here
public void start() {
engine.start();
transmission.engage();
brakes.check();
System.out.println("Car started");
}
public void stop() {
brakes.apply();
transmission.disengage();
engine.stop();
System.out.println("Car stopped");
}
}
// Client code
public class Main {
public static void main(String[] args) {
// Here, using a car is easy
Car car = new Car();
car.start();
car.stop();
}
}

Diagram

Loading graph...

Some other examples

  1. Operating Systems: Operating systems use facade patterns to simplify complex processes. For example, when you click on a file to open it, the operating system performs a series of complex operations behind the scenes, but all you see is the file opening.
  2. E-commerce Platforms: E-commerce platforms use facade patterns to simplify the process of placing an order. The facade hides the complexity of tasks like stock checking, payment processing, and shipping.
  3. Databases: Database connections can be simplified using a facade to hide the complexity of connection, query preparation, execution, and result handling.
  4. Web Services or APIs: Facades are used to provide a simplified interface to complex web services or APIs, making it easier for developers to work with them.
  5. Game Programming: In game programming, a game engine might use a facade to hide the complexity of tasks like loading assets, handling user input, and managing game states.
  6. Banking Systems: Banking systems use facade patterns to simplify the process of making transactions, checking balances, and other banking operations.
  7. Home Automation Systems: In a smart home system, a facade pattern can be used to simplify the process of controlling various devices like lights, thermostats, and home security systems.
  8. Software Libraries/Frameworks: Software libraries often use facades to provide a simpler or more intuitive interface to a more complex underlying system.
  9. Content Management Systems: CMS like WordPress use facade patterns to simplify the process of creating, editing, and managing content.
  10. Mobile Applications: Mobile applications use facade patterns to simplify complex processes like data synchronization, user authentication, and notification handling.

Remember, the main goal of the Facade Pattern is to make a complex system easier to use by providing a simpler, high-level interface. It doesnโ€™t add new functionality; instead, it makes existing functionality easier to use.


Recommended articles