Jump to content

  • Log In with Google      Sign In   
  • Create Account


Are lots of "Pass-Through" methods bad/poor design?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Plethora   Members   -  Reputation: 679

Like
0Likes
Like

Posted 26 April 2013 - 01:34 PM

In my ongoing efforts to keep a fairly large code-base manageable despite the fact that my current project is fairly ambitious for my knowledge (but I'm determined dammit!), I've run into a phenomena that keeps popping up and I'm unsure if its a bad thing, a necessary evil, or maybe its just fine and I'm worrying for nothing.

 

In a nutshell my program works like this.

  • I have a Main_Controller class that owns and runs the main game loop. 
  • The Main_Controller contains these objects, that are relevent to this conversation:
  • A Graphics object, an std::stack of Controller objects, and a Gui_Manager object.
  • Each Controller object basically defines a given screen (World Map, Town Screen, Battle Screen, etc) and handles input for that screen.  The Controller on the top of the stack is considered the "active" controller and thus handles all input while it is active.
  • Each Controller also contains some number of data driven objects that make up that screen.  So the Battle_Controller contains a Map, which in turn contains some Enemies, some Tiles, and so on.

There is obviously other stuff going on, but that's the basic idea.  My question is related to the ever expanding collection of Data in my game and the fact that I have a kind of hierarchy (not an inheritence hierarchy, mind you) of data (like in the above, the Map has a bunch of data made up of enemies, which have their own data made up of..., etc.)

 

Now, lets say I want to display the stats of a given enemy in a window created by my Gui_Manager.  Well, the Enemy class contains a Stat_Pack, and a getStats() method which returns the Stat_Pack in question.  Then Map contains a getStats() method as well, and then so does the Battle_Controller And finally, Gui_Manager has a displayStats(Stat_Pack stats) method which gets called by the Main_Controller

 

Obviously, the data in question has to be retrieved somehow, but I just wonder if going through a chain of 5+ function calls just to get some small amount of data from one place to another in my code is really the best way to do it.  Obviously I could give, in this case,  Gui_Manager direct access to some of this data if I wanted to, but that seems like it would completely break the modular design I've worked pretty hard to maintain.

 

The other solution I thought up seems more like a band-aid than anything else, but I could perhaps create a "middle" type class that's meant specifically to transfer data from one "branch" of code to another.  But then I think the problem is, what do I give that class access to?  Everything?  That doesn't seem like a good idea...

 

So, what do you folks think?


I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

 

http://infinityelephant.wordpress.com


Sponsor:

#2 frob   Moderators   -  Reputation: 18870

Like
0Likes
Like

Posted 26 April 2013 - 04:24 PM

I see two issues, not just one.

 

The first one I see is that it looks like you are failing to build abstract interfaces.

 

Are you implementing the Dependancy Inversion principle correctly?  That is, are you writing systems that depend on an abstract interface rather than writing systems that depend on specific concrete types.

 

 

 

The second one is inappropriate intimacy from your Gui_Manager.  

 

If you want to display the stats of one enemy, you should have that one enemy selected.  No other objects should be involved in the transaction.  Maybe it was from a mouse event or mouse hover, maybe it was from a click, but whatever triggered the event it should ultimately resolve to the Gui_Manager knowing about only one object.  

 

It makes sense for a mouse-picking code to traverse the world to see what is being pointed to.  But once it is selected nothing else should need to traverse the tree.


Check out my personal indie blog at bryanwagstaff.com.

#3 Plethora   Members   -  Reputation: 679

Like
0Likes
Like

Posted 26 April 2013 - 05:29 PM

On your first point, I have some reading to do and will be reimplementing some things, thank you for the help and the link!  I am absolutely writing systems that depend on concrete types and will need to be making some modifications to my program going forward.  As I said, this is a big project for me, and a big part of my goal is learning how to manage something a bit bigger than what I've done in the past.  I don't at all mind going back to refactor stuff like this, so I really do appreciate the lesson.  :)

 

I'd like to talk about the second point a little bit though.  I think I get what you're saying, but I just want to clarify a little bit.  So with the current example, when the mouse is clicked it happens as you said.  The coordinates are gotten and then used to determine what is clicked on just as you say.

 

Now, the part where I get a little confused is this.  In my current implementation, Gui_Manager knows absolutely nothing about anything really.  All it knows how to do is create a set of predefined windows when it is told to from outside.  It is given only the information it needs to do so.  In most cases, it doesn't even know about specific types of objects.  In the example we're discussing, Gui_Manager doesn't even know what an Enemy is.  All it knows is how to display a window which displays to the user a set of statistics given to it in the form of a Stat_Pack object (which is a simple struct containing a bunch of integers and one std::string). 

 

What I hear you saying is that once the program determines what has been clicked on, then I should have functionality built into Gui_Manager where it knows what should be displayed.  Like for example, maybe Gui_Manager contains a pointer to some object that will be changed by the event handling code.  Even as I am typing this, I see the logic in that and am working out how I will implement it.  But, to make sure, is that essentially what you mean?


I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

 

http://infinityelephant.wordpress.com


#4 frob   Moderators   -  Reputation: 18870

Like
0Likes
Like

Posted 27 April 2013 - 09:48 PM

Yes.

Keeping the two together, any object that can be displayed by the gui's stats system should implement a common interface. Perhaps call it IHasStatsPack.

On many of our studio's projects we have established the pattern of interfaces named IHasFoo or IFooable.

The interface would just be something simple:

class IHasStatsPack {
Stats_Pack* getStats() = 0;
}

Then your gui class can accept an object of type IHasStatsPack.

That's really all the gui cares about. The gui doesn't care if the object is an enemy or a player or some other game object. All it cares about is that the object has the getStats() function that returns a valid stats structure for display.

Now anyone who creates game objects can use your gui to display any object they want. It may happen that the game will only ever display stats for enemy units. But maybe not. If someday a future programmer needs to build some helper class somewhere and wants to pass that to your gui, they just need to implement the IHasStatsPack interface.

Everybody wins with this kind of design.
Check out my personal indie blog at bryanwagstaff.com.

#5 jellyfishchris   Members   -  Reputation: 300

Like
0Likes
Like

Posted 29 April 2013 - 08:04 PM

DIP.jpg






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS