Creational Patterns  «Prev  Next»
Lesson 4Common creational patterns
ObjectiveDescribe, Compare, and Contrast several Creational Patterns.

Describe, Compare, and Contrast several Creational Patterns

Describe, compare, and contrast the following four creational patterns: 1) Factory Method pattern, 2) Abstract Factory, 3) Builder, 4) Prototype. 5) Singleton
  1. Factory Method Pattern: The Factory Method pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. The Factory Method is used when there is uncertainty about the exact types and dependencies of the objects your code should work with. Instead of calling a constructor to create an object directly, the creation is delegated to a factory method, providing a layer of flexibility and scalability to your code.
  2. Abstract Factory Pattern: The Abstract Factory Pattern, another creational design pattern, extends the concept of the Factory Method pattern. It involves an interface to create families of related or dependent objects without specifying their concrete classes. Think of it as a factory of factories. The idea is to abstract the creation of objects depending on business logic, platform choice, or other considerations. Abstract Factory allows high-level abstraction and loose coupling, thereby promoting code reusability and separation of responsibilities.
  3. Builder Pattern: The Builder pattern is employed when there is a need to construct complex objects step by step. It separates the construction of an object from its representation so that the same construction process can create different types of objects. It encapsulates the construction logic in different builder classes, each defining how to combine different parts of the product, while the director class guides the builder class on the order of the execution. The Builder pattern is excellent when the product involves a lot of steps or components, providing flexibility in the composition and representation of the product.
  4. Prototype Pattern: The Prototype pattern is used when creating a new object instance is more complex or costly than copying an existing instance. The original object acts as a prototype and is cloned when a new instance is needed. This pattern hides the complexities of making new instances from the client. The copying process can be customized to handle the copying of complex object structures or cross-referencing of other objects within the clone.
  5. Singleton Pattern: The Singleton pattern restricts instantiation of a class and ensures that only one instance of the class exists in the system. It provides a global point of access to the instance, ensuring that no other part of the code can instantiate its own copy of the object. Singleton is used when a single shared value of a particular class needs to be used by other objects.

Comparing and Contrasting

While all these patterns deal with object creation, they serve different purposes.
  1. Factory Method and Abstract Factory patterns both deal with object creation but at different levels of complexity: Factory Method is about creating an instance of several derived classes, while Abstract Factory is about creating families of related or dependent objects.
  2. The Builder pattern, on the other hand, isn't about choosing the class of the product but about constructing complex objects step by step. Unlike the other patterns, the Prototype pattern leverages prototypical instances and creates new objects by copying these prototypes.
  3. Finally, the Singleton pattern is unique in its purpose: it's designed to ensure there's only one instance of a class in the system and provides a global point of access to it.
Each pattern offers unique advantages based on the situation at hand, but all serve the same general purpose: to make the code more flexible, maintainable, and organized by abstracting the process of object creation.
There is no particular limit to the number of creational patterns, any more than there is a finite number of possible blueprints for a house, there are five which are particularly well-known.
  1. Factory Method Pattern
  2. Abstract Factory
  3. Builder
  4. Prototype
  5. Singleton

We have already looked at the Singleton pattern. Later in this module, you will have a chance to explore the Factory Method pattern in detail.
The other three common creational patterns are described in this SlideShow:
 Abstract Factory pattern defines an interface for creating instances of several related abstract classes without specifying their concrete subclasses. The java.awt.Toolkit class used to create native peers for graphical user interface components is an example of an abstract factory class.
1) Abstract Factory pattern defines an interface for creating instances of several related abstract classes without specifying their concrete subclasses. The java.awt.Toolkit class used to create native peers for graphical user interface components is an example of an abstract factory class.

Builder Pattern builds different complex objects from the same set of component parts. For example, the Director class might feed a long list of names and addresses into the Builder. The Builder might build an HTML page from that list, an XML page, or a PostScript file, depending on what type of Builder it was.
2) Builder Pattern builds different complex objects from the same set of component parts. For example, the Director class might feed a long list of names and addresses into the Builder. The Builder might build an HTML page from that list, an XML page, or a PostScript file, depending on what type of Builder it was.

The Prototype pattern creates new objects by copying a prototype object. By changing the object that is copied, you change the object that is created. This is useful when you need many different copies of a relatively complex object, all of which have the same initial state.
3) The Prototype pattern creates new objects by copying a prototype object. By changing the object that is copied, you change the object that is created. This is useful when you need many different copies of a relatively complex object, all of which have the same initial state.


Three Design Patterns.
Creational: This deals with the concept of how an object can be created.
This often involves isolating the details of object creation so your code is not dependent on what types of objects there are and thus does not have to be changed when you add a new type of object. The aforementioned Singleton is classified as a creational pattern, and later in this course you will see examples of the Factory Method and Prototype.

Common Creational Patterns - Quiz

Click the link below to take a short quiz on the material just covered.
Common Creational Patterns - Quiz

Microservices Patterns