Good code architecture for this problem?

I am developing a space shooter game with custom ships. You can increase the strength of any number of ship properties through a pair of radar maps *. Inside, I represent each ship as a class of the SpaceObject subclass that contains ShipInfo , which describes the various properties of this ship.

I want to develop a relatively simple API that allows me to load a block of relative strength (from minimum to maximum, which allows a radar chart) for all properties of the vessel (some of which simplify the basic actual set of properties) and return the ShipInfo class, which I can assign to the PlayerShip class (that is, an object that was created as a game ship).

I can develop the code to do the conversion between simplified and actual properties myself, but I would like to get some recommendations on what architecture can be provided to minimize pain when interacting with this translator code (that is, without methods with 5+ arguments or whatever then another nonsense). Does anyone have any idea?

* = not actually implemented, but this plan.

+6
c # oop architecture
source share
4 answers

How about a builder template? You can have the static FillDefaults method in your ShipInfo class, and then assign each ShipInfo property using the instance method, which returns the instance you are working with, for example:

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

Inside ShipInfo, it looks something like this:

 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; } 
+5
source share

I would say that the Facade template is ideal for this kind of problem. If you have 5 arguments in your methods, consider encapsulating at least some of them in a new type.

+5
source share

It looks like you want to set some properties, but not others, but not in a certain order of importance, so that you can define overloads with an extra number of arguments.

You can implement a constructor with the minimum required values ​​that sets default values ​​for another, and then use the 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 }; 
+1
source share

For me it looks like a decorator template.

0
source share

All Articles