Isn't there some general technique that's used in the case where different objects needs to access eachother? |
Program design is tricky. It's much trickier than actually writing the code.
There is no general rule for a good design. The needs of every program just varies too much.
The best advice/rules I can give is:
1) Work out what you want your classes to be. Classes should represent clear "things", should be logical and intuitive. For example you might want a Player class, Enemy class, maybe a Map class, etc, etc.
2) Work out how the classes work together. For example a Player would need to communicate with Map in order to be able to interact with the environment (no walking through walls, etc)
3) Try to keep interactions 1-way whenever possible. Player may need to know about Map, but Map shouldn't need to know about Player. Two-way communication between classes is a
huge source of maintanance issues -- it often leads to situations where if you have to change one thing over here, you have to change something else over there (and
if when you forget, things fall apart)
4) Abstract to recycle code where logical/practical. For example Enemies and Players will both need to interact with the Map, but it doesn't make sense to write separate interaction code for both. Maybe make a MapObject class and derive Player and Enemy from it. Then MapObject can work with Map, then players and enemies don't have to worry about it.
5)
Design first, code later. Don't dive right into code. Sit down and think about all of the above. Get an idea of how it's going to work before you start writing a single line. I can say from experience that "slap it together as you go" programs are doomed to either wretched obfuscation, or even complete failure.