装饰是在原有类上增加功能用的
代理是对原有类作限制用的
机制差不多,但语义上方向相反
————————————————————————
追美女的一种方式是从她的朋友下手,让她的死党帮忙,那就离成功没有多远了。好,我们把她的死党作为这个美女的代理,开始的时候我们当然要和代理打交道了,我们用代理模式实现以下。定义一个接口,这个借口有一个behavior()方法。
- 1.public interface Girl {
- 2.
- 3. public void behavior();
- 4.
- 5.}
1.public interface Girl {
2.
3. public void behavior();
4.
5.}
然后让美女类实现这个接口
- 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+"长的非常nice");
- 10. System.out.println(this.name+"说话也非常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+"长的非常nice");
10. System.out.println(this.name+"说话也非常nice");
11.
12. }
13.
14.}
接下来定义代理类,代理类也实现了Girl接口,不但如此,代理类还要关联它要代理的对象,所以要定义Girl类型的一个成员变量,
- 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.
- 12. @Override
- 13. public void behavior() {
- 14. Random rand = new Random();
- 15. if(rand.nextBoolean())
- 16. {
- 17. System.out.println("我安排你们上自习");
- 18. girl.behavior();
- 19. }
- 20. else{
- 21. System.out.println("先看你的表现,上自习以后再说");
- 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;
10. }
11.
12. @Override
13. public void behavior() {
14. Random rand = new Random();
15. if(rand.nextBoolean())
16. {
17. System.out.println("我安排你们上自习");
18. girl.behavior();
19. }
20. else{
21. System.out.println("先看你的表现,上自习以后再说");
22. }
23. }
24.}
OK,代理类图中的所有的类都实现了,下面编写一个测试类
- 1.public class Client {
- 2.
- 3. public static void main(String[] args) {
- 4.
- 5. Girl niceGirl = new NiceGirl("小美");
- 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("小美");
6.
7. Girl friend = new GirlAgent(niceGirl);
8.
9. friend.behavior();
10. }
11.
12.}
哈哈,代理模式就学会了吧。那装饰模式是怎么回事呢?装饰模式只要改动一处代码就可以了,对代理类的behavior()方法改动如下,其他的类不用动。
-
- @Override
- 2. public void behavior() {
- 3.
- 4.System.out.println("我家MM不但知书达礼,而且还会做饭");
- 5.girl.behavior();
- 6.
- 7. }