• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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)?
 
Marshal
Posts: 25594
69
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
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic