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
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
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??
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
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?
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??
Steve
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.
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?
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|