-
Notifications
You must be signed in to change notification settings - Fork 2
2. Structural Patterns
Ercan Polat edited this page Jan 12, 2019
·
6 revisions
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Use the Adapter pattern when… You have:
- A domain-specific interface.
- A class to connect to with a mismatching interface. You want to:
- Create a reusable class to cooperate with yet-to-be-built classes.
- Change the names of methods as called and as implemented.
- Support different sets of methods for different purposes. Choose the Adapter you need… Class adapter Simple and versatile, invisible to the client. Object adapter Extensible to sub-classes of the adapter. Two-way adapter Enables different clients to view an object differently. Pluggable adapter Presence of adapter is transparent; it can be put in and taken out Several adapters can be active.
Use the Bridge pattern when… You can:
- Identify that there are operations that do not always need to be implemented in the same way. You want to:
- Completely hide implementations from clients.
- Avoid binding an implementation to an abstraction directly.
- Change an implementation without even recompiling an abstraction.
- Combine different parts of a system at runtime.
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Use the Composite pattern when… You have:
- An irregular structure of objects and composites of the objects You want:
- Clients to ignore all but the essential differences between individual objects and composites of objects
- To treat all objects in a composite uniformly But consider using as well:
- The Decorator pattern to provide operations like Add, Remove, and Find
- The Flyweight pattern to share components, provided the notion of “where I am” can be disregarded and all operations start at the root of the composite
- The Visitor pattern to localize the operations that are currently distributed between the Composite and Component classes
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Use the Decorator pattern when… You have: • An existing component class that may be unavailable for subclassing. You want to:
- Attach additional state or behavior to an object dynamically.
- Make changes to some objects in a class without affecting others.
- Avoid subclassing because too many classes could result. But consider using instead:
- The Adapter pattern, which sets up an interface between different classes.
- The Composite pattern, which aggregates an object without also inheriting its interface.
- The Proxy pattern, which specifically controls access to objects.
- The Strategy pattern, which changes the original object rather than wrapping it.
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Use the Facade pattern when… A system has several identifiable subsystems and:
- The abstractions and implementations of a subsystem are tightly coupled.
- The system evolves and gets more complex, but early adopters might want to retain their simple views.
- You want to provide alternative novice, intermediate, and “power user” interfaces.
- There is a need for an entry point to each level of layered software.
But consider using instead:
- The Abstract Factory pattern for designs where subsystems create objects on behalf of the client. Choose the Facade you need… Opaque Subsystem operations can only be called through the Facade. Transparent Subsystem operations can be called directly as well as through the Facade. Singleton Only one instance of the Facade is meaningful.
Use the Flyweight pattern when… There are:
- Many objects to deal with in memory
- Different kinds of state, which can be handled differently to achieve space savings
- Groups of objects that share state
- Ways of computing some of the state at runtime You want to:
- Implement a system despite severe memory constraints
Use the Proxy pattern when… You have objects that:
- Are expensive to create.
- Need access control.
- Access remote sites.
- Need to perform some action whenever they are accessed. You want to:
- Create objects only when their operations are requested.
- Perform checks or housekeeping on objects whenever accessed.
- Have a local object that will refer to a remote object.
- Implement access rights on objects as their operations are requested.