Facade Design Pattern
Definition: The Facade pattern provides a unified interface to access a set of interfaces within a subsystem. It defines a high-level interface that simplifies the use of the subsystem.
Usage Scenario Example: Consider a complex multimedia system comprising modules such as audio and video. The Facade pattern offers a simplified interface to manage these modules collectively, making external calls more straightforward.
Proxy Design Pattern
Definition: The Proxy pattern provides a proxy to control access to other objects. The proxy acts as an intermediary between the client and the target object, enabling additional processing before and after method calls.
Usage Scenario Example: Implementing the Proxy pattern in network requests facilitates lazy loading of images. The proxy class manages image loading: if the image is cached in memory, it returns it directly; otherwise, it loads from disk or network.
Adapter Design Pattern
Definition: The Adapter pattern converts the interface of one class into another interface expected by the client. It enables classes with incompatible interfaces to collaborate effectively.
Usage Scenario Example: Suppose the system includes an old email-sending class, but a new email-sending library with a different interface needs to be integrated. An adapter can be created to ensure compatibility between the new library and the existing system.
Decorator Design Pattern
Definition: The Decorator pattern enables adding new functionality to an existing object without modifying its structure. It is a structural design pattern that wraps existing classes.
Usage Scenario Example: Consider a graphical user interface library with a window class. To add features such as borders and scrollbars, the Decorator pattern allows adding these functionalities without modifying the window class by creating decorator classes.
Summary
Although all four patterns are structural design patterns, they solve different problems and are used in distinct scenarios:
- Facade provides a unified high-level interface for a group of interfaces in a subsystem, simplifying its use.
- Proxy is primarily used to control access to objects, allowing additional operations before and after invoking the target object's functionality.
- Adapter is mainly used to resolve interface incompatibility issues, enabling classes that cannot work together due to incompatible interfaces to collaborate.
- Decorator provides a flexible way to extend functionality by wrapping existing classes with decorator classes to add new features.