L8: Building on Established Solutions to Create Objects Flashcards
Design patterns
23 design patterns
Not always applicable!
Structured along three families:
- Creational
- Structural
- Behavioral
What are the patterns covered in Software Design and what category do they belong to?
Creational:
- Abstract Factory
- Factory
- Singleton
Structural:
- Decorator
- Façade
Behavioral:
- Command
- Iterator
- Observer
- State
- Strategy
- Template Method
Implementing Creation
Patterns for creating objects
* Making object creation more flexible (or fixed) to match a use case
* Controlling who creates what, how and when
Often use design principles
* Encapsulate knowledge about what concrete classes the system uses
* Hide how these instances are created (data abstraction)
Implementing structure
Patterns for structuring units and their relationships
* Making independent units work together (integration)
* Defining how to compose objects with different functionalities at runtime
Often use design principles
* Separating distinct concerns into units
* Abstracting how these concerns interact
* Interfacing their relations
Implementing behavior
Patterns for implementing communication between units
* Making separate units responsible for a specific functionality
* Characterizing complex control flows at runtime
Often use design principles
* Separating concerns into distinct, communicating units
* Encapsulation of behavior
* Abstract (procedural) behavior through inheritance or composition
Singleton
It’s a simple pattern
Restrict the instantiation of a class to a single instance
(i.e., there can only be one object of that type)
* Ensure that only a single instance exists
* Define access to that instance
* Control the instantiation of the instance
Used for
* Storing and managing variables (instead of having them globally accessible)
* Implementing other design patterns
* Logging, configuring, file system, window manager etc.
Being careful with the singleton
A singleton is sometimes considered a bad practice
* Introduces (often unnecessary) global access
* Not really aligning to the single-responsibility principle
(responsible for ensuring own uniqueness and performing other functions)
This can lead to
* Additional (global) dependencies and increased coupling
* Reduced extensibility (what if multiple objects are needed?)
* Testing challenges when tested unit relies on singleton
* Abuse of the single, global access point for everything (instead of proper abstraction)
Factory
It’s more complex than a singleton
Define interface to create object, but defer instantiation to subclasses
* Decouple object creation from concrete class
(new couples to concrete class and violates dependency inversion principle)
* Enable instantiation of objects whose types are unknown when coding
* Allow subclasses to specify/decide what objects to create
Used for
* Creating user-interface or game-engine elements (e.g., Swing, JavaFX)
* Creating loggers with different configurations
* Loading plug-ins dynamically
Being careful with the factory
Naturally increases the complexity of the code by introducing abstractions
Easily overused, can be hard to judge whether a factory is useful
Generics
Using a list instead of an array to store multiple vehicles
Uses an ArrayList
* ArrayList<E>
* Implements interface List<E>
* E represents generic type to be replaced by concrete type
* Used to implement generic data structures/classes</E></E>
Why use generics?
Generics (can) improve readability, reusability, and robustness
Ensure type safety (compiler checks for type of data in data structure)
Avoid runtime errors