Is Having an Instance of a Class Inside Another Class Considered a Bad Practice?
up vote
0
down vote
favorite
So, basically what I'm trying to say is to imagine that you are working on a project that is a hotel reservation system.
So you have the HotelMenu class which will show the options for the Hotel at which you are trying to stay. Inside the HotelMenu, there will a field of type Hotel, which contains all the methods that you could do for a Hotel, such as makeReservation(), cancelReservation(), seeRoomsAvailable(), and so on.
At the same time, inside the Hotel class, there would be an instance variable of type Room, which would contain the room characteristics such as bedSize, for example.
Would the above program be a bad programming practice. I thought so because if you were to make a change in the Room class, then it would break all the other classes. Like let's say that you are modifying the Room class to add a method to change the bedSize, and this would in turn affect the Hotel class, which would ultimately affect the HotelMenu, breaking the whole system. Therefore, would this be an example of bad programming practice due to tight coupling?
architecture abstraction
add a comment |
up vote
0
down vote
favorite
So, basically what I'm trying to say is to imagine that you are working on a project that is a hotel reservation system.
So you have the HotelMenu class which will show the options for the Hotel at which you are trying to stay. Inside the HotelMenu, there will a field of type Hotel, which contains all the methods that you could do for a Hotel, such as makeReservation(), cancelReservation(), seeRoomsAvailable(), and so on.
At the same time, inside the Hotel class, there would be an instance variable of type Room, which would contain the room characteristics such as bedSize, for example.
Would the above program be a bad programming practice. I thought so because if you were to make a change in the Room class, then it would break all the other classes. Like let's say that you are modifying the Room class to add a method to change the bedSize, and this would in turn affect the Hotel class, which would ultimately affect the HotelMenu, breaking the whole system. Therefore, would this be an example of bad programming practice due to tight coupling?
architecture abstraction
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So, basically what I'm trying to say is to imagine that you are working on a project that is a hotel reservation system.
So you have the HotelMenu class which will show the options for the Hotel at which you are trying to stay. Inside the HotelMenu, there will a field of type Hotel, which contains all the methods that you could do for a Hotel, such as makeReservation(), cancelReservation(), seeRoomsAvailable(), and so on.
At the same time, inside the Hotel class, there would be an instance variable of type Room, which would contain the room characteristics such as bedSize, for example.
Would the above program be a bad programming practice. I thought so because if you were to make a change in the Room class, then it would break all the other classes. Like let's say that you are modifying the Room class to add a method to change the bedSize, and this would in turn affect the Hotel class, which would ultimately affect the HotelMenu, breaking the whole system. Therefore, would this be an example of bad programming practice due to tight coupling?
architecture abstraction
So, basically what I'm trying to say is to imagine that you are working on a project that is a hotel reservation system.
So you have the HotelMenu class which will show the options for the Hotel at which you are trying to stay. Inside the HotelMenu, there will a field of type Hotel, which contains all the methods that you could do for a Hotel, such as makeReservation(), cancelReservation(), seeRoomsAvailable(), and so on.
At the same time, inside the Hotel class, there would be an instance variable of type Room, which would contain the room characteristics such as bedSize, for example.
Would the above program be a bad programming practice. I thought so because if you were to make a change in the Room class, then it would break all the other classes. Like let's say that you are modifying the Room class to add a method to change the bedSize, and this would in turn affect the Hotel class, which would ultimately affect the HotelMenu, breaking the whole system. Therefore, would this be an example of bad programming practice due to tight coupling?
architecture abstraction
architecture abstraction
asked Nov 14 at 22:51
Alvaro Solari
12
12
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309919%2fis-having-an-instance-of-a-class-inside-another-class-considered-a-bad-practice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown