Creational Design Pattern Design Patterns and Principles Chapter 2 Part 1 Flashcards
- focus on how objects are created and managed, ensuring flexibility, efficiency, and maintainability in your code.
- They provide a structured approach to object instantiation, making it easier to control the creation process and decouple the creation logic from the rest of your application
Creational design patterns
- are used for centralized management of internal or external resources and they provide a global point of access to themselves
- is one of the simplest design patterns: it involves only one class which is responsible to make sure there is no more than one instance; it does it by instantiating itself and in the same time it provides a global point of access to that instance
- Ensure that only one instance of a class is created and Provide a global access point to the object.
SINGLETON
When to use SINGLETON
○ Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code.
○ A special care should be taken in multi-threading environments when multiple threads must access the same resources through the same singleton object.
SINGLETON
Common Uses
○
Logger Classes
○
Configuration Classes
○
Accessing resources in shared mode
○
Database connections
- is probably the most used design pattern in modern programming languages like Java and C#. It comes in different variants and implementations.
- Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.
FACTORY METHOD
FACTORY METHOD
When to use
○
A framework delegate the creation of objects derived from a common superclass to the factory
○
the base factory class does not know what concrete classes will be required to create -delegates to its subclasses the creation of concrete objects
○
factory subclasses subclassesare aware of the concrete classes that must be instantiated
FACTORY METHOD
Common Uses
○ Along with singleton pattern, the factory method is the most used pattern.
○ Almost any application has some factories. Here are a some examples:
○ factories providing an xml:
■ parser:javax.xml.parsers.DocumentBuilderFactory
■
javax.xml.parsers.SAXParserFactory
- is used. Using this pattern a framework is defined, which produces objects that follow a general pattern and at runtime this factory is paired with any concrete factory to produce objects that follow the pattern of a certain country.
-is a super-factory which creates other factories (Factory of factories). - A pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes..
ABSTRACT FACTORY
ABSTRACT FACTORY
When to use
○
A system should be configured with one of multiple families of products
○
A system should be independent of how its products are created, composed and represented
○
Products from the same family should be used all together, products from different families should not be used together and this constraint must be ensured.
○
Only the product interfaces are revealed, the implementations remains hidden to the clients.
- are made of parts produced by other objects that need special care when being built
- Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process.
BUILDER
BUILDER when to use
○
Useful when an object needs to be created with many possible configurations
- allows an object to create customized objects without knowing their class or any details of how to create them.
PROTOTYPE
PROTOTYPE when to use
○
Useful when object creation is costly
○
Cloning documents with different content but similar structure