Re: Observer Design Pattern



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
.



Relevant Pages

  • Re: My LogGenerator.java wont compile - too many errors - help
    ... I meant static factory method, it's what you implemented to replace the ... constructor that you made private. ... Consider the singleton pattern. ...
    (comp.lang.java.help)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)
  • Re: My LogGenerator.java wont compile - too many errors - help
    ... A static factory method is a normal method, getName) that is used to create instances of a class. ... Normally instances are created by a constructor. ... Consider the singleton pattern. ...
    (comp.lang.java.help)
  • Re: reflection a singleton class?
    ... inside the singleton in its constructor. ... private S ... > what I want to do is to invoked the Init() method on all these classes ...
    (microsoft.public.dotnet.languages.csharp)