DEV Community

Discussion on: Angular — Facade Design Pattern and how it can improve performance

Collapse
 
maxime1992 profile image
Maxime

You've articulated most of your article around the fact that the facade pattern will "improve the performance of your Angular applications".

Can you explain why it would? IMO, this pattern won't improve performances. It's a way to organise code and abstract some parts of it so that consumers aren't aware of some underlying implementations. But has nothing to do with runtime performances.

A good example I can think of is having a facade to mix data that comes from a store (ngrx for example) and data that aren't stored in it (let say a running timer for example, defined as an rxjs observable). Any component consuming the mixed data between the 2 wouldn't need to be aware of the complexity happening behind the scenes, which is nice. But at no point it'll improve the performances.

The FacadeService you wrote with AuthService, CartService, PaymentService is an anti pattern IMO. You're only putting every services in the same bag, with no additional value: no method combining multiple services, each service is already well isolatedm they're already provided as services so having a facade won't help for testing any further and now 2 separate components have access to way more than they should:

  • LoginComponent can pay, addToCart, removeFromCart
  • CartComponent can login

I think facades overall are a great pattern in some scenarios but should not always be used. If the abstraction layer in place is already enough for a simple case, using a facade will not help.

This is just my personal opinion.

Collapse
 
vivekdogra02 profile image
V.D

Hi Maxime,

Thank you for sharing your perspective and concerns regarding the use of the Facade pattern in terms of performance and code organization. You raise valid points, and I appreciate the opportunity to address them.

You are correct that the primary purpose of the Facade pattern is not to improve runtime performance but rather to provide a simplified interface and abstraction for consuming code. I apologize for any confusion caused by the mention of performance improvement. The primary benefits of the Facade pattern lie in code organization, encapsulation, and simplification of complex subsystems.

Regarding the example of mixing data from different sources, such as a store and a running timer, using a Facade can indeed provide a convenient way to abstract the complexity and present a unified interface to consumers. This abstraction can enhance code readability, maintainability, and ease of use. However, as you rightly pointed out, it does not directly impact runtime performance.

Regarding the example of the FacadeService with AuthService, CartService, and PaymentService, I understand your concerns. It's essential to carefully consider the context and requirements when applying the Facade pattern. While the Facade pattern can be useful for consolidating and simplifying access to multiple services, it should be used judiciously. In cases where the services are already well-isolated, and there are no shared operations or complex interactions between them, the use of a Facade might not provide significant additional value.

It's important to strike a balance between code organization, encapsulation, and the complexity introduced by adding a Facade.
In some cases, the existing abstraction layers provided by Angular itself, such as component composition, dependency injection, or module separation, might be sufficient. Evaluating the trade-offs and considering the specific needs of the application are crucial when deciding whether to introduce a Facade or rely on existing patterns and abstractions.

Ultimately, the choice of whether to use the Facade pattern or not depends on the specific requirements, complexity, and maintainability goals of the application. It is not a one-size-fits-all solution, and thoughtful consideration should be given to its application.

Thank you for raising these points, and I hope this response clarifies the purpose and considerations surrounding the Facade pattern in Angular development. If you have any further questions or concerns, please feel free to ask.