Detailed description of the Application of Singleton mode in Ruby Design Mode Programming, and ruby Design Mode
Introduction
Singleton mode is one of the simplest forms of design patterns. This mode aims to make an object of A Class A unique instance in the system. To achieve this, you can start by instantiating the client. Therefore, we need to use a mechanism that only allows the generation of unique instances of the object class to "Block" access to all objects to be generated. Use the factory method to restrict the instantiation process. This method should be a static method (class method), because it is meaningless to let the class instance generate another unique instance.
Key Points
Obviously, the singleton mode has three key points. One is that a class can only have one instance; the other is that it must create the instance on its own; and the third is that it must provide the instance to the entire system on its own.
From a specific implementation perspective, there are three points: first, the class in singleton mode only provides private constructors, and second, the class definition contains a static private object of this class, third, this class provides
Singleton
class ClassVariableTester @@class_count = 0 def initialize @instance_count = 0 end def increment @@class_count = @@class_count + 1 @instance_count = @instance_count + 1 end def to_s "class count :#{@@class_count} -- instance count :#{@instance_count}" end end cv1 = ClassVariableTester.new cv1.increment cv1.increment puts("cv1:#{cv1}") cv2 = ClassVariableTester.new puts("cv2:#{cv2}") #cv1:class count :2 -- instance count :2 #cv2:class count :2 -- instance count :0
When the second object is created, @ class_count is 2, and 2 @ instance_count is 0 because class variables are shared by all instances, after cv1.increment is called twice, @ class_count is 2. When cv2 is created for the second ClassVariableTester object, @ class_count is shared. Therefore, @ class_count is still 2 at this time.
The instance variable can only serve the current object, so the @ instance_count of the Instance Object cv2 is 0.
This feature of class variables is a singleton mode.
class SimpleLogger @@instance = SimpleLogger.new def self.get_instance @@instance end private_class_method :new end sl1 = SimpleLogger.get_instance sl2 = SimpleLogger.get_instance puts sl1 == sl2
Result: true.
A class variable is used to save only one class instance, and a class method is required to return this Singleton instance.
However, you can still create another instance object through SimpleLogger. new. Therefore, you need to set a new method as private.
sl3 = SimpleLogger.new private method `new' called for SimpleLogger:Class (NoMethodError) require 'singleton' class SimpleLogger include Singleton end #puts SimpleLogger.new sl1 = SimpleLogger.instance sl2 = SimpleLogger.instance puts sl1 == sl2
Result: true
The Ruby Class Library provides singleton to simplify the creation of singleton classes.
If Singleton is mixed in, class variables are omitted. initialize the Singleton instance, create the class-level instance method, and set new to private.
You can use SimpleLogger. instance to obtain the logstore Singleton.
However, the two methods are still different.
The first method is called "eager instantiation )".
An instance object is created before it is required.
The second method is called "lazy instantiation )"
It is created only when the instance is called.
However, this Singleton cannot really stop anything. You can use public_class_method to change the new method to public.
Open the class and set the new method to public. You can use SimpleLogger. new to create an object.
class SimpleLogger public_class_method :new end puts SimpleLogger.new
There are two cases:
(1) Use global variables. Do not use global variables whenever possible, because global variables are tightly coupled by programs,
In fact, Singleton mode and global variables play the same role,
$ Logger = SimpleLogger. new
(2) Use a class as a singleton,
class SimpleLogger WARNING = 1 INFO = 2 def initialize(file) @@log = File.open(file, "w") @@level = WARNING end def self.warning(msg) puts @@level > WARNING @@log.puts(msg) if @@level > WARNING @@log.flush end def self.level @@level end def self.level=(new_level) @@level = new_level end end SimpleLogger.new("test.txt") puts SimpleLogger.level SimpleLogger.level = SimpleLogger::INFO puts SimpleLogger.level SimpleLogger.warning("warning")
Instance
require 'rubygems'require 'watir'require 'singleton'class AutoTest include Singleton def OpenUrl(url) @browser= Watir::Browser.new @browser.goto(url) @url=url end def set_textarea(text) @browser.text_field(:id,'kw').set(text) end def click @browser.button(:id,'su').click endendtest,test2 = AutoTest.instancetest.OpenUrl('http://www.baidu.com')test.set_textarea('aslandhu')test.click
Although two AutoTest instances are created here, the second instance is actually nil, that is, it is not created successfully.
require 'rubygems'require 'watir'require 'singleton'require 'thread'class TestOneObj endclass <<TestOneObj include Singleton def instance @browser= Watir::Browser.new self end def openurl(url) @browser.goto(url) end def set_textarea(text) @browser.text_field(:id,'kw').set(text) end def click @browser.button(:id,'su').click endendtest = TestOneObj.instancetest2 = TestOneObj.instancep test.inspectp test2.inspecttest.openurl('www.baidu.com')test2.set_textarea('aslandhu')test.click
The above code tries to create two Browser objects, but in fact both objects are the same. Although two IE Windows are opened, the object is still one, that is, test and test2 are the same object.
Articles you may be interested in:
- Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
- The template method in the design mode applies two examples in Ruby.
- Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
- The example explains how Ruby uses the decorator mode in the design mode.
- Example of using Builder mode in Ruby Design Mode Programming
- Programming in Ruby design mode-Introduction to adapter Mode
- Ruby uses code instances in the proxy mode and decoration mode in the Design Mode
- Ruby uses the simple factory mode and factory method mode in the Design Mode
- Application instance analysis of appearance mode in Ruby Design Mode Programming