Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to model something

Reply
Thread Tools

How to model something

 
 
Tobias Weber
Guest
Posts: n/a
 
      05-24-2008
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
 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      05-24-2008

-------- Original-Nachricht --------
> Datum: Sat, 24 May 2008 20:09:51 +0900
> Von: Tobias Weber <(E-Mail Removed)>
> An: http://www.velocityreviews.com/forums/(E-Mail 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)
>
> --
> Tobias Weber


Hi Tobias,

I'd use Struct ...


Best regards,

Axel


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

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,'spac e',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

------------------------------------------------------------------


--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-24-2008

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

 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      05-25-2008
[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


 
Reply With Quote
 
Tobias Weber
Guest
Posts: n/a
 
      05-25-2008
In article <(E-Mail Removed)>,
Mark Wilden <(E-Mail Removed)> 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
 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      05-25-2008

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

> In article <(E-Mail Removed)>,
> Mark Wilden <(E-Mail Removed)> 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





 
Reply With Quote
 
Tobias Weber
Guest
Posts: n/a
 
      05-27-2008
In article <(E-Mail Removed)>,
Mark Wilden <(E-Mail Removed)> 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
 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      05-27-2008

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
XPath query for <?define something="something" ?> Pekka Järvinen XML 2 04-29-2008 08:12 PM
How to find and replace something that is nested inside something else? alainfri@gmail.com Perl Misc 4 05-31-2007 11:50 PM
Convert Java Model to Java Model without XML erinbot@gmail.com Java 1 10-06-2006 09:00 PM
umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless and clueless o.0 C++ 4 10-13-2004 10:34 PM



Advertisments