1/62
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Why use design patterns?
Capitalize on experience and knowledge of design
Increase reusability of software
improve flexibility so that evolution cost is reduced
standardized vocabulary with which to communicate
What are the components of the factory method?
Product: defines the interface of objects the factory method creates
ConcreteProduct: implements the Product interface
Creator: declares the factory method, which returns an object of type product
Will have a method that will return the product
May define a default implementation of the factory method that returns a default ConcreteProduct object
ConcreteCreator: overrides the factory method to return an instance of a ConcreteProduct
What is the structure of the factory method?
Where is the factory method applicable?
Used when a class cannot predict the type of objects it needs to create.
Delegates object creation to subclasses, allowing them to decide which specific class to instantiate.
Promotes flexibility by localizing the knowledge of which helper subclass is responsible for creating the object.
Involves a Product interface or abstract class, with multiple Concrete Products.
Includes a Creator class (abstract or base), and Concrete Creators that override the factory method to instantiate specific products.
Makes it easy to add new product types without modifying existing code.
What are pros of the factory method?
Avoid tight coupling between the creator and the concrete products
Single responsibility Principle: can move the product creation code into one place in the program, making the code easier to support
Open/closed principle: can introduce new types of products into the program without breaking existing client code
what are the cons to factory method?
The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern
What are the components of the builder design pattern?
Builder: specifies an abstract interface for creating each part of the Product
ConcreteBuilder: constructs and assembles parts of the product
It must provide a mechanism to retrieve the product
Responsible for building the product in a specific way
Director: utilized the builder interface to create the objects
Product: the complex object that is being constructed
what is the structure of the builder design pattern
Where is the builder pattern applicable?
Refactoring/Avoiding the telescopic constructor anti-pattern
Constructing some complex objects needs to allow different representations for that object
What are pros to the builder design pattern?
Able to construct objects step-by-step, defer construction steps or run steps recursively
Facilitates reuse of construction code when building various representations of products
Single responsibility principle: complex construction code is separated from the business logic of the product
what are the cons to the builder pattern?
Increase in complexity of the system since it requires creating multiple new classes
What is the main difference between the builder and factory design patterns?
Main difference between the builder and factory, in the builder you have one product being customized whereas in the factory you have multiple products being customized.
What are the components of the prototype design pattern?
Prototype: declares an interface for cloning itself
concretePrototype: implements an operation for cloning itself
Client: creates a new object by asking a prototype to clone itself
What is the structure of the prototype design pattern?
where is the prototype pattern applicable?
Your code should not depend on the concrete classes of object that you need to copy
Think copying objects passed from a library
To reduce the number of subclasses that only differ in the way they initialize their respective objects
Clone and initialize instead of subclassing
What are pros to the prototype design pattern?
Can clone objects without coupling to their concrete classes
Remove repeated initialization code in favor of cloning pre-built prototypes
Proceed complex objects more conveniently
An alternative to inheritance when dealing with configuration presets for complex objects
What are the cons to the prototype design pattern?
Cloning complex objects is difficult for objects with circular references or contain objects that do not support copying
What are the components of the singleton design pattern?
Private instance: ensure the class only has one instance
Private Constructor: prevents other classes from creating instances of the singleton
Public getter for the instance: responsible for checking whether an instance has been created
Serves as global access point
What is the structure of the singleton design pattern?
where is the singleton design pattern applicable?
There must be exactly one instance of a class
We have some global data that must be accessible to client in a defined manner
What are the pros to the singleton design pattern?
Can be sure that a class has only a single instance
Gain a global access point to that instance
The singleton object is initialized only when it is requested for the first time
What are the cons to the singleton design pattern?
Violates the single responsibility principle: the pattern solves two problems at the time
Can mask bad design
Requires special treatment in a multithreaded environment so that multiple threads will not create a singleton object several times
It may be difficult to unit test the client code of the singleton
what are the components of the adapter design pattern?
Target: definees the domain-specific interface the client useless
Client: utilizes objects conforming to the Target interface
Adaptee: defines an existing interface that needs adaptin
Adapter: adapts the interface of adaptee to the Target interface
What is the structure of the adapter pattern at the class level?
what is the structure of the adapter pattern at the object level?
where is the adapter pattern applicable?
Need to use some existing class, but its interface is not compatible with the one needed
Creating a reusable class that cooperates with unrelated or unforeseen classes
For the Object Adapter, you need to use several existing subclasses, but it would not be feasible to adapt their interface by subclassing each one
what are the pros to the adapter pattern?
Single responsibility principle: can separate the interface or data conversion code from the primary business logic of the program
open/closed principle: can introduce new types of adapters into the program without breaking the existing client code
What are the cons to the adapter pattern?
The overall complexity of the code increases because you need to introduce a set of new interface classes
what are the components of the composite design pattern?
Component: declares the interface for objects in the composition and implements default behavior for the interface common to all classes
Leaf: represents leaf objects in the composition defining behavior for primitive objects in the composition
Composite: defines behavior for components having children and stores child components
Client: manipulates objects in the composition through the component interface
what is the structure of the composite design pattern?
where is the composite design pattern applicable?
Representing part-whole hierarchies of objects
Clients should be able to ignore the difference between composition of objects and individual objects
what are the pros of the composite pattern?
More efficient usage of complex tree structures by taking advantage of polymorphism and recursion
open/closed principle: new element types can be introduced without breaking the existing code
what are the cons of the composite pattern?
Difficult to provide a common interface for classes whose functionality differs too much
what are the components of the decorator design pattern?
Components: the interface for objects that can have responsibilities added to them dynamically
ConcreteComponent: an object to which additional responsibilities can be attached
Decorator: defines the same interface as the component and maintains a reference to the components object
ConcreteDecorator: adds responsibilities to the component
what is the structure of the decorator pattern?
where is the decorator pattern applicable?
Need to add responsibilities to individual objects dynamically without affecting other objects
Need to be able to remove responsibilities of on object
When extension by subclassing is impractical
what are the pros of the decorator pattern?
You can extend an objects behavior without making a new subclass
Flexibility: you can add or remove responsibilities from an object at runtime
You can combine several behaviors by wrapping an object into multiple decorators
Single responsibility principle: a monolithic class can be divided into several smaller classes
what are the cons of the decorator pattern?
When wrappers are stacked it can be hard to remove a specific one from the stack
It can be difficult to implement the pattern such that its behavior does not depend on the order in the decorators stack
Instantiation is more complicated to include all of the decorators
what are the components of the facade design pattern?
Facade: knows which subsystem classes are responsible for a request
Subsystem classes: implements subsystem functionality
Client: communicate with the subsystem by sending requests to the Facade
what is the structure of the facade design pattern?
where is the facade pattern applicable?
Want to provide a simple interface to a complex subsystem
There are many dependencies between clients and the implementation classes of an abstraction
Layer your subsystems
what are the pros of the facade design pattern?
Manages complexity by separating your code from a subsystem
what are the cons of the facade design pattern?
Prone to becoming a god class antipattern by coupling to all of the classes of the application
Adds more complexity than it helps to control in simple systems
what are the components of the template method?
Abstract class: defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
Implements a template method defining the skeleton an algorithm
Concreteclass: implements the primitive operations to carryout subclass-specific steps of the algorithm
what is the structure of the template method?
where is the template method applicable?
Implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behaviors that can vary
A common behavior among subclasses should be factored and localized in a common class to avoid code duplication
Control subclasses extensions by defining a template method that class hook operations to specify points of extension
what are the pros of the template method?
Lets clients override certain parts of a large algorithm, making them less affected by changes that happen to other parts of the algorithm
Can pull the duplicate code into a superclass
what are the cons of the template method?
clients are limited by the provided skeleton of an algorithm
Maintenance effort typically increases with the number of steps.
what are the components of the iterator design pattern?
Iterator: defines an interface for accessing and traversing elements
concreteIterator: implements the iterator interface, keeping track of the current position in the traversal of the aggregate
Aggregate: defines an interface for creating an iterator object
ConcreteAggregate: implements the iterator creation interface to return an instance of the proper ConcreteIterator
what is the structure of the iterator pattern?
where is the iterator pattern applicable?
Accessing an aggregate objects contents without exposing its internal representation
Supports multiple traversals of aggregate objects
Provide a uniform interface for traversing different aggregate structures
what are the pros to the iterator pattern?
Single responsibility principle: cleans up the client code and the collections by extracting bulky traversal algorithms into separate classes
Open/Closed principle: implement new types of collections and iterators and pass them to existing code without breaking anything
Can iterate over the same collection in parallel
Can delay an iteration and continue it when needed
what are the cons to the iterator pattern?
Applying the pattern can be an overkill if your app only works with simple collections
Using an iterator may be less efficient than going through elements of some specialized collections directly
What are the components of the state pattern?
Context: defines the interface of interest to clients and maintains an instance of a ConcreteState subclass that defines the current state
State: defines an interface for encapsulating the behavior associated with a particular state of the Context
ConcreteState subclasses: each subclass implements a behavior associated with a state of the Context
what is the structure of the state pattern?
where is the state pattern applicable?
An object's behavior depends on its state, and it must change its behavior at run-time depending on that state
Operations have large, multipart conditional statements that depend on the object’s state
what are the pros to the state pattern?
Single responsibility: organizes the code related to particular states into separate classes
open/closed principle: introduce new states without changing existing state classes or the context
Simplifies the code of the context by eliminating state machine conditionals
what are the cons of the state pattern?
Can be overkill if a state machine has only a few states or rarely changes states
what are the components of the strategy pattern?
Strategy: declares an interface common to all supported algorithms
Context relies on this to call the algorithm defined by a ConcreteStrategy
ConcreteStrategy: implements the algorithm using strategy interface
Context: is configured with a ConcreteStrategy object, maintaining a reference to a strategy object
what is the structure of the strategy pattern?
where is the strategy pattern applicable?
Many related classes differ only in their behavior then Strategies provide a way to configure a class with one of many behaviors
Need different variants of an algorithm
An algorithm uses data that clients should not know about
A class defines many behaviors, and these appear as multiple conditional statements in its operations
what are the pros of the strategy design pattern?
Can swap algorithms used inside an object at runtime
Can isolate the implementation details of an algorithm from the code that uses it
Can replace inheritance with composition
Open/closed principle: introduce new strategies without having to change the context
what are the cons of the strategy pattern?
Overkill if you only have a couple of algorithms and they rarely change
Clients must be aware of the differences between strategies to select a proper one
Some languages have functional type support which facilitates different implementations of an algorithm