Today continue to design the journey of the model, to bring you the Decorator model, international practice, first look at the definition.
Decorator mode: To extend functionality, decorators provide a more resilient alternative to integration, dynamically attaching responsibility to objects.
A simple description of where the decorator pattern works, when we have designed a class, we need to add some auxiliary functionality to this class, and do not want to change the code of this class, this is when the decorator mode Xiong Wei. There is also a principle: classes should be open to extensions and closed to modifications.
The following into the topic, today in that movie, suddenly think of young in the game on the bloody ah, haha, the following game as the background to introduce the decorator mode. The brothers who played the game should know that each character in the game has weapons, shoes, wrist guards, rings, and a variety of rubies, sapphires, yellow gems and so on.
The following requirements begin: The design of the game's equipment system, the basic requirements, to be able to calculate the damage and description of each kind of equipment after the mosaic of various gems:
Specific requirements:
1, weapon (attack 20), Ring (attack 5), wrist guard (attack 5), shoe (attack 5)
2, Sapphire (attack 5/), Lemon topaz (attack 10/capsules), Ruby (attack 15/capsules)
3, each equipment can be arbitrarily set 3 capsules
Well, the requirements are introduced, of course, do not spit groove my design, where the attack of the shoes, the key moment can also hit people. Let's start with a tentative idea that, for years of object-oriented experience, we might be designing:
If that's what you're designed for, I'm on it. You write hundreds of classes, add two gems, haha, exponential growth heard, ready to work overtime.
Perhaps you will also design: Write a super class, and then the various set of gems, and then in the calculation of the attack, the hard if there are several gems, congratulations, the code is not very large, but add a weapon, you have to write more than how many if it.
The above describes some of the possibilities of the design, are not very good, below to see how to integrate the Decorator mode:
First is the super class of equipment
Package Com.zhy.pattern.decorator; /* * * Equipment interface * * @author zhy * * */publicinterface iequip{ / * * * Calculate Damage * * @return */public int Caculateattack (); /* * Description of the equipment * * @return * /Public String description ( );}
Then weapons, rings, wrist guards, shoes.
Package Com.zhy.pattern.decorator;/** Weapon * Attack damage * @author Zhy **/ Public classArmequip implements iequip{@Override Public intCaculateattack () {return -; } @Override PublicString Description () {return "Dragon Slayer Knife"; }}
Package Com.zhy.pattern.decorator;/** * Ring * Attack 5 * @author Zhy **/ Public classRingequip implements iequip{@Override Public intCaculateattack () {return 5; } @Override PublicString Description () {return "holy war Ring"; }}
Package Com.zhy.pattern.decorator;/** * Wrist protection * Attack 5 * @author Zhy **/ Public classWristequip implements iequip{@Override Public intCaculateattack () {return 5; } @Override PublicString Description () {return "Holy War wrist guard"; }}
Package Com.zhy.pattern.decorator;/** * Shoes * Attack 5 * @author Zhy **/ Public classShoeequip implements iequip{@Override Public intCaculateattack () {return 5; } @Override PublicString Description () {return "Holy war Boots"; }}
Next of course is the ornament, the jewel, the first super class
Package Com.zhy.pattern.decorator; /* */Publicinterface iequipdecorator extends iequip{ }
Down sapphire, yellow gem, ruby
Package Com.zhy.pattern.decorator;/** Sapphire Ornament * +5 * damage per attack * @author Zhy **/ Public classBluegemdecorator implements iequipdecorator{/** Each accessory maintains a gear*/ PrivateIequip Equip; PublicBluegemdecorator (Iequip equip) { This. Equip =Equip; } @Override Public intCaculateattack () {return 5+Equip.caculateattack (); } @Override PublicString Description () {returnEquip.description () +"+ Sapphire"; }}
Package Com.zhy.pattern.decorator;/** Yellow GEM ornament * +10 * damage per attack * @author Zhy **/ Public classYellowgemdecorator implements iequipdecorator{/** Each accessory maintains a gear*/ PrivateIequip Equip; PublicYellowgemdecorator (Iequip equip) { This. Equip =Equip; } @Override Public intCaculateattack () {return Ten+Equip.caculateattack (); } @Override PublicString Description () {returnEquip.description () +"+ Yellow Gems"; }}
Package Com.zhy.pattern.decorator;/** Ruby Ornament per attack +15 * * @author Zhy **/ Public classRedgemdecorator implements iequipdecorator{/** Each accessory maintains a gear*/ PrivateIequip Equip; PublicRedgemdecorator (Iequip equip) { This. Equip =Equip; } @Override Public intCaculateattack () {return the+Equip.caculateattack (); } @Override PublicString Description () {returnEquip.description () +"+ Ruby"; }}
Well, to this end, we have implemented the requirements of the function, is not each class is very clear and simple, the following look at the test:
Package Com.zhy.pattern.decorator; Public classtest{ Public Static voidMain (string[] args) {//a set of 2 rubies, 1 sapphire bootsSystem. out. println ("a set of 2 rubies, 1 sapphire boots"); Iequip Equip=NewRedgemdecorator (NewRedgemdecorator (NewBluegemdecorator (NewShoeequip ()))); System. out. println ("Damage:"+equip.caculateattack ()); System. out. println ("Description:"+equip.description ()); System. out. println ("-------"); //a weapon set with 1 rubies and 1 sapphiresSystem. out. println ("a weapon set with 1 rubies, 1 sapphires, 1 yellow gems"); Equip=NewRedgemdecorator (NewBluegemdecorator (NewYellowgemdecorator (NewArmequip ()))); System. out. println ("Damage:"+equip.caculateattack ()); System. out. println ("Description:"+equip.description ()); System. out. println ("-------"); }}
Output:
one set of 2 rubies, 1 sapphire boots attack + Description: Jihad boots + sapphire + Ruby + Ruby ------- A set of 1 rubies, 1 sapphires, 1 Yellow gems weapon attack description: Dragon Slayer knife + topaz + sapphire + Ruby -------
Praise, if the need for a few more equipment, a few gems, we can casually add, and then happy heart work.
Well, congratulations, you've learned a design pattern, decorator mode.
Now, according to the example of the definition of the understanding, I do not have to say more.
Java API also has the figure of the decorator pattern, if you beginner Java, you must remember the Java inside the various streams, very painful it, but when you Ming
White your design will feel a lot clearer after that.
Think of InputStream as our iequip, filterinputstream as our iequipdecorator, is not nearly the same as our design ~
Well, to here, programming is also very fun ~ Yes, you crossing leave a message, give a praise Bai ~
SOURCE Click to download
Design pattern Decorator mode takes you back to the legendary world