1 PackageShb.java.demo;2 /**3 * Test Decorator Mode4 * @package: SHB.JAVA.DEMOJAVA025 * @authorshaobn6 * @Describe:7 * @Time: 2015-9-9 a.m. 12:17:008 */9 Public classTestdecrator {Ten One /** A * @authorshaobn - * @Describe: - * @paramargs the * @Time: 2015-9-9 a.m. 12:16:49 - */ - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub +Campusactivity CA =NewAdvancedact (Newcampusinneract ()); - Ca.play (); +Campusactivity ca_2 =NewAdvancedact (Newcampusouteract ()); A Ca_2.play (); at } - - } - Interfacecampusactivity{ - - voidplay (); in } - classCampusinneractImplementscampusactivity{ to Public voidPlay () { +System.out.println ("Play basketball with my little friends"); - } the } * classCampusouteractImplementscampusactivity{ $ Public voidPlay () {Panax NotoginsengSystem.out.println ("and small partners to go downtown shopping"); - } the } + classCampus__Implementscampusactivity{ A Public voidPlay () { the + } - } $ classAdvancedactImplementscampusactivity{ $ Privatecampusactivity CA; - PublicAdvancedact (campusactivity CA) { - This. ca =CA; the } - Public voidPlay () {Wuyi Ca.play (); theSystem.out.println ("Riding an electric car to play volleyball, driving in a car to sing"); - } Wu -}
1 PackageShb.java.demo;2 /**3 * Test Decorator Mode4 * @package: SHB.JAVA.DEMOJAVA025 * @authorshaobn6 * @Describe:7 * @Time: 2015-9-9 a.m. 12:17:008 */9 Public classtestdecrator_2 {Ten One /** A * @authorshaobn - * @Describe: - * @paramargs the * @Time: 2015-9-9 a.m. 12:16:49 - */ - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub +Adcampusinneract Acia =Newadcampusinneract (); - Acia.play (); +Adcampusouteract Acoa =Newadcampusouteract (); A Acoa.play (); at } - - } - Interfacecampusactivity{ - - voidplay (); in } - classCampusinneractImplementscampusactivity{ to Public voidPlay () { +System.out.println ("Play basketball with my little friends"); - } the } * classAdcampusinneractextendscampusinneract{ $ Public voidPlay () {Panax Notoginseng Super. Play (); -System.out.println ("Riding an electric car and playing volleyball with a small partner"); the } + } A classCampusouteractImplementscampusactivity{ the Public voidPlay () { +System.out.println ("and small partners to go downtown shopping"); - } $ } $ classAdcampusouteractextendscampusouteract{ - Public voidPlay () { - Super. Play (); theSystem.out.println ("Go out and sing in a car"); - }Wuyi } the classcampus__{ - Public voidPlay () { Wu - } About } $ classAdcampus__extendscampus__{ - Public voidPlay () { - //Enhanced Features ... - } A}
The first paragraph uses decorator mode, and the second paragraph uses inheritance.
In fact, we can see from two pieces of code, our aim is to let the original function enhancement, so it is derived from a richer method, so we naturally think of inheritance. At the same time, there is a problem, when we use inheritance, when we find that the need to add a lot of classes, we need to rewrite a class to inherit the parent class, thus overriding the method. This will add a lot of burden, and the coupling is very high, not easy to extend the program. The decorator pattern comes along with it. We just need to define a subclass of an interface, then get a reference to the same interface, and then enhance the method that belongs to the subclass object under the interface reference.
Two pieces of code comparison, it is obvious that the pros and cons.
If there are errors, I hope you will correct them.
It's not too early to go to bed ....
2015-09-09
Java Test Decorator Mode