Midterm Review 2 Flashcards
What is the Builder pattern?
An object creational pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations.
What is the motivation for the Builder pattern?
It is useful when given data can have multiple complex representations and when similar types of objects can have vastly different construction processes
When should you use the Builder pattern?
When the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled; When the construction process must allow different representations for the object that’s constructed
What are the participants of the Builder pattern?
- Builder: Specifies the abstract methods for building each part of the object
- ConcreteBuilder: Handles the construction of the individual parts of each object implementation, maintains a representation of the object as it is constructed and returns the newly created object
- Director: Accepts a builder and uses it to construct an object
- Product: Represents the constructed object.
What are the pros and cons of the Builder pattern?
Pros: allows you to vary your products’ internal representation and hides how assembly actually takes place, isolates code construction and representation, offers finer control over construction process
What is the Composite pattern?
An object structural pattern which composes objects into tree structures to represent part-whole hierarchies and lets clients treat individual objects as compositions of objects uniformly.
What is the motivation for the Composite pattern?
Using a composite, graphics components can be handled interchangeably whether they are the final graphic being drawn, or if they are a parent component that comprises further graphics
When should you use the Composite pattern?
When you want to represent part-whole hierarchies of objects or when you want clients to be able to ignore the difference between compositions of individual objects (clients will treat all objects in the composite structure uniformly)
What are the participants of the Composite pattern?
- Component: Declares the interface for objects in the composition, implements the default behaviour for the interface common to all
classes, as appropriate, and declares an interface for accessing and managing its child components - Leaf: Represents leaf objects in the composition (a leaf has no children), defines behaviour for primitive objects in the composition
- Composite: Defines behavior for components that have children, stores child components, and implements child-related operations in the Component interface
- Client: Manipulates objects in the composition through the Component
interface
What are the pros and cons of the Composite pattern?
Pros: defines class hierarchies consisting of primitive objects and composite objects, makes the client simple, makes it easier to add new kinds of components
Cons: can make your design overly general
What is the Adapter pattern?
A class/object structural pattern which converts the interface of a class into another interface clients expect and lets classes work together that couldn’t otherwise because of inoperable interfaces.