Re: Interface complexity problem in game
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Sun, 25 Sep 2005 16:46:47 GMT
Responding to Guild...
Two questions from beginners about constructing an RPG game in one day. Coincidence? Tell your instructor that a computer game is too big a project for a homework problem.
I'm trying to design a game carefully and deliberately, using every abstraction trick that I can to make it extremely maintainable.
The active entities of my game are represented as objects of a common class, subclassed and composed for varying behaviour. I let the objects control their behaviour when I call a method on the object called act(world). I can't define what this method will do, because the point of it is that it will vary wildly from object to object.
This is probably a Bad Idea except in very specific situations. See my response in the "RPG game in UML" thread.
I pass in a 'world' object that gives access to the things that may be needed by the entity to do something interesting. And that is where the problem is: My current strategy is going to result in the class of the world object having perhaps hundreds of methods, everything that any entity in my game might want to do to the world.
This is an Even Worse Idea for exactly the reasons you identify. (One exception below.)
Generally collaborations in OO applications should be on a peer-to-peer basis. One object sends a message directly to another object that needs to respond. To address the message (i.e., get to the right responding object) one navigates relationships from the Class Model. Using relationship navigation is one of the most important aspects of OO development because it is the main tool for managing state variables (i.e., restricting access to attributes). See the category on Relationships in my blog for more on this.
I know that it is a bad thing to have such a huge interface. I don't know exactly what the methods will be at this stage in design, but I want to adjust my design to allow me to add complexities to the 'world' without adding nearly as many complexities to the interface.
I do not have a clear idea of the domain that I am working with and that's an unfortunate but unavoidable reality. However, I can say that the overwhelming majority of methods on world objects will be of the form: world.doSomething(parameter,position), where an action is taken upon a spot in the world nearby to the entity such as slaying a monster, opening a door, cutting down a tree, etc. with a different parameter or parameters for each kind of action.
This paragraph suggests that 'world' represents another, largely undefined subsystem. In that case it is a subsystem interface. Using the GoF Facade pattern is a common way to implement subsystem interfaces. While that Facade is a single class with a large number of interface methods, it does not implement the functionality. It is simply a buffer class that re-dispatches the messages to the objects that implement the subsystem. So in that case a large number of methods is acceptable. In fact, it is highly desirable because it hides the implementation of the subsystem.
What you do NOT want to do is provide a mega-class /within/ a subsystem. Within a subsystem objects have carnal knowledge on one another and talk to one another directly. As you noticed, such god classes are big and complex. Consequently their cohesion is abysmal and they are a pain to maintain. Whenever your classes that implement a subsystem become so large that cohesion is sacrificed, it is time to delegate responsibilities to other classes.
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- Follow-Ups:
- Re: Interface complexity problem in game
- From: Brendan Guild
- Re: Interface complexity problem in game
- References:
- Interface complexity problem in game
- From: Brendan Guild
- Interface complexity problem in game
- Prev by Date: Re: A question about substitution principle
- Next by Date: Re: substitution principle and contract
- Previous by thread: Re: Interface complexity problem in game
- Next by thread: Re: Interface complexity problem in game
- Index(es):
Relevant Pages
|