views:

213

answers:

4

I am developing a space shooter game with customizable ships. You can increase the strength of any number of properties of the ship via a pair of radar charts*. Internally, i represent each ship as a subclassed SpaceObject class, which holds a ShipInfo that describes various properties of that ship.

I want to develop a relatively simple API that lets me feed in a block of relative strengths (from minimum to maximum of what the radar chart allows) for all of the ship properties (some of which are simplifications of the underlying actual set of properties) and get back a ShipInfo class i can give to a PlayerShip class (that is the object that is instantiated to be a player ship).

I can develop the code to do the transformations between simplified and actual properties myself, but i would like some recommendations as to what sort of architecture to provide to minimize the pain of interacting with this translator code (i.e. no methods with 5+ arguments or somesuch other nonsense). Does anyone have any ideas?

*=not actually implemented yet, but that's the plan.

+5  A: 

I would say the Facade pattern is perfect for that kind of problem. If you have 5+ arguments on your methods, consider encapsulating at least part of them in a new type.

Sylvestre Equy
I'm not that familiar with the Facade pattern; mind pointing me in the direction of an explanation? (even a link to wikipedia or stackoverflow will do)
RCIX
(+1, btw) heh: http://www.google.com/search?q=facade+pattern
ANeves
http://dofactory.com/Patterns/PatternFacade.aspx
Sylvestre Equy
+1  A: 

Seems like you want to set some properties but not the others, but not in a particular order of importance so that you could define overloads with incrementally more arguments.

You could implement a constructor with minimum required values that sets default values for the other, and then use object initializer to set the remaining relevant values:

// Didn't set properties 2 3 and 6, only set the ones needed in this case.
SpaceObject ship = new SpaceObject(someRequiredValue) {
  Property1 = 50,
  Property4 = Game.Settings.Ships.Armor.Strong,
  Property5 = new PropertySet1{
    Prop51 = "Enterprise",
    Prop53 = true,
    Prop57 = false
};
ANeves
not bad, though i don't think that would work as i have property block A which i want to translate to property block B and i'm looking for an architecture to minimize the pain of interacting with the code that does the translation :)
RCIX
You could apply the same principle to the property block. But the Builder pattern looks good for what you want.
ANeves
A: 

To me this looks like a case for the decorator pattern.

Filburt
+5  A: 

What about the Builder pattern? You could have a static FillDefaults method on your ShipInfo class and then assign each property of the ShipInfo via an instance method that returns the instance that you're working with, like this:

ShipInfo.FillDefaults().CalculateSomething(50).AssignName("Testing...").RelativeFiringPower(10).ApplyTo(myShip);

Within ShipInfo, this would look something like:

public static ShipInfo FillDefaults()
{
    ShipInfo newInstance = ...;
    // Do some default setup here
    return newInstance;
}

public ShipInfo CalculateSomething(int basis)
{
    // Do some calculation
    // Assign some values internally
    return this;
}

// Keep following this pattern of methods
public void ApplyTo(SpaceObject obj)
{
    // Some checks here if you want
    obj.ShipInfo = this;
}
jasonh
I think I need to go back through the design patterns book and actually study it rather than flip through it. I totally missed this solution when reading the question and thinking of how I would solve it, and I love the elegance! I even use JQuery on a regular basis and didn't make the connection, and EVERYTHING in JQuery uses the Builder pattern...
Ricket