The difference between decoration and agency

Source: Internet
Author: User

Decoration is used to add functionality to the original class.
The agent is restricted to the original class.
The mechanism is the same, but semantically opposite.

————————————————————————

A way to chase a beautiful woman from her friends, let her best friend help, it is not far from success. OK, we put her best friend as the agent of this beautiful woman, at the beginning of course we have to deal with agents, we use the proxy mode to achieve the following. Define an interface that has a behavior () method.

    1. 1.public interface Girl {
    2. 2.
    3. 3. Public Void behavior ();
    4. 4.
    5. 5.}
1.public interface Girl {   2.       3. Public    Void Behavior ();   4.  5.}  


Then let the beauty class implement this interface

  1. 1.public class Nicegirl implements Girl {
  2. 2.
  3. 3. private String name;
  4. 4. Public Nicegirl (String name) {
  5. 5. this.name = name;
  6. 6.}
  7. 7. @Override
  8. 8. Public Void behavior () {
  9. 9. System.out.println (this.name+"long and very nice");
  10. System.out.println (this.name+"speaks very nice");
  11. 11.
  12. 12.}
  13. 13.
  14. 14.}
1.public class Nicegirl implements Girl {   2.  3.    private String name;   4. Public    Nicegirl (String name) {   5.        this.name = name;   6.    }   7.    @Override  8.    public void behavior () {   9.        System.out.println (this.name+ "long and very nice");        System.out.println (this.name+ "speaks very nice");   One.    }  14.}  


Next define the proxy class, the proxy class also implements the girl interface, not only that, the proxy class is also associated with the object it wants to proxy, so to define a member variable of the girl type,

  1. 1.Import Java.util.Random;
  2. 2.
  3. 3.public class Girlagent implements Girl {
  4. 4.
  5. 5. private Girl Girl;
  6. 6.
  7. 7. Public girlagent (Girl Girl) {
  8. 8. super ();
  9. 9. this.girl = girl;
  10. 10.}
  11. 11.
  12. @Override .
  13. Public Void behavior () {
  14. Random rand = new Random ();
  15. if (Rand.nextboolean ())
  16. 16. {
  17. System.out.println ("I arrange for you to study in self-study");
  18. Girl.behavior ();
  19. 19.}
  20. else{ .
  21. System.out.println ("First look at your performance, after the study to say Again");
  22. 22.}
  23. 23.}
  24. 24.}
1.import Java.util.Random;   2.  3.public class Girlagent implements Girl {   4.       5.    private Girl Girl;   6.  7.    Public girlagent (Girl Girl) {   8.        Super ();   9.        this.girl = girl;   Ten.    }   .  @Override    . public void behavior () {   .        Random rand = new Random ();        if (Rand.nextboolean ())   .        {            System.out.println ("I arrange for you to study in self-study");            Girl.behavior ();   .        else{   .            System.out.println ("First look at your performance, after the study to say Again");   .    }   24.}  


OK, all classes in the proxy class diagram are implemented, and a test class is written below

  1. 1.public class Client {
  2. 2.
  3. 3. public static void Main (string[] args) {
  4. 4.
  5. 5. Girl Nicegirl = new Nicegirl ("Mei");
  6. 6.
  7. 7. Girl friend = new Girlagent (Nicegirl);
  8. 8.
  9. 9. Friend.behavior ();
  10. 10.}
  11. 11.
  12. 12.}
1.public class Client {   2.  3. Public    static void Main (string[] args) {   4.           5.      Girl nicegirl = new Nicegirl ("Mei");   6.  7.      Girl friend = new Girlagent (nicegirl);   8.         9.      Friend.behavior ();   Ten.    }   .  12.}  


Haha, the agency mode to learn it. What about the decorating pattern? Decoration mode as long as the change of a code, the proxy class behavior () method changes as follows, the other classes do not move.

      1. @Override
      2. 2. Public Void behavior () {
      3. 3.
      4. 4.system.out.println ("My home mm not only know the book of the ceremony, but also cooking");
      5. 5.girl.behavior ();
      6. 6.
      7. 7.}

The difference between decoration and agency

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.