设计模式之-静态代理

静态代理能做什么呢?

比如周杰伦 他会唱歌 有很多人想找周杰伦唱歌但是周杰伦一个人哪能忙的过来啊,像和客户谈合同,订票,收尾款等等的这些杂活他只需要交给经纪人做就OK了他只需要唱歌就好了.

实现思想:

代码实现:

共同的接口:

package org.west.demo;

public interface Star {
    //面谈
    void confer();
    //签合同
    void signContart();
    //订票
    void bookTacket();
    //唱歌
    void sign();
    //收尾款
    void collentMoney();
}

真实的角色:

public class RealStar implements Star{
    public void confer() {

    }

    public void signContart() {

    }

    public void bookTacket() {

    }

    public void sign() {
        System.out.println("唱歌");
    }

    public void collentMoney() {

    }
}

代理角色:

 1 package org.west.demo;
 2 
 3 public class ProxyStar implements Star{
 4    //代理是不会唱歌的这时候我们需要一个真实的角色
 5     private RealStar realStar;
 6 
 7     public ProxyStar(RealStar realStar) {
 8         this.realStar = realStar;
 9     }
10 
11     public void confer() {
12         System.out.println("面谈");
13 
14     }
15 
16     public void signContart() {
17         System.out.println("签合同");
18 
19     }
20 
21     public void bookTacket() {
22         System.out.println("订票");
23 
24     }
25 
26     public void sign() {
27          realStar.sign();
28     }
29 
30     public void collentMoney() {
31         System.out.println("收尾款");
32 
33     }
34 }

客户:

 1 package org.west.demo;
 2 
 3 public class Client {
 4 
 5     public static void main(String[] args) {
 6         RealStar real = new RealStar();
 7         ProxyStar proxyStar = new ProxyStar(real);
 8         proxyStar.confer();
 9         proxyStar.signContart();
10         proxyStar.bookTacket();
11         proxyStar.sign();
12         proxyStar.collentMoney();
13 
14     }
15 
16 }
原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/11264906.html