Design issue about which class a method should belong to

T

Tony Johansson

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
 
A

Alberto Poblacion

Tony Johansson said:
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.
 
P

Peter Duniho

Tony said:
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
 
A

Arne Vajhøj

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
 

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

Top