• Groovy 设计模式 -- proxy & delegate


    Proxy

    https://en.m.wikipedia.org/wiki/Proxy

    代理人 与 被代理人 是 一对一的关系。

    A proxy is an agent or substitute authorized to act for another person or a document which authorizes the agent so to act, and may also be used in the following contexts:

    delegate

    https://en.m.wikipedia.org/wiki/Delegate

    代表模式 -- 组织与组织之间的会谈, 派出的代表来商谈。

    A delegate is someone who attends or communicates the ideas of or acts on behalf of an organization at a meeting or conference between organizations, which may be at the same level or involved in a common field of work or interest.

    Organizations may hold conventions where the membership from different parts of the organization is assembled.[1] Delegates attend the convention to represent their part of the organization.

    For example, an organization may be national in scope and consist of many local member clubs. Such an organization may hold an annual meeting where each local club can send delegates, or representatives to vote on behalf of the club, to the national convention.

    agent

    https://en.m.wikipedia.org/wiki/Law_of_agency

    The law of agency is an area of commercial law dealing with a set of contractual, quasi-contractual and non-contractual fiduciary relationships that involve a person, called the agent, that is authorized to act on behalf of another (called the principal) to create legal relations with a third party.[1] Succinctly, it may be referred to as the equal relationship between a principal and an agent whereby the principal, expressly or implicitly, authorizes the agent to work under his or her control and on his or her behalf. The agent is, thus, required to negotiate on behalf of the principal or bring him or her and third parties into contractual relationship. This branch of law separates and regulates the relationships between:

    • agents and principals (internal relationship), known as the principal-agent relationship;
    • agents and the third parties with whom they deal on their principals' behalf (external relationship); and
    • principals and the third parties when the agents deal.
    ['eɪdʒənt]['eɪdʒ(ə)nt]
    • n.经纪人;(演员、音乐家、运动员、作家等的)代理人
    • 网络代理商;代理专区;智能体
    1.
    a person or business authorized to act on another's behalf:
    Our agent in Hong Kong will ship the merchandise. A best-selling author needs a good agent.

    Proxy Agent 区别

    http://www.cnblogs.com/Enjoymyprocess/archive/2010/04/11/1709724.html

    Proxy -- 中介 -- 通道

    Agent -- 代理 -- 具有行为能力的实体, 被授权,完全代表。

    Proxy物理上至少部分存在客户端,只有胖客户端和瘦客户端之分,如发Email,装个Foxmail就是胖Proxy,登录mail.163.com/mail.qq.com就是瘦客户端,这没什么本质区别,总之Proxy代理了用户向Agent发出了请求,它是渠道,是工具,直接面向用户。

    Agent相对Proxy来说,更有了实体气质。在英文里面,一个坐席员,一个投诉处理专员,一个销售人员,都可以称为Agent,他是代表一个大实体和客户打交道的小实体,做为一种实体,它是可以资源化的,数量有上限,有生命周期,比如每个公司的客服部每个班次只有那么多员工,这批员工根据不同的技能分组,不同组别的员工处理不同的业务投诉、咨询,受理,他们通过不同的渠道(用户通过Proxy投递信息的方式)如IVR,邮件,网站收到用户的信息,并开始(注意,是“开始”)处理。

    Proxy 和 Delegate 区别

    http://www.cnblogs.com/x3d/archive/2012/06/27/2566324.html

    代理(Proxy)是名词,委派(Delegate)是动词,其次代理说明了若干个对象实现了一个共同的接口,而委派只是说明一个对象引用了另一个对象,并不牵扯接口。

    Delegate DEMO

    http://groovy-lang.org/design-patterns.html#_delegation_pattern

    The Delegation Pattern is a technique where an object’s behavior (public methods) is implemented by delegating responsibility to one or more associated objects.

    class Person {
        def name
        @Delegate MortgageLender mortgageLender = new MortgageLender()
    }
    
    class MortgageLender {
        def borrowAmount(amount) {
           "borrow \$$amount"
        }
        def borrowFor(thing) {
           "buy $thing"
        }
    }
    
    def p = new Person()
    
    assert "buy present" == p.borrowFor('present')
    assert "borrow \$50" == p.borrowAmount(50)

    Proxy DEMO

    http://groovy-lang.org/design-patterns.html#_proxy_pattern

    The Proxy Pattern allows one object to act as a pretend replacement for some other object. In general, whoever is using the proxy, doesn’t realise that they are not using the real thing. The pattern is useful when the real object is hard to create or use: it may exist over a network connection, or be a large object in memory, or be a file, database or some other resource that is expensive or impossible to duplicate.

    1.13.1. Example

    One common use of the proxy pattern is when talking to remote objects in a different JVM. Here is the client code for creating a proxy that talks via sockets to a server object as well as an example usage:

    class AccumulatorProxy {
        def accumulate(args) {
            def result
            def s = new Socket("localhost", 54321)
            s.withObjectStreams { ois, oos ->
                oos << args
                result = ois.readObject()
            }
            s.close()
            return result
        }
    }
    
    println new AccumulatorProxy().accumulate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    // => 55

    Here is what your server code might look like (start this first):

    class Accumulator {
        def accumulate(args) {
            args.inject(0) { total, arg -> total += arg }
        }
    }
    
    def port = 54321
    def accumulator = new Accumulator()
    def server = new ServerSocket(port)
    println "Starting server on port $port"
    while(true) {
        server.accept() { socket ->
            socket.withObjectStreams { ois, oos ->
                def args = ois.readObject()
                oos << accumulator.accumulate(args)
            }
        }
    }
  • 相关阅读:
    hdu 1022 Train Problem I 模拟
    Eclipse plugin开发 —实现语法高亮
    一篇英文版的eclipse插件textEditor资料
    (转)Eclipse常用快捷键
    Eclipse plugin开发 —实现单行与多行注释
    Eclipse plugin开发 —实现文本内容帮助
    CSS borderstyle
    ClassLoader getResource
    javascript add table content from xml
    postgres 当前时间
  • 原文地址:https://www.cnblogs.com/lightsong/p/8655106.html
Copyright © 2020-2023  润新知