- Creational
- Structural
- Behavioral
- Security
- Concurrency
- Sql
- User Interface
- Relational
- Social
- Distributed
- Abstract Factory
- Builder
- Factory Method
- Prototype
- Singleton
- Lazy Instantiation
- Utility Pattern
Definition: How to instantiate objects at runtime
Consider using when:
- Unsure of which concrete implementation of an interface I want to return
- Creation should be separated from representation of an object
- Lots of if/else blocks when deciding which concrete class to create
- Switch statements when deciding which concrete class to create
Intent:
- Separate object creation (instantiation) from the decision of which object to create
- Add new classes and functionality without breaking the O in SOLID
- Factory-produced objects
- Factories themselves
- Store which object to create outside of the program
- In a database
- In a configuration file
- Encapsulate object creation
- Allows for late-bound decisions regarding instantiation
- configuration-based
- other persistent storage
- input or other dynamic data
- Caller does know what concrete factory it needs
- "Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."
- Adds an interface to the factory itself from Simple Factory
- Defers object creation to multiple factories that share an interface
- Derived classes implement or override the factory method of the base
Advantages:
- Eliminate references to concrete classes
- Factories
- Objects created by Factories
- Factories can be inherited to provide even more specialized object creation
- Rules for object initialization is centralized
Disadvantages:
- May need to create a factory just to get a concrete class delivered
- The inheritance hierarchy gets deeper with coupling between concrete factories and created classes
- "Provide an interface for creating families of related or dependent objects without specifying their concrete classes"
- Factories create different types of concrete objects (products)
- A Factory now represents a "family" of objects that it can create
- Factories may have more than one factory method
Intent:
- Rid program logic of null checks (guard clauses) where possible
- Provide a non-functional object in place of a null reference
- Allow methods to be called on Null objects, unlike a null reference
Objections/Criticism:
- Can make bugs/errors appear as normal program execution
- In most cases no compiler error would occur when the Null Object is a reference type; could lead to run time errors instead of compile time errors
- Some overhead involved in checking for Null Object versus checking for null(nil)
- Must be used anywhere in the codebase where null is assigned currently -- otherwise there isn't a strong guarantee that the Null Object(s) will always be returned
(placeholder)
(placeholder)
(placeholder)
(placeholder)
(placeholder)
(placeholder)
(placeholder)
(placeholder)
(placeholder)