(Java) from scratch-decorator design pattern

Source: Internet
Author: User

Decorator design Pattern :
Simple definition : Enhance the functionality of a class, and also allow these decorations to decorate each other.

Application Scenario : When you want to expand the functionality on a feature, and the expanded functionality has a large number of permutations, the inheritance will derive a large number of subclasses, at this time using decorator mode to solve.

Decorator Design pattern steps :
1. Maintain a reference to a decorated class in the interior of the decoration class.
2. Let the adornment class have a common parent class or parent interface.

For example: People have a behavior called "eat fruit", of which there are 4 kinds of fruit: apples, bananas, pears, oranges
Now there are the following requirements:
Class A human behavior: eating apples
B-Type human behavior: Eat apples first, then eat bananas
C-Type human behavior: Eat bananas first, then eat apples
Class D Human behavior: Eat oranges first, then eat pear

Let's do this with subclass inheritance: The code is as follows

1 Interfaceieatfruit{2  Public voideatfruit ();3 }4 5 classPersonAImplementsieatfruit{6 7     @Override8      Public voidEatfruit () {9System.out.println ("Eat apples"); Ten     } One  A } - classPersonbImplementsieatfruit{ -  the @Override -      Public voidEatfruit () { -System.out.println ("Eat apples");  -System.out.println ("Eat banana");  +     } -  + } A classPersoncImplementsieatfruit{ at  -    @Override -      Public voidEatfruit () { -System.out.println ("Eat banana"); -System.out.println ("Eat apples");  -     } in  - } to classPersondImplementsieatfruit{ +  -     @Override the      Public voidEatfruit () { *System.out.println ("Eat oranges"); $System.out.println ("Eat pear"); Panax Notoginseng     } -  the}

This is certainly not a problem, each class of people corresponding to the new object can achieve the corresponding requirements.
However, when the demand changes to:
A certain type of human behavior: Eat the above four kinds of fruit, and require a sequencing
There are 24 kinds of permutations ....
So you're going to do it with inheritance? It is obviously inappropriate to write 24 sub-classes to implement an interface.
If you use decorator mode, the code will become:

1 Interfaceieatfruit{2      Public voideatfruit ();3 }4 5 classPersonAImplementsieatfruit{6 ieatfruit eat;7      PublicPersonA () {8          This. Eat = This;9     }Ten      PublicPersonA (Ieatfruit _eat) { One          This. Eat =_eat; A     } - @Override -      Public voidEatfruit () { the         if(! ( This. EatinstanceofPersonA)) { -              This. Eat.eatfruit (); -System.out.println ("Eat apples");  -}Else{ +System.out.println ("Eat apples");  -         } +     } A      at } - classPersonbImplementsieatfruit{ - ieatfruit eat; -      Publicpersonb () { -          This. Eat = This; -     } in      Publicpersonb (Ieatfruit _eat) { -          This. Eat =_eat; to     } + @Override -      Public voidEatfruit () { the         if(! ( This. Eatinstanceofpersonb)) { *              This. Eat.eatfruit (); $System.out.println ("Eat banana"); Panax Notoginseng}Else{ -System.out.println ("Eat banana");  the         } +     } A      the } + classPersoncImplementsieatfruit{ - ieatfruit eat; $      PublicPersonc () { $          This. Eat = This; -     } -      PublicPersonc (Ieatfruit _eat) { the          This. Eat =_eat; -     }Wuyi @Override the      Public voidEatfruit () { -         if(! ( This. EatinstanceofPersonc)) { Wu              This. Eat.eatfruit (); -System.out.println ("Eat pear");  About}Else{ $System.out.println ("Eat pear");  -         } -     } -      A } + classPersondImplementsieatfruit{ the ieatfruit eat; -      PublicPersond () { $          This. Eat = This; the     } the      PublicPersond (Ieatfruit _eat) { the          This. Eat =_eat; the     } - @Override in      Public voidEatfruit () { the         if(! ( This. EatinstanceofPersond)) { the              This. Eat.eatfruit (); AboutSystem.out.println ("Eat oranges");  the}Else{ theSystem.out.println ("Eat oranges");  the         } +     } -      the }Bayi  the  the  -  Public classDemo2 { -  the      Public Static voidMain (string[] args) { the         //This allows the above 4 categories of people to decorate each other, you can arbitrarily any combination of "eat fruit" the         //such as: eat oranges, eat apples thePersond d =NewPersond (); -PersonA A =NewPersonA (d); the a.eatfruit (); theSystem.out.println ("-------I am the dividing line------------"); the         //such as: eat apples--eat oranges --eat pear---eat bananas94PersonA A2 =NewPersonA (); thePersond D2 =NewPersond (A2); thePersonc C2 =NewPersonc (D2); thePersonb B2 =Newpersonb (C2);98 b2.eatfruit (); About     } - 101}

The output results are as follows:
Eat oranges
Eat apples
-------I'm a split line------------
Eat apples
Eat oranges
Eat pear
Eat bananas

Final Summary :
What is the difference between an enhanced class implemented by an inherited implementation and a decorated pattern implementation?
Inheritance-Implemented enhancement classes:
Pros: The code is structurally clear and simple to implement.
Cons: For each class that needs to be enhanced, create a specific subclass to help enhance it, which causes
The succession system is too large.
Enhanced classes implemented by Decorator mode:
Pros: Multiple classes that need to be enhanced can be enhanced internally by polymorphic techniques to make these decorative classes
Achieve the effect of decorating each other. More flexible to use.
Disadvantage: an instance of a class that needs to be enhanced internally through polymorphic maintenance is required. Which in turn makes the code slightly more complex

(Java) from scratch-decorator design pattern

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.