Fantasy RPG (item forging and decorator mode)

Source: Internet
Author: User
Tags inheritance

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.