Design Mode: modifier Mode

Source: Internet
Author: User

 

Today, we will continue our journey of designing patterns to bring you the decorating patterns and international practices. Let's look at the definition first.

Decorator mode: to expand a function, the decorator provides a more flexible alternative than integration, dynamically attaching the responsibility to the object.

First, let's briefly describe the role of the modifier mode. When we have designed a class, we need to add some auxiliary functions to this class and do not want to change the code of this class, at this time, it is time for the decorator mode to show the power. This also embodies the principle that classes should be open to extensions and closed to modifications.

Next, let's start with the question. When I watch a movie there today, I suddenly think of the storm in the game field when I was young. Haha, the following describes the decorator mode with the game as the background. Brothers who have played games should know that each role in the game contains weapons, shoes, wristbands, rings, various rubs, sapphire, and yellow gems.

The following requirement begins: design the game's equipment system. The basic requirement is to calculate the attack power and description of each type of equipment after a variety of gems are embedded:

Specific requirements:

1. Weapons (attack power 20), rings (attack power 5), wristbands (attack power 5), and shoes (attack power 5)

2. Sapphire (attack strength 5/X), yellow gemstone (attack strength 10/X), Ruby (Attack Strength 15/X)

3. Three pieces of each equipment can be embedded at will

 

Well, the demand has been introduced. Of course, don't let me talk about my design. Where can Nima shoes attack the power? It can also hit people at critical moments. The preliminary assumption below is that we may design this design out of years of object-oriented experience:

If you design it like this, I rely on it. In this case, you write hundreds of classes and add two gems at will. Haha, have you heard of exponential growth? Are you ready to work overtime.

Maybe you will design it like this: Write a super class, then set various gems in it, and then, where the attack power is calculated, which of the following kinds of gems are available for the powerful If? Congratulations, the amount of code is not very large, but IF you want to add a weapon, how many more IF statements do you have to write.

The above describes some possible designs, which are not very good. Let's take a look at how to integrate the modifier model:

First, the supercategory of equipment

 

Package com. zhy. pattern. decorator;/*** equipment interface ** @ author zhy **/public interface IEquip {/*** computing attack power ** @ return */public int caculateAttack (); /*** device description ** @ return */public String description ();}

Then they are weapons, rings, wristbands, and shoes.

 

 

Package com. zhy. pattern. decorator;/*** weapon * attack power 20 * @ author zhy **/public class ArmEquip implements IEquip {@ Overridepublic int caculateAttack () {return 20 ;}@ Overridepublic String description () {return dragon removal knife ;}}

Package com. zhy. pattern. decorator;/*** ring * attack strength 5 * @ author zhy **/public class RingEquip implements IEquip {@ Overridepublic int caculateAttack () {return 5 ;}@ Overridepublic String description () {return promotion ring ;}}

Package com. zhy. pattern. decorator;/*** wrist guard * attack strength 5 * @ author zhy **/public class WristEquip implements IEquip {@ Overridepublic int caculateAttack () {return 5 ;}@ Overridepublic String description () {return mujial wrist guard ;}}

Package com. zhy. pattern. decorator;/*** ShoeEquip attack strength 5 * @ author zhy **/public class ShoeEquip implements IEquip {@ Overridepublic int caculateAttack () {return 5 ;}@ Overridepublic String description () {return war boots ;}}

Next, of course, it's an ornament and a gem. First, it's super-class.

 

 

Package com. zhy. pattern. decorator;/*** ornament interface * @ author zhy **/public interface IEquipDecorator extends IEquip {}

Down sapphire, yellow gemstone, Ruby

 

 

Package com. zhy. pattern. decorator; /*** sapphire ornament * each attack power + 5 * @ author zhy **/public class BlueGemDecorator implements IEquipDecorator {/*** each ornament maintains one device */private IEquip equip; public BlueGemDecorator (IEquip equip) {this. equip = equip;} @ Overridepublic int caculateAttack () {return 5 + equip. caculateAttack () ;}@ Overridepublic String description () {return equip. description () ++ sapphire ;}}

Package com. zhy. pattern. decorator; /* the yellow gemstone ornament * each attack power + 10 * @ author zhy **/public class YellowGemDecorator implements IEquipDecorator {/*** each ornament maintains one device */private IEquip equip; public YellowGemDecorator (IEquip equip) {this. equip = equip;} @ Overridepublic int caculateAttack () {return 10 + equip. caculateAttack () ;}@ Overridepublic String description () {return equip. description () ++ gemstone ;}}

Package com. zhy. pattern. decorator; /*** Ruby ornament each attack power + 15 *** @ author zhy **/public class RedGemDecorator implements IEquipDecorator {/*** each ornament maintains one device */private IEquip equip; public RedGemDecorator (IEquip equip) {this. equip = equip;} @ Overridepublic int caculateAttack () {return 15 + equip. caculateAttack () ;}@ Overridepublic String description () {return equip. description () ++ Ruby ;}}

Now, we have implemented the required functions. Is every class clear and simple? Let's take a look at the test below:

 

 

Package com. zhy. pattern. decorator; public class Test {public static void main (String [] args) {// a boot System with two rubs and One sapphire. out. println (a boot with two rubs and One sapphire); IEquip equip = new RedGemDecorator (new ShoeEquip (); System. out. println (attack power: + equip. caculateAttack (); System. out. println (Description: + equip. description (); System. out. println (-------); // a System containing one Ruby and One sapphire. out. println (a weapon with one Ruby, One sapphire, and one yellow gemstone embedded); equip = new RedGemDecorator (new BlueGemDecorator (new YellowGemDecorator (new ArmEquip (); System. out. println (attack power: + equip. caculateAttack (); System. out. println (Description: + equip. description (); System. out. println (-------);}}

Output:

 

 

Attack Strength of one boot with two rubs and one Sapphire: 40 Description: mujiam boot + sapphire + Ruby ------- One inlaid Ruby and One sapphire, attack Power of a yellow gemstone weapon: 50 Description: Dragon removal knife + yellow gemstone + sapphire + Ruby -------

Like or dislike. If you need just a few more equipment and several kinds of gems, we can add them as soon as possible, and then work happily.

 

 

Well, congratulations, you have learned another design pattern, the modifier pattern.

I don't need to talk about the definition based on the examples.

 

Java APIs also have the modifier mode. If you are a beginner in Java, you must remember the various streams in Java. It is very painful, but when you understand

After your design, you will feel much clearer.

Regard InputStream as our IEquip and FilterInputStream as our IEquipDecorator. Is it almost the same as our design ~

 

Now, is programming fun ~ Yes, you may leave a comment and give a thumbs up ~

 

 

Download source code

 

 

 

 

 

 

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.