Using inheritance to extend
We first think that there should be a base class weapon, which is for all kinds of weapons to inherit, such as Sword, Axe, Bow. The Description field represents the description of the weapon, such as "One-hand Light Sword", and the Damage () method is used to obtain the damage of the weapon, getdescription the instructions for obtaining the weapon. Without considering gems, we get the following design:
Now we consider how to create a weapon inlaid with gems. We first consider that we can use inheritance to implement such a design, it turns out that if we need to define all the gem-Inlaid Swords (sword), we need 3+6+7 = 16 classes (Note: Three items hole, each hole has blue, red, green three choice, can be two or three holes the same color), If we give the sword inlaid with two blue and one Ruby as Blue2redsword, give the three colors different swords named Blueredgreensword, and the rest of the analogy. So, we're going to get this huge class system (which only draws the parts):
And this is just the beginning, if we need to add a gem, such as white, it can attach the curse effect, or we need to add a hole to the weapon, then our class number will rapidly from more than 10 to dozens of.
We found the problem with inheritance: A large number of classes will be created when using inheritance. In addition, using inheritance also means that we need to instantiate a particular subclass to get the functionality we need (the method), which is determined at compile time (compile time), and the client of the class cannot control when (run time) changes as needed unless another subclass is instantiated.