Proxy mode

The definition of proxy mode: proxy mode provides a proxy object for a certain object, and the proxy object controls the reference to the original object. For example, one person or an organization acts on behalf of another person or another organization. In some cases, a client does not want or cannot directly refer to an object, and the proxy object can act as an intermediary between the client and the target object.

From the above description of the agency model, we can know that its purpose is to control object references. In the life scene, we take buying a car as an example. If we want to buy a car, we must go through the car 4S shop. The car 4s shop acts as an agent. Its purpose It is to control the car buying behavior of car buyers. You must go through the car 4S shop to buy a car from the car manufacturer.

The proxy object usually implements the same interface as the proxy object.

1.) First create a new interface for buying a car

public interface IBuyCar {
    //买车
    void buyCar();
}

2.) Declare a customer who wants to buy a car and implement the buy car interface

public class Customer implements IBuyCar {

    private int cash;//购车款

    public int getCash() {
        return cash;
    }

    public void setCash(int cash) {
        this.cash = cash;
    }

    @Override
    public void buyCar() {
        Log.e("buyCar", "买一辆车花费了-->" + cash + "元");
    }
}

3.) Declare that a 4S shop that buys a car as an agent for a car also implements the car buying interface and must accept orders from customers

public class BuyCarProxy implements IBuyCar{
    private Customer customer;//接收买车客户

    public BuyCarProxy(Customer customer){
        this.customer=customer;//接收买车客户
    }

    @Override
    public void buyCar() {//实现为客户买车
        customer.buyCar();
    }
}

4.) Create a client to simulate buying a car

 Customer customer=new Customer();
  customer.setCash(120000);
  BuyCarProxy buyCarProxy=new BuyCarProxy(customer);
  buyCarProxy.buyCar();

5.) Realize authority control through proxy mode

Through the above example, we may have a question, can't we go directly to the manufacturer to buy a car? Of course, if the implementation class can meet the requirements in the usage scenario, we can of course implement the class directly, but when the implementation class cannot meet the requirements, we need to extend the requirements. According to the principle of opening and closing, you cannot modify the implementation class code, then you Use proxy classes. For example, to purchase a car, we need to conduct a review of the purchase price of the customer. If it meets the conditions, we will buy the car. If it does not meet the requirements, we will inform the customer that the purchase price is insufficient.

@Override
    public void buyCar() {//实现为客户买车
        int cash=customer.getCash();
        if(cash<100000){
            Log.e("buyCar","你的钱不够买一辆车");
            return;
        }
        customer.buyCar();
    }

 

 

 

 

Guess you like

Origin blog.csdn.net/sinat_37138973/article/details/88247163