Chapter 17: Facade Pattern

大綱

When should you use it?

  • The facade pattern is a structural pattern that provides a simple interface to a complex system.

    • The facade: provides simple methods to interact with the system. This allows consumers to use the facade instead of knowing about and interacting with multiple classes in the system.

    • The dependencies: are objects owned by the facade. Each dependency performs a small part of a complex task.

  • When should you use it?

    • Use this pattern whenever you have a system made up of multiple components and want to provide a simple way for users to perform complex tasks.

Playground example

  • 目標: a product ordering system involves several components: customers and products, inventory in stock, shipping orders and others.

    • provide a facade to expose common tasks such as placing and fulfilling a new order

  • Dependencies

  • Facade

What should you be careful about?

  • Be careful about creating a “god” facade that knows about every class in your app.

  • It’s okay to create more than one facade for different use cases.

Tutorial project

Key points

  • The facade pattern provides a simple interface to a complex system. It involves two types: the facade and its dependencies.

  • The facade provides simple methods to interact with the system. Behind the scenes, it owns and interacts with its dependencies, each of which performs a small part of a complex task.

Last updated