Hello, everyone! Today the universe is full of four big words, design mode! Let’s swim in the ocean of design patterns!

The proxy pattern is one of the structural patterns. What is the structural pattern pinch? The structural pattern is more concerned with the composition of classes and objects.

What is proxy mode pinch?

Let’s say I want to access class B, but I don’t want to access class B directly, I want to access class B through class A.

A lot of people are thinking, do you eat food? Why don’t you just go to Category B, with all the bells and whistles, where are the Matryoshka dolls?

What is the benefit of the proxy pattern, which primarily provides alternative access to the target object? Based on the implementation of the target object, you can enhance additional functions and expand the target object.

There are many such examples in real life. When you want to contact a star, you cannot directly contact him or her, but through his or her agent, who is responsible for a series of things about the star.

When you want to find a house, you usually go to an intermediary, who can directly care about the housing supply and help you do a series of things well.

The proxy pattern is the same. The proxy class manages the target class and makes some enhancements to the target class, such as maintaining, extending, controlling the life cycle of the target class, doing log output, etc. The proxy class is actually an intermediate layer.

Advantages of the proxy mode: clear responsibilities and high scalability.

Disadvantages of the proxy pattern: Proxy objects need to be added, and target objects can be requested more slowly because they are not accessed directly.

Class diagram:Concrete implementation:

/ / take-away
public interface TakeOut {
    void place(a);
}

// Ramen take-out
public class LaMianNoodles implements TakeOut {

    @Override
    public void place(a) {
        System.out.println("Ramen Takeout"); }}// The delivery guy
public class TakeAwayDeliveryMan implements TakeOut {

    / / take-away
    private TakeOut takeOut;

    public TakeAwayDeliveryMan(TakeOut takeOut) {
        this.takeOut = takeOut;
    }

    @Override
    public void place(a) { takeOut.place(); }}public class Client {
    public static void main(String[] args) {
        TakeOut takeOut = new LaMianNoodles();
        TakeAwayDeliveryMan takeAwayDeliveryMan = newTakeAwayDeliveryMan(takeOut); takeAwayDeliveryMan.place(); }}/ / get take-out
Copy the code

Well, that’s it for this chapter, I hope you can get something out of it!

May everyone read this article with skepticism and explore how it works.

Road obstruction and long, the past for preface, the future for chapter.

Looking forward to our next meeting!