Original: Curly brace MC(wechat official account: Huakuohao-MC), welcome to share, please keep the source.

The proxy pattern is the same as the proxy in everyday life, that is, create a proxy class to replace the real business class to complete the core business scenario. The proxy pattern can hide real business class details, and the proxy class can even perform some logging operations at the start and end of the core business, similar to the AOP we often use.

For example

We have a function to load and display images, we don’t want to show the details of loading images, and we want to count how long it takes to load images each time. At this time, we can consider using proxy mode.

Take a look at the UML diagram:

Now, how does this code work

public interface Image {
    void display(a);
}
Copy the code

Classes that actually do the core business

public class RealImage implements Image {

    private String fileName;

    public RealImage(String fileName) throws InterruptedException {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    @Override
    public void display(a) {
        System.out.println("Displaying " + fileName);
    }

    private void loadFromDisk(String fileName) throws InterruptedException {
        System.out.println("Loading " + fileName);
        Thread.sleep((long) (Math.random()*100)); }}Copy the code

Proxy class, in addition to complete the main business logic, but also added to the load picture statistics.

public class ProxyImage implements Image {

    private RealImage realImage;
    private String fileName;

    public ProxyImage(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void display(a) throws InterruptedException {
        if (realImage == null) {long startTime = System.currentTimeMillis();
            realImage = new RealImage(fileName);
            long endTime = System.currentTimeMillis();
            long spendTime = endTime - startTime;
            System.out.println("Loading image cost:"+ spendTime ); } realImage.display(); }}Copy the code

How does the client work

public class ProxyPatternDemo {
    public static void main(String[] args) throws InterruptedException {
        // Use proxy classes
        Image image = new ProxyImage("test_10mb.jpg");

        // The image is loaded and the loading time is counted. All details are hidden
        image.display();
        System.out.println(""); }}Copy the code

conclusion

Proxy pattern is one of the structural pattern, the frequency of daily use is relatively high, the most typical application is AOP, logging and other functions.

This paper reference www.tutorialspoint.com/design_patt…

Recommended reading

1. Java concurrent programming stuff (10) — Final summary

2. Common network problem locating tools that programmers should master

3. Do you know how to use Awk

4. Teach you how to build a set of ELK log search operation and maintenance platform

Original: Curly brace MC(wechat official account: Huakuohao-MC) Focus on JAVA basic programming and big data, focus on experience sharing and personal growth.