0% found this document useful (0 votes)
2 views

GoDesignPattern

The Singleton Pattern is a design pattern that ensures a class has only one instance and provides a global access point to it, useful for managing shared resources. Key characteristics include single instance, global access, and lazy initialization, with advantages such as controlled access and reduced memory footprint, but it also introduces global state and hidden dependencies. Common use cases include configuration management, logging, and database connections, and while powerful, it should be used carefully to maintain clean and maintainable design.

Uploaded by

habib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

GoDesignPattern

The Singleton Pattern is a design pattern that ensures a class has only one instance and provides a global access point to it, useful for managing shared resources. Key characteristics include single instance, global access, and lazy initialization, with advantages such as controlled access and reduced memory footprint, but it also introduces global state and hidden dependencies. Common use cases include configuration management, logging, and database connections, and while powerful, it should be used carefully to maintain clean and maintainable design.

Uploaded by

habib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Design Pattern In Golang :

Singleton Pattern

Rihab Sakhri
01

What is the Singleton Pattern?


The Singleton Pattern is a design pattern that
restricts a class to a single instance and provides
a global point of access to that instance. This can
be particularly useful when you want to control
access to shared resources, like a configuration
object or a connection pool.

Rihab Sakhri
Key Characteristics :
1. Single Instance : Only one instance of the class can exist.

2. Global Access Point : The instance can be accessed globally,

usually through a static method.

3. Lazy Initialization : The instance is created only when it is

needed, not when the application starts.

Rihab Sakhri
03

When to Use the Singleton

Pattern:
- When you need exactly one instance of a class to coordinate

actions across the system.

- When the overhead of creating and destroying instances is too

costly.

- When you want to control access to shared resources.

Rihab Sakhri
Implementation in Go :

Here's how you can implement the Singleton Pattern in Go:

Rihab Sakhri
05

Implementation in Go (Continued) :

Rihab Sakhri
Explanation of the Code :

1. Singleton Struct : This is the main struct that holds any

necessary fields. In our example, we have a value field

to demonstrate how we can interact with the singleton

instance.

2. Global Variables :

- instance: This variable holds the single instance of the Singleton

struct.

- mu: A mutex is used to ensure that access to the singleton instance

is thread-safe, preventing race conditions when multiple goroutines

try to access it simultaneously.

3. Singleton Struct : This function checks if the instance is nil. If it is

it creates a new instance of Singleton and initializes it.

- The mu.Lock() and defer mu.Unlock() ensure that only one

goroutine can create the instance at a time, maintaining the singleton

property.

Rihab Sakhri
07

Explanation of the Code (continued) :


4. SetValue and GetValue Methods : These methods allow you to

interact with the singleton instance. You can set and get the value of

the singleton.
5. Main Function :
- In the main function: we demonstrate how to get the instance of the

singleton, set a value, and retrieve it.


- We also check if both singleton1 and singleton2 are the same

instance, which they should be, confirming the singleton behavior

Rihab Sakhri
Key Advantages of the Singleton

Pattern:

1. Controlled Access : Since there is only one instance, you can

control how it is accessed and modified.

2. Reduced Memory Footprint : It avoids the overhead of

creating multiple instances of a class, especially for

resources that are expensive to create.

3. Global Access Point : Provides a simple way to access

shared resources without passing instances around.

Rihab Sakhri
09

Key Disadvantages of the

Singleton Pattern:

1. Global State : It essentially introduces a global state into your

application, which can make testing and debugging more challenging.

2. Hidden Dependencies : Other parts of the code may rely

on the singleton, leading to hidden dependencies that can

complicate the code structure.

3. Difficulties in Extending : If you want to extend the

functionality of the singleton, it can become tricky because the

pattern enforces a single instance. This can limit flexibility in your

design.

Rihab Sakhri
Use Cases for the Singleton Pattern :

1. Configuration Management : A single configuration object that

holds application settings.

2. Logging : A logger that writes to a file or console,

ensuring all parts of the application use the same logging

instance.

3. Database Connections : Managing a single connection

pool to a database, which can be expensive to create

repeatedly.

Rihab Sakhri
11

Conclusion :

The Singleton Pattern can be a powerful tool in your design

arsenal, but like any design pattern, it should be used

judiciously. It’s great for ensuring that only one instance of a

resource exists, but it’s essential to be aware of the potential

downsides, such as global state and hidden dependencies.


In Golang, implementing the Singleton Pattern is a great way

to get comfortable with struct and concurrency concepts. Just

remember to keep your design clean and maintainable!

Rihab Sakhri
Thank you!

We'll dive deeper into the design pattern and

explore the factory pattern in an upcoming

post so you don’t miss it!

Rihab Sakhri

You might also like