The application of single case mode in Ruby design pattern programming _ruby Special Topics

Source: Internet
Author: User
Tags class definition goto instance method require

Brief introduction
A single case pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a Class A unique instance of the system. To do this, you can start by instantiating it from the client. Therefore, you need to "block" all access to the object that you want to generate, using a mechanism that only allows you to generate unique instances of the object class. Use the factory method to limit the instantiation process. This method should be a static method (class method) because it makes no sense to let an instance of a class to generate another unique instance.


Main points
It is clear that there are three main points of the singleton pattern, one is that a class can have only one instance, and the other is that it must create the instance itself; third, it must provide this instance to the whole system.
From the point of view of implementation, it is the following three points: first, the class of the single case mode provides only the private constructor, the second is that the class definition contains a static private object of the class, and three is the class that provides the


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, the @ @class_count is 2, and the second @instance_count is 0, because the class variable is shared by all instances, the party cv1.increment calls two times after the @ @class_count is 2, When the second Classvariabletester object is created Cv2, the @ @class_count is shared, so the @ @class_count at this time is still 2.
The instance variable can only serve the current object, so the instance object Cv2 @ @instance_count is 0
This property of a class variable is a single case pattern

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 


The result is: true.
A class variable is used to hold an instance of the only class, and a class method is required to return the singleton instance.

However, you can create another instance object by Simplelogger.new, so you need to set a new method 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 


The result is: true

Singleton is provided in the Ruby class library to simplify the creation of a single instance class.
With Singleton, you omit the Create class variable, initialize the singleton instance, create a class-level instance method, and set new to private.
Use Simplelogger.instance to get a single example of the logger.

But the two ways are different.
The first is called the "diligence single case (eager instantiation)".
The instance object was created before it was really needed.
The second way is called "inert single case (lazy instantiation)"
is not created until instance is invoked.

But this singleton can't really stop anything, and you can use the Public_class_method to change the new method for the public.
When you open a class and set the new method to public, you can use Simplelogger.new to create the object.

Class Simplelogger 
 public_class_method:new 
end 
 
puts Simplelogger.new 

There are two different situations:

(a) Use global variables, try not to use global variables, because the global variables are tightly coupled to the program,
In fact, the single case pattern and the global variable function are the same,
$logger = Simplelogger.new

(ii) Use of classes as a single example,

Class Simplelogger 
  
 WARNING = 1 
 INFO = 2 
 
 def initialize (file) 
  @ @log = File.Open (file, "W") 
  @ @level = W Arning 
 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 
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 End End
test,test2 = autotest.instance
test. OpenURL (' http://www.baidu.com ')
test.set_textarea (' Aslandhu ')
Test.click


Although there are two autotest instances created here, the second instance is actually nil, which means no success was created.

Require ' rubygems ' require ' watir ' require ' singleton ' require '
thread '
class Testoneobj
 
end
class <<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
  End
Test = testoneobj.instance
test2 = testoneobj.instance
p test.inspect
p test2.inspect
Test.openurl (' www.baidu.com ')
test2.set_textarea (' Aslandhu ')
Test.click

The above code attempts to create two browser objects, but in fact the two objects created are the same. Although two IE windows are open, the object is still one, that is, test and test2 are the same object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.