saasbook

Singleton example

Aug 13th, 2012
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.45 KB | None | 0 0
  1. class Customer
  2.  
  3.   # attr_accessor :name
  4.   def name ; @name ; end
  5.   def name=(newname) ; @name = newname ; end
  6.  
  7.   # accessor for the singleton
  8.  
  9.   def self.null_customer
  10.     @@instance ||= Customer.new
  11.     # put the singleton in its own class!
  12.     class << @@instance
  13.       def name ; "ANONYMOUS" ; end
  14.       def name=(new) ; raise "Can't change name of null customer " ; end
  15.       def logged_in? ; false ; end
  16.     end
  17.     @@instance
  18.   end
  19.  
  20. end
Advertisement
Add Comment
Please, Sign In to add comment