A: 

Not quite an answer, but to me this fairly screams "properties pattern". There's a well-known yegge rant about this, I think it'll offer you some decent pointers.

http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html

callingshotgun
A: 

This looks like the classical OOP polymorphism/visitor dilemma. However, your requirements make it easier.

Basically, I would create a base class Ship that all concrete Ships derive from. This class would have methods:

class Ship
{
  void encounterBlackHole() {}
  void encounterNebula() {}
  ... etc. ...
};

with empty default bodies. When you add a new Phenomenon, you just add new method with empty body. (The methods may have arguments, like coordinates or weight of the black hole etc.)

For the Effects and their interaction - I think you should add more information about how you want it, eg. are the interactions rare or usual, are they cumulative in some sort, are they confined to a limited set of variables they control ...

jpalecek
Thanks for the feedback. I clarified this with "No information other than the set of Effects acting on a Ship is needed to resolve what the final result should be."
John Feminella
+1  A: 

An interesting potential option would be to use a variant of the Visitor Pattern.

Judith Bishop and R. Nigel Horspool wrote a paper about design pattern efficiency in which they explained various variants on the classic visitor pattern using C# 3 features.

In particular, I would take a look at how they work with delegates to handle the visitor pattern. Using a list or stack of delegates could potentally give you an interesting way to handle multiple effects from multiple objects, and be much easier to extend either side of the class hierarchy (add ships or add effects) without huge breaking code changes.

Reed Copsey
Seems like a good design, perhaps better than my anser
Martin
A: 

Sounds like a classic multiple dispatch problem to me.

A: 

Interesting question

In a way or an other, A ship will have to know what phenomena can affect it and a phenomena which effects it has on which ship.

This could be stored in an xml file parsed at run-time.

Maybe you could use the Decorator pattern to calculate the effects. You generate various phenomenas at run-time.

Suppose your ships implement an Interface IShip and everywhere in your code, you use IShips.

Now, suppose all your phenomena also implement the Interface IShip (required by the decorator design pattern).

IShip myShip = myShip.AddPhenomena(PhenomenaGeneratedByAFactoryForThisShip);

In the phenomena, you wrap the methods from the original ship, so you can perform modification to properties and all.

Also, if you use the strategy pattern you can generate any kind of phenomena you'd like.

Removing a phenomena, can be used by walking the pile of decorated ships you have, and rewrapping it, so I see no problem there.

As for the synergies, I think I would use a slightly modified phenomena, which works only if the target ship has all the phenomenas on itself.

Martin
A: 

either every Ship will have to know how to handle every Phenomenon, or every Phenomenon will have to know about every Ship.

I think this is the key to your problem and it's true if every ship-phenomena interaction is unique. In the table you created, that appears to be the case, so for n ships and m phenomena, you need to specify n*m interaction. You are right to smell a maintenance issue.

The only way out is to make ship-phenomena interactions not as unique by making the effect of phenomena depend on the properties of the ship. For example, we could say only ships built with strange-alumina will survive a black hole.

But having only one property doesn't buy you anything, you could've just as easily specified which ships are affected by black holes. The key is that multiple properties exhibit the same combinatorial explosion.

So ships built with strange-alumina will survive black holes, and ships built without can go faster. 1 Property allows you to specify 2 things (1 bit = 2 possibilities). Ships built with corbite engines will go faster in a warp zone. Ships with both corbite engines and strange-alumina will gain 50% shield in a nebula field, etc.

The addition of properties to ships allows you to avoid specifying how every phenomena interacts with every ship, and yet still have every phenomena and every ship exhibit an appropriate behavior.

If there are M ships, then you only need log2(M) properties to give every ship a unique behavior.

Paul
I think this could be an idea, but won't it limit the possibilities somehow ? What if a ship needs to have some exceptions, special properties ? I am also not sure it respects the original spec, by introducing new properties. Adapt your code to the spec, not the other way around.
Martin
A: 

I think the answer of a problem is depend on how good the question ask.

I think the way to design is depend on what the question is(or the question will goes in the future)

you give a table, then I think the solution is maintain a table and query it.

python code here:(not tested and just shows for a example)

class Ship():
     def __init__(self,type):
         self.type=type
     def encounterPhenomena(self,type): # let Phenomena to process ship
         p = Phenomena(type)
         p.process(self)

class Phenomena():
     processTable = {}
     def __init__(self,type):
         self.type=type
     def process(self,ship):
         try:
             self.processTable[self.type](ship.type) #query the internal table to process ship
         except:
             pass #if Phenomena don't know this ship type then no process..
     def addType(type,processMethod):
         processTable[type]=processMethod #add new Phenomena, and add processMethod

def run():
    A = Ship(type = 'RedShip')
    A.encounterPhenomena(type='GravityWell');

If process method changed, simply modify the process method in the Phenomena class.

If you think ship need to know how to process Phenomena, then change process method into the ship class.

or you think there are other things not only Phenomena need to change status of ship(like other ships, shoal rock), you need to maintain a process table in ship class and make Phenomena one of it,

speak again, how to design is depend on the question its self.

linjunhalida