Я хочу добавить конфигурацию в мой модуль. Я видел, что некоторые проекты (например, геокодер ) используют класс Singleton для этой цели. Я заметил, что запись конфигурации в переменную класса будет иметь тот же эффект
Есть ли различия? Будет ли переменная класса безопасным решением в многопоточном приложении?
См. Пример кода ниже:
# using class variable
module MyApp
class << self
attr_accessor :config
def configure(config)
self.config = config
end
end
end
# using Singleton
module M
def self.config
Configuration.instance.data
end
def self.configure(config)
Configuration.instance.data = config
end
class Configuration
include Singleton
attr_accessor :data
# other methods
end
end
# or use class variable in the Configuration class itself
# in case if we need additional methods for configuration
module M
def self.config
Configuration.data
end
def self.configure(config)
Configuration.data = config
end
class Configuration
class << self
attr_accessor :data
end
# other methods
end
end