Structural Patterns Flashcards
Purpose Defines an abstract object structure independently of the implementation object structure in order to limit coupling.
Use When
- Abstractions and implementations should not be bound at compile time.
- Abstractions and implementations should be independently extensible.
- Changes in the implementation of an abstraction should have no impact on clients.
- Implementation details should be hidden from the client
BRIDGE
Permits classes with disparate interfaces to work together by creating a common object by which they may communicate and interact.
Use When
- A class to be used doesn’t meet interface requirements.
- Complex conditions tie object behavior to its state.
- Transitions between states need to be explicit.
ADAPTER
Facilitates the creation of object hierarchies where each object can be treated independently or as a set of nested objects through the same interface.
Use When
- Hierarchical representations of objects are needed.
- Objects and compositions of objects should be treated uniformly
COMPOSITE
Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors.
Use When
- Object responsibilities and behaviors should be dynamically modifiable.
- Concrete implementations should be decoupled from responsibilities and behaviors.
- Subclassing to achieve modification is impractical or impossible.
- Specific functionality should not reside high in the object hierarchy.
- A lot of little objects surrounding a concrete implementation is acceptable.
Facilitates the reuse of many fine grained objects, making the utilization of large numbers of objects more efficient.
Use When
- Many like objects are used and storage cost is high.
- The majority of each object’s state can be made extrinsic.
- A few shared objects can replace many unshared ones.
- The identity of each object does not matter.
FLYWEIGHT
Supplies a single interface to a set of interfaces within a system.
Use When
- A simple interface is needed to provide access to a complex system.
- There are many dependencies between system implementations and clients.
- Systems and subsystems should be layered.
FACADE
Sometimes the information displayed in a shopping cart is the product of a single item while other times it is an aggregation of multiple items. By implementing items as composites we can treat the aggregates and the items in the same way, allowing us to simply iterate over the tree and invoke functionality on each item. By calling the getCost() method on any given node we would get the cost of that item plus the cost of all child items, allowing items to be uniformly treated whether they were single items or groups of items.
COMPOSITE
A billing application needs to interface with an HR application in order to exchange employee data, however each has its own interface and implementation for the Employee object. In addition, the SSN is stored in different formats by each system. By creating an adapter we can create a common interface between the two applications that allows them to communicate using their native objects and is able to transform the SSN format in the process.
ADAPTER
The Java Virtual Machine (JVM) has its own native set of functions that abstract the use of windowing, system logging, and byte code execution but the actual implementation of these functions is delegated to the operating system the JVM is running on. When an application instructs the JVM to render a window it delegates the rendering call to the concrete implementation of the JVM that knows how to communicate with the operating system in order to render the window.
BRIDGE
Many businesses set up their mail systems to take advantage of this pattern. When messages are sent from someone in the company to an external address the mail server decorates the original message with copyright and confidentiality information. As long as the message remains internal the information is not attached. This decoration allows the message itself to remain unchanged until a runtime decision is made to wrap the message with additional information.
DECORATOR
By exposing a set of functionalities through a web service the client code needs to only worry about the simple interface being exposed to them and not the complex relationships that may or may not exist behind the web service layer. A single web service call to update a system with new data may actually involve communication with a number of databases and systems, however this detail is hidden due to the implementation of the façade pattern.
FACADE
Systems that allow users to define their own application flows
and layouts often have a need to keep track of large numbers of
fields, pages, and other items that are almost identical to each
other. By making these items into flyweights all instances of each
object can share the intrinsic state while keeping the extrinsic
state separate. The intrinsic state would store the shared properties,
such as how a textbox looks, how much data it can hold, and
what events it exposes. The extrinsic state would store the
unshared properties, such as where the item belongs, how to
react to a user click, and how to handle events.
FLYWEIGHT