Facade Design Pattern In Dart/Flutter

The Facade design pattern is one of the twenty-three well-known GoF design patterns describing how to solve recurring design problems to design flexible and reusable object-oriented software. I recommend you to go through my article to get a detailed explanation of the design pattern series. In this article, we'll cover the Facade design pattern.

  1. What is the Facade Design Pattern?
  2. An Example of a Facade Design Pattern?
  3. When to use?
  4. Pitfalls
  5. Conclusion

Let's get started without any further delay.

What is the Facade design pattern?

According to the book "Design Patterns, Elements of Reusable Object-Oriented Software", Facade Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Let us consider a scenario where you are a guest checking into a hotel. There, you need to interact with the people at reception for services like check-in, room service, and laundry. The front desk simplifies the process, hiding the complexities of the underlying operations (such as housekeeping, kitchen, and laundry services) and providing an easy interface for guests.

Sub system

An Example of a Facade Design Pattern

This Dart code demonstrates the Facade Design Pattern using a hotel operation scenario. The hotel comprises various subsystems such as housekeeping, kitchen, and laundry. The Facade Design Pattern simplifies the interface to these subsystems, making them easier to use.

// Housekeeping subsystem
class Housekeeping {
  void doHousekeeping() {
    print('Doing housekeeping...');
  }
}

// Kitchen subsystem
class Kitchen {
  void prepareFood() {
    print('Preparing food...');
  }
}

// Laundry subsystem
class Laundry {
  void doLaundry() {
    print('Doing Laundry...');
  }
}

// Facade class
class HotelFacade {
  // Private instances of the subsystems
  Housekeeping _housekeeping;
  Kitchen _kitchen;
  Laundry _laundry;

  // Constructor to initialize the subsystems
  HotelFacade(this._housekeeping, this._kitchen, this._laundry);

  // Method providing a simplified interface to the subsystems
  void frontDeskService() {
    _housekeeping.doHousekeeping();
    _kitchen.prepareFood();
    _laundry.doLaundry();
  }
}

// Main function
void main() {
  // Create instances of the subsystems
  Housekeeping housekeepingObj = Housekeeping();
  Kitchen kitchenObj = Kitchen();
  Laundry laundryObj = Laundry();
  
  // Create an instance of the Facade class
  HotelFacade hotel = HotelFacade(housekeepingObj, kitchenObj, laundryObj);
  
  // Use the Facade to interact with the subsystems
  hotel.frontDeskService();
}

The HotelFacade class acts as the facade, offering a simplified front desk service () method for interacting with the subsystems(Housekeeping, Kitchen, and Laundry). This method encapsulates the complexities of the subsystems, making them easier to use.

When To Use?

  • When you want to create a simple interface for a complex subsystem.?
  • When there are numerous interdependent classes in a subsystem, it can lead to increased complexity.?
  • When you want to separate clients from the components of the subsystem.?

Pitfalls

  • A facade can oversimplify a system, which may be problematic for clients who need more control or access to the underlying complexity.
  • Using a facade in a new system may indicate underlying design issues. It is often more suitable as a refactoring tool for existing complex systems.
  • As a facade tends to combine functionalities, it is essential to avoid becoming a "god object" that violates the Single Responsibility Principle.

Conclusion

In this article, we covered the Facade Design Pattern, a great way to simplify interactions with complex systems. It provides a simple interface to a complex subsystem by creating a higher-level interface, making the subsystem more straightforward to use and understand. However, like any design pattern, it should be used judiciously, considering the project's needs. If you find my articles useful, connect with me on LinkedIn, give your feedback, and keep motivating me to create more content like this.


Similar Articles