java achieve a static proxy mode

public class ProxyDemo {
    public static void main(String[] args) {
        Person p = new Person("阿健");
        Matchmaker m = new Matchmaker(p);
        m.miai();
    }
}

interface Subject{
    public void miai();
}

// the proxy class
class Person implements Subject {

    private String name;
    public Person(String name){
        this.name = name;
    }
    
    @Override
    public void miai() {
        // TODO Auto-generated method stub
        System.out.println(name+"正在相亲中...");
    }
    
}

// proxy class
class Matchmaker implements Subject {

    private Subject target;
    
    public Matchmaker(Subject target){
        this.target = target;
    }
    
    private void before(){
        System.out.println("准备工作!");
    }
    
    private void after(){
        System.out.println("相亲后的工作!");
    }
    
    @Override
    public void miai() {
        // TODO Auto-generated method stub
        before();
        target.miai();
        after();
    }
    
}
 

Published 40 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sj_1993/article/details/103037425