views:

149

answers:

3

The problem to model is this:

A hierarchy of levels within an Army, starting with the national army in whole, through field armies, subunits, and eventually the individual men. Each level may involve links to one or more other classes such as General or Officer or whatever. The units within say a field army need to be able to communicate with each other, especially for purposes of modeling morale, cohesion, etc, as well as with those of any enemy field army (e.g. a unit routing in my army affects the enemy morale positively). Furthermore, each unit needs to communicate with those above and below it in the hierarchy (for obvious purposes).

I was thinking of having the links in the physical hierarchy represented by actual pointers (possibly bilateral) in each of these entities' classes (e.g. army* in each unit and unit* or a whole collection of them in each army) and then making use of the observer design pattern to implement any communication in other cases (such as the case I mentioned above).

However, being no expert in design patterns or programming for that matter I do not know whether there is any other more efficient manner to do this. Any help would be greatly appreciated.

+3  A: 

Sounds like "Small Boy With A Pattern" syndrome. You're looking for a pattern instead of thinking about your problem.

The natural data structure for a hierarchy is a tree. I'd start with that.

If the requirement is that every unit in the tree must communicate with all others, I'd say that Observer is not for you. Every unit would have to be register with all the others. You'll have an N-squared firestorm of messages every time an event was fired.

Mediator might be better. Units would send events to the mediator, allowing consumers to register their interest in receiving a particular kind of message. Producers and consumers only know about the mediator, not each other. Loose coupling is your friend.

duffymo
Well, I've already 'solved' the problem and created working code using pointers, and lots of them. Now I wanted to apply a better OO/design pattern approach to it and see if it can be done more elegantly or efficiently. I think it can, hence my question. Thanks for your help as well.
Kristian D'Amato
@Kristian - nobody can tell that from your original post. I just re-read it, and it's not clear whether this is a hypothetical problem or a description of an existing solution.
duffymo
Yes that may be but it's beside the point whether a solution exists or not. I'm sure other people have modeled similar problems and there may be better solutions than mine.
Kristian D'Amato
+3  A: 

There is a model/design pattern for communicating events between disparate entities that may not know of eachothers existence before the communication happens. The pattern is called 'Publish/Subscribe'.

Each entity sends events it wants to publish to a broker and tells the broker about what kinds of events it would be interested in. The broker handles making sure the subscribing entities learn of events they find interesting that are published.

This is like the Observer pattern, but in the Observer pattern each interested entity subscribes individually to each entity it wants events from. I think this could result in a lot of overhead because that requires everybody to care about creation and destruction of things.

Anyway, there is a nice Wikipedia article on Publish/Subscribe.

I would use the Composite pattern (which basically means a tree of some form) for the individual armies. And possibly Observer for relationships up and down the hierarchy or with siblings. But Observer requires too much registering and unregistering for it to be workable in the general case.

Omnifarious
+1  A: 

For modeling the structure, this looks like classic application of the Composite pattern. Then you can use Visitor or Interpreter for modeling the operations on sub-units.

Nikolai N Fetissov
Thanks, I will have a look at those patterns (Still very green at them).
Kristian D'Amato