• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Want to access the Composer object from the Composed object.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I want to access composer object from the composed object. Say I have a chess room and a chess board. When drawing class relationship chess room will be composed of chess board. Now the problem is I can not find a way to access the chess room method from the chess board object. Please help me on this. Thanks
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

waqas imtiaz wrote:Hi I want to access composer object from the composed object. Say I have a chess room and a chess board. When drawing class relationship chess room will be composed of chess board. Now the problem is I can not find a way to access the chess room method from the chess board object. Please help me on this. Thanks



Two possible responses...

One, why would you want to do this? Why would a chess board need to know what room it is in? Heck, what if it is not in a room, such as a chess board in a public park?

Two, I guess you can always add that information during construction -- meaning pass the chess room to the chess board as a parameter of one of its constructors.

Henry
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Two possible responses...

One, why would you want to do this? Why would a chess board need to know what room it is in? Heck, what if it is not in a room, such as a chess board in a public park?

Two, I guess you can always add that information during construction -- meaning pass the chess room to the chess board as a parameter of one of its constructors.

Henry



Well the reason why I want to access chess room because chess room contains a list of players that are in the the chess room and I need to know about these players from the chess board.
Chess board in my case can not be in the park as there is no park in this case and you have to choose composition or aggregation based on the scenario and in my scenario chess board can not reside out side the chess room as there is nothing else to sit in.

The method you suggested I think is not a good way to do it. Is it?? It is not in favour of object oriented programming and my cause problems during maintenance as well. Please if there is any other way to get access??
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

waqas imtiaz wrote:
The method you suggested I think is not a good way to do it. Is it?? It is not in favour of object oriented programming and my cause problems during maintenance as well. Please if there is any other way to get access??



To be fair, a chess board that knows what room it is in, or any other non-chessboard information, is also arguably not "object oriented" like either.

Anyway, you can't perform black magic here. If you need to access something, then you need a way to access it. If you don't want to provide direct access, then you need some sort of starting point, where you can search through to find the information you desire. And yes, your argument against it applies, even if the access is indirect via some sort of search.

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

waqas imtiaz wrote:Hi I want to access composer object from the composed object. Say I have a chess room and a chess board. When drawing class relationship chess room will be composed of chess board. Now the problem is I can not find a way to access the chess room method from the chess board object. Please help me on this. Thanks


Actually, I think there's more than one relationship going on here. As Henry says, it's not really the business of a chess-board to know where it is, since it could just as easily be in a cupboard somewhere (or, as far as your app is concerned, "nowhere").

It is, however, likely that a room could contain lots of things, including chairs, tables, cabinets, desks, and anything else you fancy sticking in it. And that suggests to me a ContainerItem relationship - where an "Item" is simply a wrapper to something (like a ChessBoard) that does know what Container it's in, and can return either its Container or its "wrapped thing" (in your case, a ChessBoard).

Now, if a Room IS-A Container, you can stick as many Item<ChessBoard>'s in it as you like.

It's still as true now as it was in 1972: "All problems in computer science can be solved by another level of indirection".

Winston
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

waqas imtiaz wrote:
The method you suggested I think is not a good way to do it. Is it?? It is not in favour of object oriented programming and my cause problems during maintenance as well. Please if there is any other way to get access??



To be fair, a chess board that knows what room it is in, or any other non-chessboard information, is also arguably not "object oriented" like either.

Anyway, you can't perform black magic here. If you need to access something, then you need a way to access it. If you don't want to provide direct access, then you need some sort of starting point, where you can search through to find the information you desire. And yes, your argument against it applies, even if the access is indirect via some sort of search.

Henry



Well Henry with due respect I am of the opinion of that this information should be a part of the object. Like I know where I am sitting. Though this home address does not belong to me but where I am currently sitting belongs to me. but anyway. Thanks man. Do you have any other option on it please?
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

waqas imtiaz wrote:
Well Henry with due respect I am of the opinion of that this information should be a part of the object. Like I know where I am sitting. Though this home address does not belong to me but where I am currently sitting belongs to me. but anyway. Thanks man. Do you have any other option on it please?



I am not sure of the issue here... if you think that the information should be part of the object, then make it part of the object.

waqas imtiaz wrote:
The method you suggested I think is not a good way to do it. Is it?? It is not in favour of object oriented programming and my cause problems during maintenance as well. Please if there is any other way to get access??



If you think that the information should not be part of the object, then don't make it part of the object.


It's your program, do what you wish. As for other options, that is your choice too. A setter method? A global variable somewhere? etc. etc. etc.

Henry
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry I think I got it. There is an aggregation relationship between the chess board and players in the chess room. You know one player will play on chess board while other will wait for its turn and will not contributing chess board at this time. So yes there is an aggregation relationship between chess board and players. While players will be in composition relationship with player list. Anyway thanks Henry for helping me think that there would be something missing in the design.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you ask me, your object graph is too flat, and it puts too many responsibilities on too few things. A ChessBoard should conceptually be about a map of pieces, it doesn't really need to know about players.

What I would think a better design would be:
A ChessRoom has a ChessGame (and maybe other things)
- A ChessGame has 2 Players and a ChessBoard
-- A ChessBoard has ChessPieces

The ChessBoard cares naught about players, and only about the positions of its pieces and legality and results of moves.
A ChessGame would control each player's access to the board, making sure they each get their turn.
A Player would need a reference to the board so it can make its move.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:If you ask me, your object graph is too flat, and it puts too many responsibilities on too few things. A ChessBoard should conceptually be about a map of pieces, it doesn't really need to know about players.

What I would think a better design would be:
A ChessRoom has a ChessGame (and maybe other things)
- A ChessGame has 2 Players and a ChessBoard
-- A ChessBoard has ChessPieces

The ChessBoard cares naught about players, and only about the positions of its pieces and legality and results of moves.
A ChessGame would control each player's access to the board, making sure they each get their turn.
A Player would need a reference to the board so it can make its move.



Well I have built all that stuff but here when player switches on turn. Chess board needs to know which player's move it is so that this players pieces on the chess board would be able to move only, not the other one's.

May be I am wrong but you need to explain what do you refer as ChessGame??? What characteristics this chess game has?
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

waqas imtiaz wrote:
Well I have built all that stuff but here when player switches on turn. Chess board needs to know which player's move it is so that this players pieces on the chess board would be able to move only, not the other one's.

May be I am wrong but you need to explain what do you refer as ChessGame??? What characteristics this chess game has?



I believe what Steve is saying, and what we have been trying to gently push you towards (but doing a bad job at it) is... Not all objects in an application have to be actual physical objects in the real world.

For example, it doesn't make sense for a chessboard to manage the process of a game. It doesn't make sense for a single player either -- as there is more than one player. There needs to be an entity that will collect the moves from the players, manage the turns, manipulate the chessboard, declare the winner, etc.

So, it may be better to create a ChessGame class -- a class that represents a process, and not a real physical object in the real world. It may also need some helper classes, which represents some other processes, etc.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic