Design Patterns in Dart: Facade Pattern

the Facade Pattern in Dart — part of your ongoing Design Patterns in Dart series. It includes explanations, a real-world analogy, and fully runnable Dart code to help your readers grasp this structural pattern clearly.


Design Patterns in Dart: Facade Pattern

What is the Facade Pattern?

The Facade Pattern is a structural design pattern that:

Provides a simplified interface to a larger, more complex system.

It hides all the inner workings of subsystems behind a single, unified interface — making it easier for clients to use without dealing with complexity.


Real-World Analogy

Imagine you’re using a remote control:

  • Behind the scenes: multiple components work together — power circuit, audio decoder, screen module…
  • But you don’t care — you just press one button.

In software, the remote control is your Facade — it provides simple access to complex logic.


When to Use the Facade Pattern

  • When you want to simplify complex APIs.
  • To provide a unified interface to subsystems.
  • To decouple clients from complex inner workings.
  • When integrating with legacy code or multiple libraries.

Dart Example: Home Theater System

Let’s model a simplified Home Theater System:

  • Subsystems:
    • Amplifier
    • DVDPlayer
    • Projector
    • Lights
  • The Facade:
    • HomeTheaterFacade

Step 1: Define Subsystems

class Amplifier {
void on() => print("Amplifier on");
void off() => print("Amplifier off");
void setVolume(int level) => print("Amplifier volume set to $level");
}

class DVDPlayer {
void on() => print("DVD Player on");
void off() => print("DVD Player off");
void play(String movie) => print("Playing '$movie'");
void stop() => print("Stopping movie");
}

class Projector {
void on() => print("Projector on");
void off() => print("Projector off");
void wideScreenMode() => print("Projector set to widescreen mode");
}

class Lights {
void dim(int level) => print("Lights dimmed to $level%");
void on() => print("Lights on");
}

Step 2: Create the Facade Class

class HomeTheaterFacade {
final Amplifier amp;
final DVDPlayer dvd;
final Projector projector;
final Lights lights;

HomeTheaterFacade(this.amp, this.dvd, this.projector, this.lights);

void watchMovie(String movie) {
print("\nGet ready to watch a movie...");
lights.dim(10);
amp.on();
amp.setVolume(5);
projector.on();
projector.wideScreenMode();
dvd.on();
dvd.play(movie);
}

void endMovie() {
print("\nShutting movie theater down...");
lights.on();
amp.off();
projector.off();
dvd.stop();
dvd.off();
}
}

Step 3: Client Code

void main() {
// Setup subsystems
final amp = Amplifier();
final dvd = DVDPlayer();
final projector = Projector();
final lights = Lights();

// Create the facade
final homeTheater = HomeTheaterFacade(amp, dvd, projector, lights);

// Client uses the simplified interface
homeTheater.watchMovie("Inception");
homeTheater.endMovie();
}

Output:

Get ready to watch a movie...
Lights dimmed to 10%
Amplifier on
Amplifier volume set to 5
Projector on
Projector set to widescreen mode
DVD Player on
Playing 'Inception'

Shutting movie theater down...
Lights on
Amplifier off
Projector off
Stopping movie
DVD Player off

Key Points

  • The subsystems remain independent.
  • The Facade simplifies how the client interacts with the system.
  • Clients don’t need to know the internal structure or operation order.

Benefits of Facade Pattern

AdvantageDescription
SimplicityReduces complexity of the API
DecouplingClients don’t interact with subsystems directly
EncapsulationHides implementation details
Better maintainabilityChanges in subsystems don’t affect the client

Drawbacks

  • The Facade can become a God class if it tries to handle everything.
  • If overused, it can hide powerful features of the subsystem.
  • It may add unnecessary abstraction if the subsystems are already simple.

Flutter & Dart Use Cases

  • Shared Preferences plugin: hides platform channels behind simple API.
  • Dio HTTP client: provides a facade over Dart’s HttpClient, interceptors, etc.
  • You can create facades in your app for:
    • Services like Auth (AuthService)
    • Complex workflows (e.g. PDF generation, payment processing)

Summary

PropertyFacade Pattern
Pattern TypeStructural
Problem SolvedSimplify complex systems
Key BenefitUnified, clean interface
Good In Flutter?Yes, excellent for encapsulating services

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Lên đầu trang