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.public interface Girl {
- 2.
- 3. Public Void behavior ();
- 4.
- 5.}
1.public interface Girl { 2. 3. Public Void Behavior (); 4. 5.}
Then let the beauty class implement this interface
- 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");
- 11.
- 12.}
- 13.
- 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.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;
- 10.}
- 11.
- @Override .
- Public Void behavior () {
- Random rand = new Random ();
- if (Rand.nextboolean ())
- 16. {
- System.out.println ("I arrange for you to study in self-study");
- Girl.behavior ();
- 19.}
- else{ .
- System.out.println ("First look at your performance, after the study to say Again");
- 22.}
- 23.}
- 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.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 ();
- 10.}
- 11.
- 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.
- @Override
- 2. Public Void behavior () {
- 3.
- 4.system.out.println ("My home mm not only know the book of the ceremony, but also cooking");
- 5.girl.behavior ();
- 6.
- 7.}
The difference between decoration and agency