Author: David Martinez
Created: November 5, 2024 - Updated: November 5, 2024
Read Time: 10 min
Welcome! Today, letโs dive into the Singleton design pattern, a fundamental yet powerful pattern in object-oriented programming. Weโll explore its structure, benefits, and practical use cases in Ruby.
The Singleton is a creational design pattern that ensures a class has only one instance and provides a global point of access to it.
This pattern is ideal for managing resources like database connections or configuration settings where only one instance is necessary.
๐ธ Use Singleton when you need to control access to shared resources or ensure that only one instance of a class is used throughout the application.
๐ธ This pattern is helpful for scenarios requiring centralized management, such as logging, caching, or connection pooling.
Imagine an application where multiple parts of the code access configuration settings. Without a Singleton, you risk multiple, inconsistent instances of these settings, leading to bugs.
The Singleton pattern ensures only one instance of the configuration class exists. Any part of the application can access this instance without worrying about duplicates.
In this example, the Singleton pattern is applied to manage application configuration.
๐น The Singleton class controls access to the single instance.
๐น The self.instance
method provides a global point of access, ensuring only one instance exists.
1๏ธโฃ Define a self.instance
method to return the single instance of the class.
2๏ธโฃ Use @instance ||= new
inside the method to create the instance if it doesnโt already exist.
3๏ธโฃ Privateize new
to prevent external code from creating additional instances.
๐ก Note: The Singleton pattern restricts object creation, ensuring only one instance exists.
๐ Simplifies access to shared resources.
๐ Reduces the risk of inconsistent states across the application.
๐ Provides centralized control and management of instances.
๐ Optimizes memory usage by limiting unnecessary instance creation.
# Singleton class for AppConfigclass AppConfig@instance = nil# Ensures only one instance of AppConfig existsdef self.instance@instance ||= newend# Stores application configurationsattr_accessor :settings# Sample initialization of settingsdef initialize@settings = { app_name: "MyApp", version: "1.0.0" }end# Prevents external instantiationprivate_class_method :newend# Client codedef mainconfig1 = AppConfig.instanceconfig2 = AppConfig.instanceputs config1.settings # => {:app_name=>"MyApp", :version=>"1.0.0"}config2.settings[:version] = "2.0.0"puts config1.settings # => {:app_name=>"MyApp", :version=>"2.0.0"}puts config1.equal?(config2) # => trueendmain# Output# {:app_name=>"MyApp", :version=>"1.0.0"}# {:app_name=>"MyApp", :version=>"2.0.0"}# true
The Singleton pattern is a powerful tool to control resource usage by ensuring a class has only one instance.
๐บ Itโs ideal for scenarios where centralized control is needed, like managing configurations.
๐บ The Singleton pattern simplifies code, minimizes memory footprint, and ensures consistency.
๐บ By implementing Singleton, you create a reliable way to manage shared resources across the application.
๐ป You can find this and other design patterns here ๐