Use the decoration mode to view the interface, and the decoration Mode Interface

Source: Internet
Author: User

Use the decoration mode to view the interface, and the decoration Mode Interface
Decorator)Intention

Decoration Mode

Dynamically add some additional responsibilities to an object. The Decorator mode is more flexible than the subclass generation function. Applicability
  • Add roles to a single object dynamically and transparently without affecting other objects.
  • Handle unrecoverable responsibilities.
  • When the subclass generation method cannot be used for expansion. One case is that there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Another scenario is that the class definition is hidden, or the class definition cannot be used to generate a subclass.
  • Using the decoration mode, we can see that the interface is implemented in the called interface,
  • The interface method is implemented where the interface is implemented, then, the interface implementation object is received at the place where the interface is called, and then the methods in the interface can be directly implemented in the class that calls the interface (the method in the interface ).
  • Java code of the abstract component role
    1. Package decorator;
    2. /**
    3. * Common method interface between the decorator and the original component (Abstract component role)
    4. * @ Author mouca. he
    5. *
    6. */
    7. Public interface InterfaceComponent {
    8. /**
    9. * Component method say ()
    10. *
    11. */
    12. Public void say ();
    13. }

    Java code of the specific component role

    1. Package decorator;
    2. /**
    3. * Original component (specific component role)
    4. * @ Author mouca. he
    5. *
    6. */
    7. Public class Component implements InterfaceComponent {
    8. Public void say (){
    9. // TODO automatically generates method stubs
    10. System. out. println ("Component. say (): Method of the original Component! ");
    11. }
    12. }

    Java code of the abstract modifier role

    1. Package decorator;
    2. /**
    3. * Abstract Modifier
    4. * @ Author mouca. he
    5. *
    6. */
    7. Public abstract class AbstractDecorator implements InterfaceComponent {
    8. Private InterfaceComponent component;
    9. Public AbstractDecorator (InterfaceComponent component ){
    10. This. component = component;
    11. }
    12. /**
    13. * Component Method Pre-processing method
    14. *
    15. */
    16. Protected void preSay (){};
    17. /**
    18. * Post-processing method for component method execution
    19. *
    20. */
    21. Protected void afterSay (){};
    22. Public void say (){
    23. PreSay ();
    24. Component. say ();
    25. AfterSay ();
    26. };
    27. }

    Specific modifier 2 java code

    1. Package decorator;
    2. /**
    3. * Decorator 2
    4. * @ Author mouca. he
    5. *
    6. */
    7. Public class DecoratorTwo extends actdecorator {
    8. Public DecoratorTwo (InterfaceComponent component ){
    9. Super (component );
    10. // TODO automatically generates the constructor stub
    11. }
    12. /**
    13. * Reload the template class preSay () method as needed
    14. */
    15. Protected void preSay (){
    16. System. out. println ("DecoratorTwo. preSay (): preSay () method of modifier 2! ");
    17. }
    18. /**
    19. * Overload the template class afterSay () method as needed
    20. */
    21. Protected void afterSay (){
    22. System. out. println ("DecoratorTwo. afterSay (): The afterSay () method of modifier 2! ");
    23. }
    24. }

    Modifier 1 java code

    1. Package decorator;
    2. /**
    3. * Decorator 1
    4. * @ Author mouca. he
    5. *
    6. */
    7. Public class DecoratorOne extends actdecorator {
    8. Public DecoratorOne (InterfaceComponent component ){
    9. Super (component );
    10. // TODO automatically generates the constructor stub
    11. }
    12. /**
    13. * Reload the template class preSay () method as needed
    14. */
    15. Protected void preSay (){
    16. System. out. println ("DecoratorOne. preSay (): preSay () method of modifier 1! ");
    17. }
    18. /**
    19. * Overload the template class afterSay () method as needed
    20. */
    21. Protected void afterSay (){
    22. System. out. println ("DecoratorOne. afterSay (): The afterSay () method of modifier 1! ");
    23. }
    24. /**
    25. * Test Method
    26. * @ Param args
    27. */
    28. Public static void main (String [] args ){
    29. // TODO automatically generates method stubs
    30. InterfaceComponent interfaceComponent = new DecoratorTwo (new DecoratorOne (new Component ()));
    31. InterfaceComponent. say ();
    32. /*
    33. * Console output:
    34. * DecoratorTwo. preSay (): The preSay () method of DecoratorTwo!
    35. * DecoratorOne. preSay (): The preSay () method of DecoratorOne 1!
    36. * Component. say (): Method of the original Component!
    37. * DecoratorOne. afterSay (): The afterSay () method of modifier 1!
    38. * DecoratorTwo. afterSay (): The afterSay () method of DecoratorTwo!
    39. */
    40. }
    41. }

    4. Advantages and Disadvantages

    Advantages: 1) provide more flexibility than inheritance 2) use different decorative combinations to create combinations of different behaviors 3) Reduce the number of classes required

    Disadvantages: 1) Flexibility leads to greater errors. 2) more objects are generated, which makes error detection difficult.


Related Article

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.