Re: Observer Design Pattern
- From: Joe Van Dyk <joe.vandyk@xxxxxxxxxx>
- Date: Thu, 22 Jun 2006 20:55:53 GMT
Oliver Wong wrote:
"Joe Van Dyk" <joe.vandyk@xxxxxxxxxx> wrote in message news:J05041.K0B@xxxxxxxxxxxxxxxxxx
Robert Martin wrote:
On 2006-05-31 06:00:00 -0500, "Krivenok Dmitry" <dima@xxxxxxxxxxxxxxxxxx> said:
Hello All!
I am trying to implement my own Design Patterns Library.
If you are talking about a code library (as opposed to a library of documents) then I'd like to discourage you. Design Patterns are not the kind of thing that you can turn into a reusable coding library.
Observer is one of the exceptions to this rule; but it's only a partial exception for the reasons that you have already pointed out in your post.
<snip>
# Ruby
require 'singleton'
class Logger
include Singleton
end
Logger is now a singleton class. Is that one of the exceptions?
I'm not familiar with Ruby, so I'm curious about the above code. I know almost nothing about Ruby, so I'm making some blind assumptions here: I assume that Ruby has the concepts of static methods, of contructors, and of constructor visibility (e.g. public, private, etc.)
Usually, singletons would have a static factory method (e.g. "getSoleInstance()") to make themselves useful. Does the above code somehow generate a static factory method for Logger? If so, how does the factory method decide which constructor to invoke? Will the include statement make the constructors become private if they were declared public?
- Oliver
http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html might answer your question.
Essentially:
- The constructor is now private
- There's now a instance method in the Logger class that returns the instance
Example:
============================================
class Normal_Logger
def initialize
puts "Normal_Logger::initialize called. "
puts " -- My object id is #{ object_id }"
end
end
class Singular_Logger
include Singleton
def initialize
puts "Singular_Logger::initialize called."
puts " -- My object id is #{ object_id }"
end
end
# Constructor called twice -- new objects created
first_normal = Normal_Logger.new
second_normal = Normal_Logger.new
fail unless first_normal.object_id != second_normal.object_id
# Constructor called once -- one object created
first_singular = Singular_Logger.instance
second_singular = Singular_Logger.instance
fail unless first_singular.object_id == second_singular.object_id
# Won't work, the constructor is private
# oops = Singular_Logger.new
============================================
Results in:
Normal_Logger::initialize called.
-- My object id is -604097668
Normal_Logger::initialize called.
-- My object id is -604097768
Singular_Logger::initialize called.
-- My object id is -604097898
.
- References:
- Re: Observer Design Pattern
- From: Oliver Wong
- Re: Observer Design Pattern
- Prev by Date: Re: OOPSLA questions
- Next by Date: Re: OO versus RDB
- Previous by thread: Re: Observer Design Pattern
- Next by thread: UML Use cases --> Context diagram
- Index(es):
Relevant Pages
|