OOP question regarding object instances, observer/observable pattern, and singletons

OOP question regarding object instances, observer/observable pattern, and singletons



Making a minesweeper app and I have a GameGrid class to hold all the squares in a multidimensional arraylist of squares, and it handles initializing the squares along with determining where the mines will be. The Square class has variables for its position (x and y), and a variable hasMine. Now, due to some circumstances arising, I have needed to pass a GameGrid instance into the constructor of Square because I needed to access a method inside GameGrid. My question is, is this good design? For some reason, the fact that I'm passing a GameGrid instance into a Square just doesn't feel right.


GameGrid


Square


x


y


hasMine


GameGrid


Square


GameGrid


GameGrid


Square



I've tried to experiment with an Observer/Observable type pattern with unfortunately no luck. Another option would be to make GameGrid a singleton, just not sure if that's the best option here.


GameGrid



I bet a similar situation like this has probably come up before, just wondering what the best solution would be.



Edit:



Decided to clear a few things up and clarify the actual problem I'm running into.



So each square is responsible for changing its own icon when it's clicked (when it's flipped over). The problem arises when I want to implement the O-fill behavior when a zero is clicked (this concept is easy if you're familiar with minesweeper). When a zero is clicked on, all the zeroes around will automatically be flipped over. I need to use GameGrid for this and not Square because Square doesn't have access to all the squares around it.


O-fill


GameGrid


Square


Square



But since a few comments have been posted and I've done a little research, I've figured out that I think I'll either just keep it as it is, and pass in the desired GameGrid instance into Square, or I'll try to work with a custom observer/observable like pattern where the updated argument (normally from notifyObservers(Object arg)) will be a custom Class that I can use to hold all the required information needed.


GameGrid


Square


notifyObservers(Object arg)






make the grid observe the square - or pass a listener into it, which then can be called, eg. when tapping it. your question does not indicate, why you would want to pass the whole grid into the constructor of a square.

– Martin Zeitler
Sep 17 '18 at 3:45







Which GrameGrid method do you want to call from Square? Can you not pass GrameGrid object in the Square method?

– curlyBraces
Sep 17 '18 at 3:46



GrameGrid


Square


GrameGrid


Square






Potentially relevant: this, this

– ricky3350
Sep 17 '18 at 4:26






You are trying to access some behavior that is part of GameGrid to the Square. What is that behavior? What is it you are trying to do? I think you should think in terms what needs to be done rather than what pattern. One cannot determine a pattern or just decide without knowing what the issue is. Please explain little more what is it that you want the Square class do or needs that which GameGrid is already doing?

– prasad_
Sep 17 '18 at 5:29


GameGrid


Square


Square


GameGrid




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế