Two objects with dependencies on each other. This is bad?

I learn a lot about design patterns when I build my own system for my projects. And I want to ask you about a design question that I cannot find an answer to.

I am currently creating a small Chat server using sockets with multiple clients. I now have three classes:

  • Person-class that contains information such as nickname, age, and Room object.
  • A room class that contains information such as the name of the room, topic, and a list of persons currently in the room.
  • A hotel class that has a list of persons and a list of rooms on the server.

I made a diagram to illustrate it:

I have a list of people on a server in a hotel class, because it would be nice to keep track of how much is online right now (without having to go through all the rooms). People live in a hotel-class, because I would like to be able to search for a specific person without looking for a room.

Is this a bad design? Is there any other way to achieve this?

Thanks.

+7
oop dependency-management dependencies
source share
4 answers

Strictly speaking, the problem of interdependence between classes can be solved using interfaces (abstract classes, if your language, for example, C ++ or Python) IRoom and IPerson ; in pseudo code

 interface IPerson IRoom getRoom() // etc interface IRoom iter<IPerson> iterPerson() // etc 

this makes only the interfaces interdependent from each other - the actual implementation of the interfaces should depend only on the interfaces.

It also gives you a lot of implementation possibilities if you want to avoid cyclic reference cycles (which can be a problem, for example, in CPython by slowing down garbage collection) - you can use weak references, a basic relational database with a typical one-to-one table many ", etc. etc. And for the first simple prototype, you can use the simplest one in the language of your choice (perhaps a simple and, alas, necessarily circular, links [[pointers, C ++]] with Person referring to a Room and a Room to a list<Person> )

+4
source share

I do not like this. The hotel has rooms, and the rooms have people. People do not contain rooms; they belong to them.

You do not have to iterate to get your guest score. You can simply save the counter ($ Hotel-> total_guests) and change it when it changes.

+9
source share

On a larger system, this would be bad, but since from what I understand in your applications, these three classes are only used together, this is not a big problem. Just remember to specify the member variables of the person so that they point to the link to the room, not to the instance.

In addition, if it is not related to operational considerations (for example, you will have a huge number of rooms), it would probably be cleaner to create a property or getter that will sort through the rooms and gather people, rather than cache them into the hotel.

+1
source share

Mutual dependence is not bad in itself. Sometimes this requires the use of data.

I think about it differently. It will be easier to maintain code in which there are fewer relationships at all - mutual dependence or not. Just keep it as simple as possible. The only additional trick in your situation is sometimes the problem of checking and egg during the creation and deletion of sequences. You have more links to bookkeeping.

If you ask if you need a list of people in the hotel in this case, I think there are two answers. I would start by saying that your objects (in memory) provided this relationship, but you do not need an additional table of connections between people and hotels in the database. If you use Hibernate, it automatically generates an effective connection for you if you ask for it for people at the hotel (it will join the hotels at .hotel_id numbers for you).

0
source share

All Articles