avatarPeterson C

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2966

Abstract

onnection<span class="hljs-built_in"> instance </span>=<span class="hljs-built_in"> new </span>DBConnection();</pre></div><div id="1f7d"><pre> <span class="hljs-comment">//prevents the class from getting instantiated</span> <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">DBConnection</span>()</span>{}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> DBConnection <span class="hljs-title">getInstance</span>()</span>{
    <span class="hljs-keyword">return</span> instance;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">executeAction</span>()</span>{
    System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"CRUD action execute successfully"</span>);
}

}</pre></div><h2 id="c50c">Lazy initialization</h2><p id="7061">Lazy initialization is the opposite of eager initialization. It means we’ll create our instance and initialize it the first time an instance is requested if one hasn’t been created, otherwise we’ll just return the existing one instead of the eager approach.</p><div id="ddec"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LazyDBConnection</span> {

<span class="hljs-keyword">private</span> <span class="hljs-type">static</span> <span class="hljs-keyword">volatile</span> LazyDBConnection instance;</pre></div><div id="a722"><pre>    <span class="hljs-comment">//prevents the class from getting instantiated</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">LazyDBConnection</span>()</span>{}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LazyDBConnection <span class="hljs-title">getInstance</span>()</span>{</pre></div><div id="a91a"><pre>       <span class="hljs-built_in"> if </span>(instance == null) {
       <span class="hljs-built_in"> instance </span>=<span class="hljs-built_in"> new </span>LazyDBConnection();
    }
    
   <span class="hljs-built_in"> return </span>instance;
}

<span class="hljs-keyword"> public</span> void executeAction(){ System.out.println(<span class="hljs-string">"CRUD action execute successfully"</span>); } }</pre></div><p id="66d2">When developing, it’s good to keep in mind if your application needs to be single or multi-threaded. The code above would work fine for a single-threaded application, but would not work for a multi-threaded application. The reason is that if two threads enter the if code block at the same time and the instance is null, two instances will get created. That is a problem and we need to fix that. We can accomplish that by using synchronizing the code in the if block. Our code would n

Options

ow look the same with one extra check to prevent threads from creating multiple instances.</p><div id="c8f4"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">LazyDBConnection</span> {

<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">volatile</span> LazyDBConnection instance;

<span class="hljs-comment">//prevents the class from getting instantiated</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">LazyDBConnection</span>()</span>{}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LazyDBConnection <span class="hljs-title">getInstance</span>()</span>{

    <span class="hljs-keyword">if</span> (instance == <span class="hljs-literal">null</span>) {
        synchronized (LazyDBConnection.<span class="hljs-keyword">class</span>) {
            instance = <span class="hljs-keyword">new</span> LazyDBConnection();
        }
    }

    <span class="hljs-keyword">return</span> instance;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">executeAction</span>()</span>{
    
    System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"CRUD action execute successfully"</span>);
}

}</pre></div><p id="6901">Both can be used the same way. To get an instance, you just call the static <b><i>getInstance</i></b> method in the class. Then you can use that instance to do whatever the class allows. That would then make our code thread-safe. The code would now look like this.</p><div id="0329"><pre>public static void main(String[] args) {

DBConnection eagerInstance <span class="hljs-operator">=</span> DBConnection.getInstance()<span class="hljs-comment">;</span>
eagerInstance.executeAction()<span class="hljs-comment">;</span>

LazyDBConnection lazyInstance <span class="hljs-operator">=</span> LazyDBConnection.getInstance()<span class="hljs-comment">;</span>
lazyInstance.executeAction()<span class="hljs-comment">;</span>

}</pre></div><p id="0e6f">There are many other scenarios where the Singleton pattern won’t be the ideal choice, but there are times when it’s beneficial to use it. It is up to you as an engineer to use your best judgment and use it when it makes sense. Thank you for making it to the end. <b>Happy</b> <b>coding</b>.</p><blockquote id="23ba"><p><b>Previous</b>: <a href="https://readmedium.com/design-pattern-explained-in-five-minutes-4eae439005d6">Design Pattern Explained In Five Minutes</a></p></blockquote><blockquote id="4fc6"><p><b>Next</b>: <a href="https://readmedium.com/creational-design-pattern-factory-method-d21f3d454996">Creational Design Pattern: Factory Method</a></p></blockquote></article></body>

A guide to software design patterns

Creational Design Pattern: Singleton

Photo by Hal Gatewood on Unsplash

The Singleton design pattern is one of the simplest and easiest patterns to implement. It is a pattern that restricts the instantiation of a class to one single instance.

That simply means a class is responsible for creating a single instance of itself and no matter how many times you get an instance of that class, you will always get the only and same created instance. It is one of the five creational patterns, and the creational pattern is one of the three design pattern categories.

There are many times where using this pattern is beneficial. Consider this scenario. You have been tasked to write a class to access a database. Your class will be used by a module to perform basic CRUD (create, read, update and delete) operations. These operations will be performed by a single user per session.

You could implement your class to create a new connection every time a CRUD action is performed. Even though you’re still using the same session, you would end up opening and closing many connections. A better approach would be to implement the singleton pattern to prevent your application from opening multiple connections instead of just once per session. Another good scenario would be a logging class. You don’t need to create a new instance every time you need to log something in your application, creating one instance would be ideal and improve performance. I’m sure different engineers might have a different use case for when the pattern should be used and that’s perfectly fine. There’s more than one way to skin a cat. However, we’re interested in understanding the pattern benefits and how it’s implemented.

We’re going to implement this pattern using both eager and lazy initialization starting with eager first. Using eager initialization simply means we’ll create our first and only instance of our class, initialize it and return it when an instance is requested. I’m going to be using java, but implement this in other languages should be similar.

Eager initialization

public class DBConnection {
    //create the only private instance of the class
    private static DBConnection instance = new DBConnection();
    //prevents the class from getting instantiated
    private DBConnection(){}

    public static DBConnection getInstance(){
        return instance;
    }

    public void executeAction(){
        System.out.println("CRUD action execute successfully");
    }
}

Lazy initialization

Lazy initialization is the opposite of eager initialization. It means we’ll create our instance and initialize it the first time an instance is requested if one hasn’t been created, otherwise we’ll just return the existing one instead of the eager approach.

public class LazyDBConnection {

    private static volatile LazyDBConnection instance;
    //prevents the class from getting instantiated
    private LazyDBConnection(){}

    public static LazyDBConnection getInstance(){
        if (instance == null) {
            instance = new LazyDBConnection();
        }
        
        return instance;
    }

    public void executeAction(){
        System.out.println("CRUD action execute successfully");
    }
}

When developing, it’s good to keep in mind if your application needs to be single or multi-threaded. The code above would work fine for a single-threaded application, but would not work for a multi-threaded application. The reason is that if two threads enter the if code block at the same time and the instance is null, two instances will get created. That is a problem and we need to fix that. We can accomplish that by using synchronizing the code in the if block. Our code would now look the same with one extra check to prevent threads from creating multiple instances.

public class LazyDBConnection {

    private static volatile LazyDBConnection instance;

    //prevents the class from getting instantiated
    private LazyDBConnection(){}

    public static LazyDBConnection getInstance(){

        if (instance == null) {
            synchronized (LazyDBConnection.class) {
                instance = new LazyDBConnection();
            }
        }

        return instance;
    }

    public void executeAction(){
        
        System.out.println("CRUD action execute successfully");
    }
}

Both can be used the same way. To get an instance, you just call the static getInstance method in the class. Then you can use that instance to do whatever the class allows. That would then make our code thread-safe. The code would now look like this.

public static void main(String[] args) {

    DBConnection eagerInstance = DBConnection.getInstance();
    eagerInstance.executeAction();
    
    LazyDBConnection lazyInstance = LazyDBConnection.getInstance();
    lazyInstance.executeAction();
}

There are many other scenarios where the Singleton pattern won’t be the ideal choice, but there are times when it’s beneficial to use it. It is up to you as an engineer to use your best judgment and use it when it makes sense. Thank you for making it to the end. Happy coding.

Previous: Design Pattern Explained In Five Minutes

Next: Creational Design Pattern: Factory Method

Programming
Software Development
Software Engineering
Education
Java
Recommended from ReadMedium