Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class taking as parameter object of another class...chicken or the egg stuff?  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm writing a little game and I have some issues with designing some classes. For instance, I need a class Board, containing some Cells. These cells will have some methods (and they are of different types, so I'll create subclasses)


Here is what I get in short :




My question is the following : is this design correct ? To me, it does not seems very logical to have an object of type Cell as a parameter of another class, i.e. Board. But at the same time, I don't know how to do it. Isn't it a chicken or the egg dilemma ?! Hope you make myself clear !
Thanks for your insights.
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have written seems logical to me. There a few point, though.

Cell is not a subclass; it's just a class. A subclass is when you extend another class.

This code is a little troubling:

It will work fine if the array or arrays is rectangular, but it will fail if it's ragged. Better would be:
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback...I edit some typos btw.
This is a general question, but how can I test thoses classes (I mean separately from the other classes)?
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:To me, it does not seems very logical to have an object of type Cell as a parameter of another class, i.e. Board.


Does it seem reasonable to pass an object of type String as a parameter of some other class? Of course it does, you've seen it done many times. So why doesn't it seem right to pass objects of some other type?

As for your actual example, I don't see why a Board object would be passed its Cells from outside. Seems to me that it would be managing its own Cells -- but having said that, there's nothing wrong with your design considering it only as Java code.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.
As for your actual example, I don't see why a Board object would be passed its Cells from outside. Seems to me that it would be managing its own Cells

Could you be more specific ? It looks like I have overlooked something important...
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!