[GO] [Gang of Four] Design pattern learning --Singleton

Singleton --One card

** Table of Contents ** The last of the creational patterns is a singleton. I think this is a pattern that everyone is familiar with. I feel that the first design pattern I learned was a singleton.

Classes that provide DB connections and classes that get assets are generally implemented in this pattern. Guarantees the uniqueness of an instance of that class in your program.

Purpose

It guarantees that there is only one instance for a class and provides a global way to access it.

Component

ยท A class that creates only one Singleton instance

Implementation

If you code this pattern in kotlin, it will be in a simple form as shown below, so I'm not sure, so I will code it in java.

SingletonObject.kt


package singleton

object SingletonObject {
    var str = ""

    fun print() {
        println("Output result:$str")
    }
}

Create images and music files from multiple screens via singleton objects.

A class that creates only one Singleton instance

Singleton object

JavaSingletonObject.java


package singleton;

public class JavaSingletonObject {
    /**
     *Defined as static and make the instance of own class unique
     */
    public static JavaSingletonObject instance = new JavaSingletonObject();
    private String address = "/assets/temp";
    enum Assets {
        IMAGE1("image_1"),
        IMAGE2("image_2"),
        AUDIO1("audio_1"),
        AUDIO2("audio_2");

        private final String value;

        Assets(final String value) {
            this.value = value;
        }
    }

    /**
     *Define the constructor privately so that no new instance can be created.
     */
    private JavaSingletonObject(){
        System.out.println("Connection:" + address);
    }

    public String getAssets(Assets target) {
        System.out.println("Material acquisition with singleton object:" + target.value);
        return target.value;
    }
}

Screen 1 class

DisplayOne.kt


package singleton

class DisplayOne {
    fun draw() {
        println("In User1${JavaSingletonObject.instance.getAssets(JavaSingletonObject.Assets.IMAGE1)}Draw!!")
    }
}

Screen 2 class

DisplayTwo.kt


package singleton

class DisplayTwo {
    fun start() {
        println("In User2${JavaSingletonObject.instance.getAssets(JavaSingletonObject.Assets.AUDIO1)}Play!!")
    }
}

User

Screen management class

Manager.kt


package singleton

class Manager {
    init {
        DisplayOne().draw()
        DisplayTwo().start()
    }
}

This completes the implementation of the singleton pattern. This process is supposed to be executed only once because the connection to assets is established in the constructor of the `` `JavaSingletonObject``` class.

Also, since the constructor is defined as private, it is not possible to create a new instance of the class, and uniqueness is guaranteed.

Output result

[output]
Connection:/assets/temp
Material acquisition with singleton object: image_1
Image in User1_Draw 1!!
Material acquisition with singleton object: audio_1
Audio with User2_Play 1!!

Recommended Posts

[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design pattern learning
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Design Pattern #Singleton
Gang of Four (GoF) Patterns in Python
Learn the design pattern "Singleton" with Python
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Proxy
I wrote a design pattern in kotlin Singleton edition
Learn the design pattern "Chain of Responsibility" in Python
Singleton pattern in Java
Singleton pattern in Python
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"
Summary of Singleton patterns introductory design patterns learned in Java language