Java Design Pattern--Agent Pattern

1 Proxy Pattern

Purpose: Provide a proxy for other objects to control access to this object;
Implementation: Increase the middle layer, and combine the implementation and the proxied class.

Use when access to objects should be controlled; use when
access to objects should provide additional functions. The difference
from the decorator mode is that it emphasizes access control. No matter how anxious the Emperor Guangxu is, he should ask Cixi first.

2 Implementation

Code scene: At the end of the Qing Dynasty, the Empress Dowager Cixi was under the curtain and the Emperor Guangxu was overthrown, and the government was actually controlled by Cixi.

2.1 Code Implementation

Emperor interface

public interface Emperor {
    //处理朝政
    public void dealThings(String thing);
}

Agent class: Emperor's realization class Emperor Guangxu

public class GuangXu implements Emperor {
    public GuangXu() {
        System.out.println("我是光绪皇帝,我旁边坐着慈禧太后");
    }

    @Override
    public void dealThings(String thing) {
        System.out.println("光绪看着关于[" + thing + "]的奏折,无奈的摇了摇头。");
    }

}

Agency: Empress Dowager Cixi

public class CiXi implements Emperor {
    private Emperor hd = new GuangXu();
    public CiXi() {
        System.out.println("慈禧太后垂帘听政");
    }
    @Override
    public void dealThings(String thing) {
        look(thing);
        decide(thing);
        hd.dealThings(thing);
    }
    private void look(String thing) {
        System.out.println("慈禧太后过目了一下关于[" + thing + "]的奏折");
    }
    private void decide(String thing) {
        Random r = new Random();
        int i = r.nextInt(10);
        //根据心情来进行决策
        if (i % 2 == 0) {
            System.out.println("然后否决了关于[" + thing + "]的奏折");
        } else {
            System.out.println("然后同意了关于[" + thing + "]的奏折");
        }

    }

}

2.2 Involving roles

The proxy mode includes the following three roles:

Subject (Abstract Subject Role): It declares the common interface of the real subject and the proxy subject, so that the proxy subject can be used wherever the real subject is used, and the client usually needs to program against the abstract subject role.

Proxy (proxy subject role): It contains a reference to the real subject, so that the real subject object can be manipulated at any time; the proxy subject role provides an interface identical to the real subject role, so that the real subject can be substituted at any time ; the proxy subject role can also control the use of the real subject, be responsible for creating and deleting real subject objects when needed, and constrain the use of real subject objects. Typically, in a proxy topic role, the client needs to perform additional actions before or after invoking the referenced real topic operation, not just calling operations in the real topic object.

RealSubject (real subject role): It defines the real object represented by the proxy role, and implements real business operations in the real subject role. The client can indirectly call the operations defined in the real subject role through the proxy subject role.

2.3 call

caller:

public class Client {
    public static void main(String[] args) {
        CiXi cx = new CiXi();
        cx.dealThings("给北洋舰队购买装备");
    }
}

result:

我是光绪皇帝,我旁边坐着慈禧太后
慈禧太后垂帘听政
慈禧太后过目了一下关于[给北洋舰队购买装备]的奏折
然后否决了关于[给北洋舰队购买装备]的奏折
光绪看着关于[给北洋舰队购买装备]的奏折,无奈的摇了摇头。

Code address: click to jump

References:
[1] Graphical Design Patterns/(Japanese) Hiroshi Yuki; translated by Yang Wenxuan. – Beijing: People’s Posts and Telecommunications Press, 2017.1.
[ 2 ] Wikipedia Design
Patterns [ 3 ] Geek Academy WIKI – Design Patterns .
[ 4 ] Rookie Tutorial – Design Patterns .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463262&siteId=291194637