When creating classes, we need to ensure that the class prohibits any breaking modifications by needing to change internal code. We say that such a class is closed. If we need to change it somehow, we can do so by extending the class. This extensibility is where we say that the class is open for extensions.
You will create a class that determines the skills of a trooper by looking at the class of trooper. We will show you the way many developers create such a class and the way it can be created using the open/closed principle.
Create a class called
StarTrooper
:public class StarTrooper { }
To this class, add an enumerator called
TrooperClass
to identify the type of trooper we want to return the skills of. Also, create aList<string>
variable to contain the skills of the specific trooper class. Finally, create a method calledGetSkills
that returns the specific set of skills for the given trooper class.The class is quite straightforward...