{"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-07-05T03:26:45","modified_gmt":"2026-07-05T01:26:45","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":"<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 &#8220;new&#8221;.<\/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>\r\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>\r\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>\r\n<span style=\"color: #333333\">}<\/span>\r\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>\r\n<span style=\"color: #008800;font-weight: bold\">private<\/span> RestTemplate restTemplate<span style=\"color: #333333\">;<\/span>\r\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&#8217;t 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>\r\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>\r\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>\r\n    <span style=\"color: #008800;font-weight: bold\">private<\/span> <span style=\"color: #0066bb;font-weight: bold\">Student<\/span><span style=\"color: #333333\">(){<\/span>\r\n        <span style=\"color: #888888\">\/\/private to prevent instantiation from outside<\/span>\r\n    <span style=\"color: #333333\">}<\/span>\r\n    \r\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>\r\n        <span style=\"color: #008800;font-weight: bold\">return<\/span> instance<span style=\"color: #333333\">;<\/span>\r\n    <span style=\"color: #333333\">}<\/span>\r\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>\r\n        <span style=\"color: #008800;font-weight: bold\">return<\/span> count<span style=\"color: #333333\">;<\/span>\r\n    <span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\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>\r\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>\r\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>\r\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>\r\n    <span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/ Derived class Rectangle using the Prototype<\/span>\r\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>\r\n    <span style=\"color: #555555;font-weight: bold\">@Override<\/span>\r\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>\r\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>\r\n    <span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/ Derived class Square using the Prototype<\/span>\r\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>\r\n    <span style=\"color: #555555;font-weight: bold\">@Override<\/span>\r\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>\r\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>\r\n    <span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\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","protected":false},"excerpt":{"rendered":"<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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry","category-programming"],"_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":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions"}],"predecessor-version":[{"id":2173,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions\/2173"}],"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}]}}