Software Design Patterns - Creational Patterns
Software Design Patterns - Creational Patterns
Deals with object creation mechanisms
- Singleton
- Builder
- Prototype
- Factory
- Abstract Factory
Singleton Pattern
A class of which only a single instance can exist
Application needs one, and only one, instance of an object.
Applicable when
- Ownership of the single instance cannot be reasonably assigned
- Lazy initialization is desirable
- Global access is not otherwise provided for
Basic version
1 | public final class singleton{ |
Lazy initialization version(Thread-Safe)
Pros: Initialised on first call to avoid memory wastage.
Cons: locks must be added to ensure thread safety, and adding locks will affect performance.
1 | public class Singleton { |
Initialiation-on-demand Holder Idiom
1 | public class Singleton { |
Builder Pattern
Complex objects can be created directly without the user knowing the construction process and details of the object.
Solves the Telescoping Problem.
Main parts of GoF’s Builder Pattern
- Product object
1 | public class Computer{ |
- A Builder(interface or abstract class)
1 | public abstract class Builder { |
- Concrete Builder(extend the Builder)
1 | //装机人员1 |
- Director object
1 | public class Director{ |
Prototype Pattern
Creating duplicate object while keeping performance in mind. The operation will directly clone a object in the ram. RAM’s I/O is much faster than storage.
- Creator
- Prototype
- ConcretePrototype
Factory Pattern
In the factory pattern, we create objects without exposing the creation logic to the public and by using a common interface to point to the newly created object.
There’re two type of Factory Pattern
- Simple Factory
- Simple Factory create products(object)
1 | public interface Dog{ |
1 | public class DogFactory{ |
- Abstract Factory Pattern
- Abstract Factory create Factory
…write later…
Software Design Patterns - Creational Patterns
https://blog.kwunlam.com/Software-Design-Patterns-Creational-Patterns/