North wind pattern design courses --- 14 proxy mode

North wind pattern design courses --- 14 proxy mode

A summary

Sentence summary:

Not only to learn through video, but also the inside of the introduction of other blog, to explain the search, the role of search, search examples
Design models are abstractions of life, such as user access to equipment, I can start to equip the plant produced equipment, then give these doors arsenal, arsenal were the door to me, if it is Daguai equipment, can be equipped with the factory is equipped to a monster arsenal

 

1, what more instances of proxy mode using a?

Blockers: frameworks inside the interceptor

 

2. What is a proxy mode?

The client must interact with the target class proxy agent, and the agent is generally in the course of an interaction (interactive front), perform some special treatment
   Proxy mode Proxy mode is also called, is a stereotype of design 
one mode, it can provide a proxy (Proxy) for other objects to 
control access to the object. 
    The so-called proxy means having (proxy object) has a proxy-membered 
same interface class, the client must delegate object with the agent 
-based interaction, the agent is generally in the course of an interaction (for interactive front), into 
line some special treatment.

 

 

3, the role instance agency model?

Abstract class: interface class, there are interfaces to sell books
Real categories: concrete class, there is a method to sell books
Agent class: bookstore selling books such, can also be discounted, gift vouchers or the like

 

4, roles and responsibilities of agency model?

Common interface to the real subject of the proxy theme: subject (abstract thematic roles).
RealSubject (real thematic roles): Defines the real object represented by proxy role.
Proxy (proxy thematic roles): contains references to the real subject of the role, the agent role is usually the client calls to perform certain actions before or after the transfer to the theme is really the object, rather than simply return the real object.

 

5, java dynamic proxy how to achieve?

1. InvocationHandler Interface
2. invoke method
3. Proxy.newProxyInstance();

 

6, the role instance demo code in proxy mode?

1, subject (abstract thematic roles): Subject.java
2, RealSubject (real thematic roles): RealSubject.java
3, Proxy (proxy thematic roles): ProxySubject.java
4, client: MainClass.java

 

 

 

 

Second, content in summary

1, Knowledge

subject (abstract thematic roles): Subject.java
RealSubject (real thematic roles): RealSubject.java
Proxy (proxy thematic roles): ProxySubject.java
Client: MainClass.java

 

 

2, the code

subject (abstract thematic roles): Subject.java

public interface Subject {
    public  void sailBook();
}

 


RealSubject (real thematic roles): RealSubject.java

public class RealSubject implements Subject {

    public void sailBook() {
        System.out.println("卖书");
    }

}

 


Proxy (proxy thematic roles): ProxySubject.java

public class ProxySubject implements Subject{
    private RealSubject realSubject;

    public void sailBook() {
        dazhe();
        if(realSubject == null) {
            realSubject = new RealSubject();
        }
        realSubject.sailBook();
        give();
    }
    
    public void dazhe() {
        System.out.println("打折");
    }
    
    public void give() {
        System.out.println("赠送代金券");
    }
}

 


Client: MainClass.java

public class MainClass {
    public static void main(String[] args) {
        ProxySubject proxySubject = new ProxySubject();
        proxySubject.sailBook();
    }
}

 

 

 

 

Reproduced in: https: //www.cnblogs.com/Renyi-Fan/p/11030868.html

Guess you like

Origin blog.csdn.net/weixin_33691700/article/details/93572307