Skip to content

Latest commit

 

History

History
70 lines (48 loc) · 2.97 KB

File metadata and controls

70 lines (48 loc) · 2.97 KB

Facade Pattern

The Facade Pattern is a structural design pattern that provides a simplified interface to a set of interfaces in a subsystem. It hides the complexities of the subsystem and provides a single interface to the client, making it easier to use. Here are the pros and cons of using the Facade Pattern:

Example in Node.js:

In this example, the ComputerFacade provides a simplified interface to the complex subsystem consisting of CPU, Memory, and HardDrive classes. Clients only need to interact with the ComputerFacade to start the computer, hiding the complexities of the subsystem.

// Complex subsystem
class CPU {
  freeze() {}
  jump(position) {}
  execute() {}
}

class Memory {
  load(position, data) {}
}

class HardDrive {
  read(position, size) {}
}

// Facade
class ComputerFacade {
  constructor() {
    this.cpu = new CPU();
    this.memory = new Memory();
    this.hardDrive = new HardDrive();
  }

  start() {
    this.cpu.freeze();
    this.memory.load(0, this.hardDrive.read(0, 1024));
    this.cpu.jump(0);
    this.cpu.execute();
  }
}

// Usage
const computer = new ComputerFacade();
computer.start();

Now for the Pros & Cons

Pros:
  1. Simplified Interface: Facade provides a simplified interface to a complex subsystem, making it easier to use and understand for clients.

  2. Encapsulation: It encapsulates the complexities of the subsystem within the facade class, hiding implementation details and promoting loose coupling between the client and subsystem.

  3. Decoupling: Facade helps decouple the client from the subsystem, allowing changes to the subsystem's implementation without affecting the client code.

  4. Promotes Modularity: By providing a separate interface to interact with the subsystem, Facade promotes modularity and separation of concerns in the design.

  5. Reduced Dependency: Clients only need to depend on the facade class, rather than multiple classes within the subsystem, reducing dependency and potential maintenance issues.

Cons:
  1. Limited Functionality: Facade may provide a simplified interface, but it may also limit access to certain features or functionalities of the subsystem, which may be needed by some clients.

  2. Violation of Single Responsibility Principle: Facade classes may grow to accommodate multiple responsibilities related to interfacing with the subsystem, potentially violating the Single Responsibility Principle.

  3. Tight Coupling to Facade: Clients become tightly coupled to the facade class, which may lead to issues if changes to the subsystem require corresponding changes to the facade interface.

  4. Decreased Visibility: Since Facade hides the complexities of the subsystem, it may decrease visibility into the subsystem's operations, making it harder to debug or troubleshoot issues.

  5. Additional Abstraction Layer: Introducing a facade adds an additional abstraction layer to the design, which may add complexity and overhead, especially in simple systems.