{"id":2005,"date":"2022-11-12T12:00:00","date_gmt":"2022-11-12T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/the-software-design-patterns-creational-patterns-part-1\/"},"modified":"2026-08-28T13:39:16","modified_gmt":"2026-08-28T11:39:16","slug":"the-software-design-patterns-creational-patterns-part-1","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/the-software-design-patterns-creational-patterns-part-1\/","title":{"rendered":"The Software Design Patterns \u2013 Creational Patterns (Part 1)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Learn Creational Patterns in Software Design Patterns, including Factory, Singleton, Builder, and Prototype patterns with practical code examples.<\/em><\/p>\n\n\n<h3>TL;DR<\/h3>\n<ul>\n<li>\n<p>Creational Patterns focus on how objects are created in software applications.<\/p>\n<\/li>\n<li>\n<p>The <strong>Factory Pattern<\/strong> provides an approach for creating objects while deferring instantiation.<\/p>\n<\/li>\n<li>\n<p>The <strong>Singleton Pattern<\/strong> restricts a class to a single instance.<\/p>\n<\/li>\n<li>\n<p>The <strong>Builder Pattern<\/strong> separates the construction of complex objects from their representation.<\/p>\n<\/li>\n<li>\n<p>The <strong>Prototype Pattern<\/strong> creates new objects by cloning an existing prototype.<\/p>\n<\/li>\n<\/ul>\n<p>This is Part 1 of the Series: Software Design Patterns. In this tutorial series, I would explain to you the important software design patterns you need to know. There are actually dozens of Software Design Patterns out there. However, many of them are just features natively supported by certain programming languages.<\/p>\n<p>We would examine three categories of Software Design Patterns namely Creational Patterns, Structural Patterns and Behavioural Patterns.<\/p>\n<p>In this part, we would focus on Creational Patterns and we would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Factory Pattern<\/a><\/li>\n<li><a href=\"#t2\">Singleton Pattern<\/a><\/li>\n<li><a href=\"#t3\">Builder Pattern<\/a><\/li>\n<li><a href=\"#t4\">Prototype Pattern<\/a><\/li>\n<\/ol>\n<p>We would also try to give example with code snippet on how each is applied.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Factory Method Pattern<\/strong><\/h4>\n<p>To understand the Factory Pattern, we first need to understand the concept of Factory in programming. A factory is simply an object for creating other objects. It could be a function that returns an object. Such function if called a factory function and it normally invokes the \u201cnew\u201d.<\/p>\n<p>For example, in a Spring application,\u00a0 we can have a factory function is given below that returns a RestTemplate object. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Bean<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> RestTemplate <span style=\"color: #0066bb; font-weight: bold;\">getRestTemplate<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">RestTemplate<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>From the example above, if you need to want a new RestTemplate, you simply declare a private method of RestTemplate and annotate it with <strong><em>@Autowired<\/em> <\/strong>annotation. Like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> RestTemplate restTemplate<span style=\"color: #333333;\">;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Benefits of Factory Pattern<\/strong><\/p>\n<ul>\n<li>subclasses of objects and redefine which classes to instantiate<\/li>\n<li>instantiation is deferred to subclases<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Singleton Pattern<\/strong><\/h4>\n<p>This pattern restricts the instantiation of a class to just a single instance. It ensures that<\/p>\n<ul>\n<li>only one instance of an object exists<\/li>\n<li>provides access to that instance<\/li>\n<li>controls creation of new objects<\/li>\n<\/ul>\n<p>Singleton pattern has the benefit of keeping the global namespace clean and also permits lazy initialization. This is preferred to global variables. To implement singleton class, you will have to:<\/p>\n<ul>\n<li>declare all the constructors of the class as private. In this way, the class can\u2019t be instantiated<\/li>\n<li>provide a static method that returns a reference to that instance<\/li>\n<\/ul>\n<p>Example of a singleton class is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Student<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> Student instance <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Student<span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/ eagerly loads the singleton<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> count<span style=\"color: #333333;\">;<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Student<\/span><span style=\"color: #333333;\">(){<\/span>\n        <span style=\"color: #888888;\">\/\/private to prevent instantiation from outside<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n    \n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> Student <span style=\"color: #0066bb; font-weight: bold;\">getInstance<\/span><span style=\"color: #333333;\">(){<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> instance<span style=\"color: #333333;\">;<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getCount<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> count<span style=\"color: #333333;\">;<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Builder Pattern<\/strong><\/h4>\n<p>This is a pattern that breaks down the construction of complex object into several steps. The objective is to separate complex object creation from its representation. The Builder patter follows the approach:<\/p>\n<ul>\n<li>encapsulate the creation and assembling of various parts of a complex object into a separate Builder object<\/li>\n<li>object creation is delegated to a Builder object instead of creating the object directly<\/li>\n<\/ul>\n<p>Since demonstrating this pattern is a bit more involved, you can find details in The Builder Pattern<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Prototype Pattern<\/strong><\/h4>\n<p>In the Prototype Pattern, new objects are created based on an existing prototype.<\/p>\n<p>To implement this pattern,\u00a0 you need to first create a base abstract class. This class would specify a pure virtual <em>clone()<\/em> function. So classes can then derive from this abstract class implementing the <em>clone()<\/em> method.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Prototype Base class<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">abstract<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span> <span style=\"color: #008800; font-weight: bold;\">implements<\/span> Cloneable <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> Shape <span style=\"color: #0066bb; font-weight: bold;\">clone<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> CloneNotSupportedException<span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>Shape<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">super<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">clone<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #888888;\">\/\/ Derived class Rectangle using the Prototype<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Rectangle<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Shape <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> Shape <span style=\"color: #0066bb; font-weight: bold;\">clone<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> CloneNotSupportedException <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>Rectangle<span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">super<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">clone<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #888888;\">\/\/ Derived class Square using the Prototype<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Square<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Shape <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> Shape <span style=\"color: #0066bb; font-weight: bold;\">clone<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> CloneNotSupportedException <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>Square<span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">super<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">clone<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"learnpress\">\n<div class=\"learn-press-message error\">This shortcode LP Profile only use on the page <a href=\"https:\/\/www.kindsonthegenius.com\/wp-admin\/admin.php?page=learn-press-settings\"><strong>Profile<\/strong><\/a><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<h3>FAQs<\/h3>\n<p><strong>What are Creational Patterns?<\/strong><\/p>\n<p>Creational Patterns are software design patterns that focus on object creation and provide different approaches for creating and managing objects.<\/p>\n<p><strong>What is the Factory Pattern?<\/strong><\/p>\n<p>The Factory Pattern uses a factory object or function to create other objects, allowing the details of object instantiation to be separated from the code that uses the object.<\/p>\n<p><strong>What is the Singleton Pattern?<\/strong><\/p>\n<p>The Singleton Pattern restricts the instantiation of a class to a single instance and provides a way to access that instance.<\/p>\n<p><strong>What is the Builder Pattern?<\/strong><\/p>\n<p>The Builder Pattern separates the construction of a complex object from its representation by delegating object creation to a Builder object.<\/p>\n<p><strong>What is the Prototype Pattern?<\/strong><\/p>\n<p>The Prototype Pattern creates new objects based on an existing object or prototype, typically by implementing a cloning operation.<\/p>\n<h3>Final Thoughts<\/h3>\n<p>Creational Patterns provide different approaches to managing object creation in software applications. Factory, Singleton, Builder, and Prototype each address object creation in a different way.<\/p>\n<p>Understanding these patterns gives you a foundation for recognizing and applying common approaches to object creation. They are the first category in the broader Software Design Patterns series, followed by Structural and Behavioural Patterns.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Learn Creational Patterns in Software Design Patterns, including Factory, Singleton, Builder, and Prototype patterns with practical code examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry","category-programming"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2005"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions"}],"predecessor-version":[{"id":2558,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions\/2558"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}