Front | Back |
Intent of Singleton
|
Ensure class has only 1 instance, and provide global access to it (static class in java).
|
Motivation of Singleton
|
Many cases where we need only 1 instance of a class (ex. most factories)
|
Applicability of Singleton
|
Must have only 1 instance of a class & have a well-known access point to it. When only instance should be extended by subclassing - clients should not have to modify code to use extended instance.
|
Paticipants of Singleton
|
The Singleton itself. It defines an instance operation that lets clients
access its unique instance. It may be responsible for creating its own
instance via a private constructor.
|
Consequences of Singleton
|
Controlled access to a sole instance. Reduced namespace. Permits refinement of operations & representation. Permits variable number of instances. More flexible than class operations.
|
Implementation of Singleton
|
Can hide operation that creates the instance behind a class operation to guarantee creation occurs successfully. Since it has a pointer to a singleton object, the actual singleton can be changed at run-time via subtyping. Allows instantiation of object at time client specifies.
|
Intent of Factory Method
|
Define an interface for creating an object but let the subclasses decide which class to instantiate.
|
Motivation of Factory Method
|
Encapsulates the knowledge of which subclass to create. Lets the application using the Factory Method be ignorant of which subclass is needed.
|
Applicability of Factory Method
|
Class cannot anticipate class of objects it must create. Class wants its subclasses to specify the object it creates. Classes delegate responsibility to one of several helper subclasses & want to localize knowledge of which subclass is the delegate
|
Participants of Factory Method
|
Product - defines the interface of objects created by factory method. Concrete Product - Implements the Product interface. Creator - Declares the factory method. Concrete Creator - Overrides the factory method to return an instance of a Concrete Product
|
Collaboration of Factory Method
|
Creator relies on its subclasses (Concrete Creators) to define factory method that will return an instance of the appropriate Concrete Product class
|
Consequences of Factory Method
|
Eliminates need to bind application specific class into code. Potential disadvantage - large overhead to create just 1 object. Provides hooks for subclasses which can make for complicated object creation. Connects parallel class hierarchies.
|
Implementation of Factory Method
|
2 major varieties - Creator can be an abstract class, or it can be a concrete class that provides a default implementation. There can also be parameterized factory methods that take a parameter dictating which type of object to create. All objects created share common interface.
|
Intent of Bridge
|
Decouple an abstraction from its implementation.
|
Motivation for Bridge
|
Abstraction could have several possible implementations. It is difficult to modify, extend, or reuse abstraction & implementation independently. The bridge puts abstraction and its implementation in separate class hierarchies.
|