Composition or Module

  • Thread starter Tomoyuki Kosimizu
  • Start date
T

Tomoyuki Kosimizu

Hi,

Let me hear your opinions, composition or module.

Composition:

class Representation
def to_string(obj)
return sprintf('%s=%s', obj.class, obj.value)
end
end

class Domain
def initialize
@value = 10
end
attr_reader :value

def to_s
Representation.new.to_string(self)
end
end

puts(Domain.new)

or Module:

module Representation
def to_s
return sprintf('%s=%s', self.class, @value)
end
end

class Domain
include Representation

def initialize
@value = 10
end
end

puts(Domain.new)

(e-mail address removed)-net.ne.jp
 
B

Bill Atkins

Module seems more natural to me, but maybe "Representable" would be a
better name.
 
D

Douglas Livingstone

def to_s
Module seems more natural to me, but maybe "Representable" would be a
better name.

Or something like "Pretty_s" because to_s exists anyway.

Douglas
 
B

Bill Atkins

Well, he wants to_s so that when you run "puts Domain.new" Ruby wil
automaticall call to_s on the new domain object and output the result.

Bill
 
R

Robert Klemme

Tomoyuki Kosimizu said:
Hi,

Let me hear your opinions, composition or module.

Composition:

class Representation
def to_string(obj)
return sprintf('%s=%s', obj.class, obj.value)
end
end

class Domain
def initialize
@value = 10
end
attr_reader :value

def to_s
Representation.new.to_string(self)

Not efficient because you create a stateless instance every time you need
the conversion to string. You could make Representation a singleton though.
end
end

puts(Domain.new)

or Module:

module Representation
def to_s
return sprintf('%s=%s', self.class, @value)

Rather do this as it is more flexible:

return sprintf('%s=%s', self.class, self.value)

Also, you might want to add

def value() nil end

to avoid errors for classes that don't implement "value()".
end
end

class Domain
include Representation

def initialize
@value = 10
end
end

puts(Domain.new)

Why do you want to separate this if you override Domain#to_s (i.e. a
standard method) anyway?

You could as well

- put the implementation into Domain#to_s and leave out Representation

- Make the conversion an instance method of the module so you don't need to
create new instances of the module all the time (for composition).

- Take the second approach (i.e. the one with the mixin) but make
Representation a base class.

Maybe there are other options but that depends on your design goal. If you
tell a bit more maybe we can come up with a more appropriate solution.

Kind regards

robert
 
R

Ruth A. Kramer

Bill said:
Module seems more natural to me, but maybe "Representable" would be a
better name.

I'm just a newbie/lurker, but I wonder why you suggest that, particulary
in that Representable is (so far) an adjective rather than a noun. I'm
fairly sure nouns serve better as names for objects--is it different for
modules?

(That's not to say a new usage of representable as a noun could not be
"coined". (My reference for the adjective bit is the current online
Merriam-Webster (m-w.com:
http://m-w.com/cgi-bin/dictionary?book=Dictionary&va=representable&x=10&y=15)

Just trying to clarify my own thinking.

regards,
Randy Kramer
 
Z

Zach Dennis

Ruth said:
I'm just a newbie/lurker, but I wonder why you suggest that, particulary
in that Representable is (so far) an adjective rather than a noun. I'm
fairly sure nouns serve better as names for objects--is it different for
modules?

(That's not to say a new usage of representable as a noun could not be
"coined". (My reference for the adjective bit is the current online
Merriam-Webster (m-w.com:
http://m-w.com/cgi-bin/dictionary?book=Dictionary&va=representable&x=10&y=15)

Just trying to clarify my own thinking.

According to your link:
"7 : to describe as having a specified character or quality "

Modules create a namespace for functionality that doesn't necessarily
constitute creating a class. With this you could say that Modules
describe a set of functionality (or characters/quality).

Modules aren't a thing, they don't describe an object, an item, a noun
by itself. They describe a set of functionality that can be used on a
*thing*. That's why they are so useful as mixins. This is also why I
agree with Bill Atkins in that Representable is a better name for a
Module, then Representation.

If you create a class MyRep which mixins module Representable you are
giving MyRep the behavior and ability to be a representation. This
behavior is best described as being Representable.

Zach
 
B

Bill Atkins

Modules generally add functionality to another module or class. Since
they can't be made into objects on their own, I think adjectives are
more module names than nouns. Basically, by including this module,
you're making a class able to represent itself (i.e. the class
becomes Representable). matz follows the same convention with the
Enumerable and Comparable classes in Ruby.

Bill
 
R

Ruth A. Kramer

Bill said:
Modules generally add functionality to another module or class. Since
they can't be made into objects on their own, I think adjectives are
more module names than nouns. Basically, by including this module,
you're making a class able to represent itself (i.e. the class
becomes Representable). matz follows the same convention with the
Enumerable and Comparable classes in Ruby.

Bill (and Zach),

Thanks!

Randy Kramer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top