How to model something

T

Tobias Weber

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)
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sat, 24 May 2008 20:09:51 +0900
Von: Tobias Weber <[email protected]>
An: (e-mail address removed)
Betreff: How to model something
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)

Hi Tobias,

I'd use Struct ...


Best regards,

Axel


-----------------------------------------------------------
Human=Struct.new:)name,:gender,:weight,:position)

def check_and_multiply(arg,default_value,factor)
# return factor, if arg==default_value, else return 1
if arg==default_value
return factor
else
return 1
end
end
class Human
def ask_for_weight(weight_loss_factor=0.5)
if self.gender=='female'
return "Don't ask me this!"
else
return self.weight*check_and_multiply(self.position,'space',weight_loss_factor)
end
end
def change_sex
temp=self.dup
if self.gender=='male'
temp.gender='female'
end
if self.gender=='female'
temp.gender='male'
end
return temp
end
def change_position
temp=self.dup
if self.position=='earth'
temp.gender='space'
end
if self.gender=='space'
temp.gender='earth'
end
return temp
end

end



man1=Human.new('John','male',75.0,'space')
man2=Human.new('James','male',75.0,'earth')
woman=Human.new('Mary','female',55.0)
p woman.ask_for_weight
p man1.ask_for_weight
p man2.ask_for_weight
p man2.change_sex.name
 
R

Robert Klemme

Some random thougths...

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
 
M

Mark Wilden

[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
 
T

Tobias Weber

Mark Wilden said:
[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.
 
M

Mark Wilden

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

?

Beats me!
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
 
T

Tobias Weber

Mark Wilden said:
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.
 
M

Mark Wilden

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
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top