[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to model something

Tobias Weber

5/24/2008 11:08:00 AM

Hi,
my programm needs to store persons. They have a weight and a gender. You
are only allowed to ask males for their weight, females will throw
dishes/exceptions. There *is* a method to change someones gender.

I could have 'if gender' in every accessor or use the state pattern. But
that has no state, so I'm confused. I would have to access the variable
in the wrapper class.

The programm can also shoot people into space, in which case the
implementation of weight changes but the same restrictions apply.
The proxy pattern will help here, probably by having weight first call
delegate.weight, and if that isn't NoMethodException multiplying with
gravity.

Any advice or implementations of similar problems I could look at?

(and in reality people are TV series with 6 genders and 5 other
attributes, and space is just but a remote place to store things)

--
Tobias Weber
6 Answers

Robert Klemme

5/24/2008 12:58:00 PM

0


Some random thougths...

On 24.05.2008 13:08, Tobias Weber wrote:
> my programm needs to store persons. They have a weight and a gender. You
> are only allowed to ask males for their weight, females will throw
> dishes/exceptions. There *is* a method to change someones gender.
>
> I could have 'if gender' in every accessor or use the state pattern. But
> that has no state, so I'm confused. I would have to access the variable
> in the wrapper class.

I am not sure what you mean here. Here's one way to do it:

class Person
GENDERS = {
:male => Class.new do
def gender; :male; end
def weight_check; end
end.new,
:female => Class.new do
def gender; :female; end
def weight_check; raise "Go away!" end
end.new,
}.freeze

def initialize(gender)
self.gender = gender
end

def gender=(g)
@gender = GENDERS[g] or raise "Illegal gender #{g.inspect}"
end

def gender; @gender.gender; end

def weight=(w)
raise IllegalArgumentException, "negative" if w < 0
@weight = w
end

def weight; @gender.weight_check; @weight; end
end

> The programm can also shoot people into space, in which case the
> implementation of weight changes but the same restrictions apply.
> The proxy pattern will help here, probably by having weight first call
> delegate.weight, and if that isn't NoMethodException multiplying with
> gravity.

Well, in that case #weight isn't just a simple accessor but it needs an
argument (gravity). You probably want to have an attribute "mass" which
stores the proper value.

> Any advice or implementations of similar problems I could look at?
>
> (and in reality people are TV series with 6 genders and 5 other
> attributes, and space is just but a remote place to store things)

What are the other four genders? :-))

Kind regards

robert

Mark Wilden

5/25/2008 1:42:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
> Hi,
> my programm needs to store persons. They have a weight and a gender.
> You
> are only allowed to ask males for their weight, females will throw
> dishes/exceptions. There *is* a method to change someones gender.
>
> I could have 'if gender' in every accessor

That seems like the simplest approach. The person object is the one
who's creating this restriction - weight shouldn't know about gender
and gender shouldn't know about weight. Wouldn't that only affect one
accessor - weight?

///ark


Tobias Weber

5/25/2008 2:16:00 PM

0

In article <F7E21685-9F99-48F0-A3AC-990A18B0294E@mwilden.com>,
Mark Wilden <mark@mwilden.com> wrote:

> [Note: parts of this message were removed to make it a legal post.]

?

> > I could have 'if gender' in every accessor
>
> That seems like the simplest approach. The person object is the one
> who's creating this restriction - weight shouldn't know about gender
> and gender shouldn't know about weight. Wouldn't that only affect one
> accessor - weight?

In the example, yes. In reality it's 3 attributes and writing is
forbidden as well. I implemented it using one class and an Integer for
the genders and it got pretty messy as (storage) space needs to
manufacture persons and therefore write female's weight where it
normally wouldn't be allowed.

I worked around that using set_instance_variable but felt awful.

So I rewrote everything using the state pattern, but with a Struct
containing the attributes being passed around between state instances.
Even uglier code.

I hate Java, but its protected package access would help with this.

--
Tobias Weber

Mark Wilden

5/25/2008 2:56:00 PM

0


On May 25, 2008, at 7:19 AM, Tobias Weber wrote:

> In article <F7E21685-9F99-48F0-A3AC-990A18B0294E@mwilden.com>,
> Mark Wilden <mark@mwilden.com> wrote:
>
>> [Note: parts of this message were removed to make it a legal post.]
>
> ?

Beats me!

>>> I could have 'if gender' in every accessor
>>
>> That seems like the simplest approach. The person object is the one
>> who's creating this restriction - weight shouldn't know about gender
>> and gender shouldn't know about weight. Wouldn't that only affect one
>> accessor - weight?
>
> In the example, yes. In reality it's 3 attributes and writing is
> forbidden as well. I implemented it using one class and an Integer for
> the genders and it got pretty messy as (storage) space needs to
> manufacture persons and therefore write female's weight where it
> normally wouldn't be allowed.
>
> I worked around that using set_instance_variable but felt awful.
>
> So I rewrote everything using the state pattern, but with a Struct
> containing the attributes being passed around between state instances.
> Even uglier code.
>
> I hate Java, but its protected package access would help with this.

To my mind, you've got a "smell" here that's caused by wanting to use
inheritance instead of composition (the State pattern mimics
inheritance, of course). You want a person to be-a gender, not have-a
gender. This is implemented via State by having-a gender-weight
gatekeeper. This is motivated by wanting to localize knowledge of the
relationship between gender and weight, which is a good goal. However,
it's causing you to need to use backdoors like set_instance_variable
or Java's protected package access. To me, that indicates that the
object model may be wrong.

Even though gender conditions access to weight, that doesn't mean that
gender has to know about weight (which it does in your
implementation). Is gender a behavior (or state), or is it an
attribute? I think it's the latter. If you have the person object
mediate access to one of its attributes based on another of its
attributes, you put the responsibility where it truly lies.

But this is me just thinking out loud. It's your model and you know it
better than I do. I think I would give the 'if gender' approach a shot
before resorting to State, especially if the latter forces you to
violate encapsulation. I'm not a big fan of State (or Mediator, for
that matter) when there are simpler, more direct, more obvious
approaches. But that's just my armchair perspective. Good luck! :)

///ark





Tobias Weber

5/27/2008 10:58:00 AM

0

In article <002ADDAF-2900-45AE-9627-1E6EA25A20CD@mwilden.com>,
Mark Wilden <mark@mwilden.com> wrote:

> attribute? I think it's the latter. If you have the person object
> mediate access to one of its attributes based on another of its
> attributes, you put the responsibility where it truly lies.

I now distributed the possible states along two dimensions and represent
one by the persons being observed by two different storage models, one
offline and one in space.

The observers know a bit about the gender dimension and don't ask
females their weight.

Of course I left the checks in Person, nevertheless. As the class has to
do much less now it looks clean despite them.

--
Tobias Weber

Mark Wilden

5/27/2008 2:25:00 PM

0


On May 27, 2008, at 3:59 AM, Tobias Weber wrote:

> I now distributed the possible states along two dimensions and
> represent
> one by the persons being observed by two different storage models, one
> offline and one in space.
>
> The observers know a bit about the gender dimension and don't ask
> females their weight.
>
> Of course I left the checks in Person, nevertheless. As the class
> has to
> do much less now it looks clean despite them.

Sounds interesting, but I'm not quite able to visualize it. If you'd
care to, it would be great to see some pared-down code illustrating
the method you adopted.

///ark