Midterm Review Flashcards
What is the Singleton Pattern?
An object creational pattern that ensures a class only has one instance, used when only one instance is needed and must be accessible from a well-known point.
What are the pros and cons of the Singleton pattern?
Pros: controlled access to a single instance, very easy to alter to allow for a variable number of instances, more flexible than using static class methods
Cons: not much better than having a global variable
What are the known uses of the Singleton pattern?
Logging
Database access
Other shared resource
What is the motivation for a Singleton pattern?
Some classes should only have one instance, such as one print queue
What is the Factory pattern?
A class creational pattern which defines an interface for creating an object but defers instantiation to subclasses.
What is the motivation for a Factory pattern?
Some situations require the creation of similar objects with different structures depending on the environment.
When should you use a Factory pattern?
- When a class can’t anticipate the class of objects it must create
- When a class wants its subclasses to specify the objects it creates
- When you want to localize the knowledge of which helper subclass is the delegate
What are the participants of the Factory pattern?
- Product: defines the interface that the factory creates
- ConcreteProduct: implements the interface
- Creator: declares the factory method (possibly also defines default ConcreteProduct types)
- ConcreteCreator: overrides the factory to return new types of Product
What are the pros and cons of the Factory pattern?
Pros: decouples client code from having to know about different kinds of products or how to create them, makes subclassing easy and flexible, makes use of parallel class structures trivial
Cons: may need to subclass the creator class in client code if client-specific types are required which can lead to extra code
What are the known uses of the Factory pattern?
C# GetEnumerator: creates an enumerator for the type on which it is called
Javascript createElement: creates a new HTML element based on a passed HTML element type
What is the Abstract Factory pattern?
An object creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.
What is the motivation for an Abstract Factory pattern?
It can define the shared structure of related/connected objects and have the concrete implementation handle the details
When should you use an Abstract Factory pattern?
- When a system should be independent of how its products are created, composed, and represented
- When a system should be configured with one of multiple families of products
- When a family of related product objects is designed to be used together, and you need to enforce this constraint
- When you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
What are the participants of the AbstractFactory?
- AbstractFactory: Declares an interface for operations that create abstract product objects
- ConcreteFactory: Implements the operations to create product objects
- AbstractProduct: Declares an interface for a type of product object
- ConcreteProduct: Defines a product object to be created by the corresponding concrete factory and implements AbstractProduct Interface
- Client: Uses only interfaces declared by AbstractFactory and AbstractProduct