PC Review


Reply
Thread Tools Rate Thread

Design issue about which class a method should belong to

 
 
Tony Johansson
Guest
Posts: n/a
 
      17th Jan 2010
Hello!

We have the two classes Person and Elevator.
I just wonder method NewFloorRequest that set the floor that the elevator
should go to which class should it belong to between Person and Elevator.

I mean if I put it in class Elevator we can pass the floor that we want to
go to as a parameter.
Class Elevator has information about how many floor the elevator has which
mean the method can check if
the passed floor for NewFloorRequest is valid.

On the other hand if I put it in class Person which doesn't seem right but
if I still put it in class Person
we can return the floor that has been selected. Class Person doesn't know
anything about how many floor the elevator has
so that means the returned fllor might not be valid.

Which class would you thing is the best choice between Person and Elevator
for method NewFloorRequest ?
I hope you can give some motivation about you choice so I get a better
understanding about why the selected class is the best choice.

//Tony


 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      17th Jan 2010
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> We have the two classes Person and Elevator.
> I just wonder method NewFloorRequest that set the floor that the elevator
> should go to which class should it belong to between Person and Elevator.
>
> I mean if I put it in class Elevator we can pass the floor that we want to
> go to as a parameter.
> Class Elevator has information about how many floor the elevator has which
> mean the method can check if
> the passed floor for NewFloorRequest is valid.
>
> On the other hand if I put it in class Person which doesn't seem right but
> if I still put it in class Person
> we can return the floor that has been selected. Class Person doesn't know
> anything about how many floor the elevator has
> so that means the returned fllor might not be valid.
>
> Which class would you thing is the best choice between Person and Elevator
> for method NewFloorRequest ?
> I hope you can give some motivation about you choice so I get a better
> understanding about why the selected class is the best choice.


Basically, a class is supposed to contain data and the methods that
operate on that data.
The way that you describe the method NewFloorRequest, it seems to operate
only on the data of class Elevator (since it accesses the number of floors).
You have not described the method as doing anything on the data of Person
(such as keeping track of which is the floor that a Person requested).
Therefore, the method should go into class Elevator.

 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      17th Jan 2010
Tony Johansson wrote:
> Hello!
>
> We have the two classes Person and Elevator.
> I just wonder method NewFloorRequest that set the floor that the elevator
> should go to which class should it belong to between Person and Elevator.
>
> [...]
> Which class would you thing is the best choice between Person and Elevator
> for method NewFloorRequest ?
> I hope you can give some motivation about you choice so I get a better
> understanding about why the selected class is the best choice.


Does it have to go into either? It seems to me that both the Person and
the Elevator class are entities existing within a large framework. In
particular, there is a broader "controller" object that should be
receiving requests from the Person and translating that into control for
the Elevator.

When you have just one Elevator object, you might be able to get away
with combining the controller and behavior logic into the same class,
but that doesn't leave the design much room to grow. Once you have
multiple Elevator instances, the controller will want to direct all
Elevator instances using broader information about the state of all
Elevator instances and all Person instances.

Putting the control logic into either the Person _or_ the Elevator will
force you into a situation where individual instances require too much
information about other instances, creating a dual role for that class
that violates the general goal of OOP that each object do one thing
well, rather than many things poorly.

Pete
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      17th Jan 2010
On 17-01-2010 05:16, Tony Johansson wrote:
> We have the two classes Person and Elevator.
> I just wonder method NewFloorRequest that set the floor that the elevator
> should go to which class should it belong to between Person and Elevator.
>
> I mean if I put it in class Elevator we can pass the floor that we want to
> go to as a parameter.
> Class Elevator has information about how many floor the elevator has which
> mean the method can check if
> the passed floor for NewFloorRequest is valid.
>
> On the other hand if I put it in class Person which doesn't seem right but
> if I still put it in class Person
> we can return the floor that has been selected. Class Person doesn't know
> anything about how many floor the elevator has
> so that means the returned fllor might not be valid.
>
> Which class would you thing is the best choice between Person and Elevator
> for method NewFloorRequest ?
> I hope you can give some motivation about you choice so I get a better
> understanding about why the selected class is the best choice.


Definitely Elevator.

The method is a message that change the state of Elevator and can
be use by Person and other classes.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Issue with moving a common method to base class Curious Microsoft Dot NET 3 10th Dec 2007 04:21 PM
Design issue: Struct vs. Class Pohihihi Microsoft C# .NET 2 26th Dec 2005 07:53 AM
Should Method Belong to the Class? Trip Microsoft Excel Programming 1 20th Sep 2005 09:29 PM
Design issue: Property vs. Method Jeff Robichaud Microsoft Dot NET Framework 3 25th Feb 2005 04:34 PM
CSharp Class Design Issue Sunny Microsoft C# .NET 12 8th Jan 2004 08:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:41 PM.