This is quite rigid - a soldier, by the above definition, must be able to Attack Ranged, and Attack non ranged. And, by definition,
This means that a Soldier is always able to attack and defend. To give me the full range of flexibility, I'd end up needing a huge number of interfaces. This didn't feel right initially, and as I plan on adding a large number of Units, it would prove impossible to take forward. It also proves inflexibile - how do we apply changes to Units? Can a non-ranged Unit be upgraded to ranged attack? Can a non-attack Unit (a peasant, for example) be given an attack upgrade? Not with the current design.
Ayende suggested using the State Pattern, something which I rather ashamedly have never used before. On initial reading, it seems ideal - it keeps the Unit behaviour self contained, meaning the Engine (which would have been a God class by every definition, had I gone down my initial route) doesn't need to know much, if anything, about Unit behaviour.
I had initially thought of using the Strategy Pattern, but this gives the problem of requiring external control. In a Domain sense, the Unit, and nothing else, should know whether it can attack something 25 feet away. This leads to the State Pattern - it allows the Unit to contain all the necessary behaviour without requiring a strategy to be passed in. The Unit changes it's own state in response to several thing such as what it is doing and what upgrades it has.
I have some research and prototyping to do before I implement this pattern, but I'll aim to blog again soon on this topic with an updated model and some code samples.
0 comments:
Post a Comment