April 05, 2023
what is Creational Patterns
I researched the topic of Creational Patterns by examining various sources, including websites focused on design patterns [1][2][3][4][5], Reddit discussions [6][7][8][9][10], and a Medium article [2]. There was a general consensus among these sources regarding the definition, purpose, and types of Creational Patterns. The sources were directly related to the original query, and I am confident in the information gathered.
Have an opinion? Send us proposed edits/additions and we may incorporate them into this article with credit.
Words
0
Time
2m 40s
Contributors
66
Words read
10.4k
Definition and Purpose of Creational Patterns
Types of Creational Patterns
Understanding and Learning Creational Patterns
Common Opinions and Critiques
Jump to top
Research
Source: "Thoughts about design patterns?" (from reddit, r/learnprogramming)
-
Creational Patterns are important to learn
- They are used to structure programs and tackle design problems
- Common creational patterns include: iterator, command, publish-subscribe, composite, object pool, singleton and factory patterns
- Singleton is often seen as an anti pattern
- Repository and Interception are more popular patterns
- It is important to understand when to use a design pattern, and when it is over-engineering
- Observer is a useful pattern to know, as well as Adapter
- The Factory pattern is often used with static factory methods
- Builder is often confused with chaining
- Design patterns require an understanding of the problem they are trying to solve
- It is important to think critically about what design patterns you are using
Source: "Factory Design Pattern or any alternatives?" (from reddit, r/csharp)
-
A Factory Design Pattern
- It is used to hide the complexity of creating objects by abstracting the process of object creation.
- The Factory pattern may not work if different BusinessManagers require different methods.
- It can be used to create objects when the caller doesn’t care which concrete type they get.
- The Factory pattern can be replaced with Dependency Injection in some cases.
-
Dependency Injection
- It is a design pattern used to create objects without the client knowing the concrete type of the object.
- It is used when a type needs an object but does not need to know which one it gets.
- It can be used to replace the Factory pattern when the object creation process is complex.
Source: "Why is the Prototype design pattern useful?" (from reddit, r/cpp_questions)
-
Creational Patterns are design patterns which are used to create objects in a more efficient, flexible, and maintainable way.
- They can remove superclass dependency and promote encapsulation.
- They are easy to use.
- They can save time when constructing something complex.
- They can help make the flow and design of code more intuitive and readable.
-
Examples of Creational Patterns include the Prototype design pattern, Factory pattern, and Builder Pattern.
- The Prototype design pattern is a way of making a copy of an object quicker.
- The Factory pattern is a way of creating objects without having to specify the exact class of object that will be created.
- The Builder pattern involves creating complex objects step by step.
Source: "What's the best way to learn Design Patterns?" (from reddit, r/softwaredevelopment)
-
Creational Patterns
- Patterns of Enterprise Application Architecture (by Martin Fowler) are a good catalog of these patterns
- SOLID principles help to understand and derive patterns
- Practice is needed to understand how to use them
- Videos and books help to understand them
-
Understanding principles
- Books like Clean Code and principles like SOLID help to instinctively move towards patterns
- Books like Budgen’s Software Design, Evans’s Domain Driven Design, and books like Spinellis’s Code Quality and Thomas’s and Hunt’s The Pragmatic Programmer get you to good foundations of design and code
-
Practice
- Everytime you need to program something stop for a bit and look a pattern that could help you
- Organize them like GoF did: structural, behavioral, creational
Source: "Salesforce Development Tutorial - Design Patter..." (from reddit, r/salesforce)
-
Creational Patterns
- Creational Patterns are design patterns that are used to create objects.
- Creational patterns are concerned with the way of creating objects.
- They provide an interface for creating objects in a way that is suitable for particular situations.
- Examples of creational patterns include the Singleton Pattern, the Builder Pattern, the Factory Pattern, the Abstract Factory Pattern, and the Prototype Pattern.
-
OOP (Object-Oriented Programming)
- OOP is a programming paradigm that helps to create objects that can be reused and manipulated.
- OOP is based on the concept of classes and objects, which are used to store and manipulate data.
- OOP is used to create software applications that are easier to maintain and extend.
💭 Looking into
What are the best practices for using Creational Patterns?
💭 Looking into
What are the design principles that are followed when using Creational Patterns?
💭 Looking into
What are some of the use cases of the Builder Pattern?
💭 Looking into
What are the advantages of using the Prototype Pattern?
💭 Looking into
When should the Singleton Pattern be used?
💭 Looking into
How does the Abstract Factory Pattern differ from the Factory Pattern?
💭 Looking into
What are the benefits of using the Factory Pattern?
Source: "Creational patterns - SourceMaking" (from web, sourcemaking.com)
-
Creational patterns
- In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
- Types of creational patterns include: Abstract Factory, Builder, Factory Method, Object Pool, Prototype, and Singleton.
- Abstract Factory, Builder, and Prototype define a factory object that’s responsible for knowing and creating the class of product objects, and make it a parameter of the system.
- Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
- Factory Methods are usually called within Template methods.
- Factory Method: creation through inheritance. Prototype: creation through delegation.
- Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.
Source: "Creational Patterns" (from web, www.gofpatterns.com)
-
Creational patterns involve the construction of new objects.
- They rarely use constructors directly.
- Rather, it provides alternate methods to return instances of the desired class.
- It allows the encapsulation of the details about which concrete classes the system use and how instances of these concrete classes are created and combined.
-
Factory Method
- When a client object does not know which class to instantiate, it can make use of the factory method to create an instance of an appropriate class from a class hierarchy or a family of related classes.
- The class that contains the factory method or any of its subclasses decides on which class to select and how to instantiate it.
-
Singleton
- Provides a controlled object creation mechanism to ensure that only one instance of a given class exists.
-
Abstract Factory
- Allows the creation of an instance of a class from a suite of related classes without having a client object to specify the actual concrete class to be instantiated.
-
Prototype
- Provides a simpler way of creating an object by cloning it from an existing (prototype) object.
-
Builder
- Allows the creation of a complex object by providing the information on only its type and content, keeping the details of the object creation transparent to the client.
Source: "Introduction to Creational Design Patterns | Ba..." (from web, www.baeldung.com)
-
Creational Design Patterns
- These patterns are concerned with the way in which objects are created and reduce complexities and instability by creating objects in a controlled manner.
-
Types of Creational Design Patterns:
- Singleton – Ensures that at most only one instance of an object exists throughout application
- Factory Method – Creates objects of several related classes without specifying the exact object to be created
- Abstract Factory – Creates families of related dependent objects
- Builder – Constructs complex objects using step-by-step approach
-
Singleton Design Pattern
- This design pattern aims to keep a check on initialization of objects of a particular class by ensuring that only one instance of the object exists throughout the Java Virtual Machine.
- It also provides one unique global access point to the object so that each subsequent call to the access point returns only that particular object.
-
When to Use Singleton Design Pattern:
- For resources that are expensive to create (like database connection objects)
- It’s good practice to keep all loggers as Singletons which increases performance
- Classes which provide access to configuration settings for the application
- Classes that contain resources that are accessed in shared mode
-
Factory Method Design Pattern
- This design pattern defines an interface for creating an object, but let subclasses decide which class to instantiate.
- It delegates the responsibility of initializing a class from the client to a particular factory class by creating a type of virtual constructor.
-
When to Use Factory Method Design Pattern:
- When the implementation of an interface or an abstract class is expected to change frequently
- When the current implementation cannot comfortably accommodate new change
- When the initialization process is relatively simple, and the constructor only requires a handful of parameters
-
Abstract Factory Design Pattern
- This design pattern is used to create families of related or dependent objects.
- It is also sometimes called a factory of factories.
-
Builder Design Pattern
- This creational pattern is designed to deal with the construction of comparatively complex objects.
- It separates out the instantiation process by using another object (a builder) to construct the object.
- This builder can then be used to create many other similar representations using a simple step-by-step approach
Source: "Creational design patterns simplified using UML..." (from web, medium.com)
-
Creational pattern
- A design pattern is a reusable solution to a recurring problem in software design. It’s a well-tested template that helps to solve a particular problem or family of problems related to object-oriented design.
- Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented, which increase flexibility and reuse of existing code.
-
Factory Pattern
- Factory pattern provides an interface for creating objects in a superclass by passing the type and allows sub classes to extend for any new object.
- Factory Method can be used when you don’t know beforehand the exact types and dependencies of the objects your code should work with, when you have created any libraries or framework and users wants to extend that, or when you want to reuse existing objects instead of rebuilding them each time and centralize our object creation process.
-
Abstract Factory Pattern
- This pattern provides families of related objects without specifying their concrete classes.
- Abstract Factory can be used when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products and you simply want to allow for future extensibility.
-
Builder Pattern
- Builder pattern helps in constructing the complex objects step by step and produce different types and representations of an object using the same construction code.
- Use the Builder pattern when your code needs to work with multiple constructor and each of the constructor has many optional parameters, when you are constructing a large object, and when you need to create an immutable class or you want instances to do specific tasks as per the requirement.
-
Prototype Pattern
- You can copy (Shallow & Deep) existing objects without making your code dependent on their classes.
- Use the Prototype pattern when your code shouldn’t depend on the concrete classes of objects that you need to copy, when you have a requirement of creating many objects which are of same type, and when you want to create a single object for the entire application life cycle.
-
Singleton Pattern
- Ensures that a class has only one instance and can be accessed globally in the application.
- Use the Singleton pattern if you need stricter control over global variables and when you have a requirement to create a single object for the entire application life cycle.
Source: "Creational Design Patterns - Javatpoint" (from web, www.javatpoint.com)
-
Creational Design Patterns
- Creational design patterns are concerned with the way of creating objects.
- These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class).
- There are 6 types of creational design patterns: Factory Method Pattern, Abstract Factory Pattern, Singleton Pattern, Prototype Pattern, Builder Pattern, and Object Pool Pattern.
💭 Looking into
What is the best example of a Creational Pattern?