Proxy (Proxy) design pattern

Proxy (Proxy) design pattern

Background 1.0.0 generated proxy mode

  • The system has a core, the core function of the code is to achieve basic.
  • In some cases, in order to meet the needs of the business, we need to reinforce the existing core after () before () and other methods of pre- and post even surrounded.

1.0.1 to strengthen the existing classes of way

  • One solution is through inheritance, override, or transform strengthen the core.
  • , The core to be modified to modify or transform objects has been reached on the strengthening of the operation through a proxy mode.

1.0.2 Why use if the proxy classes to strengthen?

  • Inheritance will expose interfaces or parent class
  • Acting can be packaged a number of different objects, complete solution object.
  • The method can use the proxy to implement the functions of the parameters, or the interface is not exposed to any parent class, better encapsulation. This is to say language, of course, different ways of operation of reflection, can be read class function.

2.0.0 defined proxy mode

Proxy mode by creating a proxy object B to the object A, B contained in the reference A, and then control the behavior of the A mode.

Examples reality: A B because labor issues will appeal to the court, A do not understand the law, we need to go and C A proxy, in order to achieve the best results.
Java `` `
Class A {
public void Preparation Materials () {}
public void eat () {}
public void sleep () {}
}

C {class
Private A A;
public C (A A) = {A this.a;}
public void appeal () {acquisition material (); the legal basis for (); to complete the action ();}
Private void acquisition material () {. a ready material ();}
Private void the legal basis for () {}
Private litigation complete void () {}
}
// C is the agent of class a, we can not access any of a through C interface or function.
`` `

3.0.0 Classification proxy mode

Acting in accordance with the time of creation:

  • Static proxy mode ---> already created during compilation
  • Create a good period ---> Run dynamic proxy mode

3.0.1 What is a compile time? What is the run? ...

4.0.0 implement proxy mode

4.0.1 Static proxy implementation

@Slf4j
public class Proxy {
    class Worker {
        public void eat() {log.info("{}", "eat");}
        public void sleep() {log.info("{}", "sleep");}
        public void work() {log.info("{}", "work");}
    }

    class WorkerProxy {
        private Worker worker;
        public WorkerProxy(Worker worker) {this.worker = worker;}
        public void work() {
            before();
            worker.work();
            after();
        }
        private void before() {log.info("{}", "before");}
        private void after() {log.info("{}", "after");}
    }

    public static void main(String[] args) {
        Proxy proxy = new Proxy();
        Worker worker = proxy.new Worker();
        WorkerProxy workerProxy = proxy.new WorkerProxy(worker);
        workerProxy.work();
    }
}

4.0.2 dynamic proxy implementation ...

Guess you like

Origin www.cnblogs.com/fengzhida/p/11373533.html