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)
}
}
}
Groovy 设计模式 -- proxy & delegate的更多相关文章
- Android的Proxy/Delegate Application框架 (主要介绍插件化开发)
1. 插件化的原理 是 Java ClassLoader 的原理:Java ClassLoader基础 常用的其他解决方法还包括:Google Multidex,用 H5 代替部分逻辑,删无用代码,买 ...
- Android的Proxy/Delegate Application框架
转自:http://blogs.360.cn/360mobile/2013/11/25/proxydelegate-application/#comment-77 有的时候,为了实现一些特殊需求,如界 ...
- php设计模式 Proxy (代理模式)
代理,指的就是一个角色代表另一个角色采取行动,就象生活中,一个红酒厂商,是不会直接把红酒零售客户的,都是通过代理来完成他的销售业务.而客户,也不用为了喝红酒而到处找工厂,他只要找到厂商在当地的代理就行 ...
- 设计模式——proxy代理模式
目录 概述 定义 角色 为什么会有代理模式? 应用场景 示例 静态代理 例子 动态代理 JDK中生成代理对象的API 代码示例: 代码示例2 Cglib代理 代码示例 AOP(AspectOrient ...
- 设计模式--Proxy
转自:http://blog.csdn.net/dan_xp/article/details/1820852 最近一直在看java的设计模式 ,感觉印象最深刻的就是"面向接口编程" ...
- 巧用代理设计模式(Proxy Design Pattern)改善前端图片加载体验
这篇文章介绍一种使用代理设计模式(Proxy Design Pattern)的方法来改善您的前端应用里图片加载的体验. 假设我们的应用里需要显示一张尺寸很大的图片,位于远端服务器.我们用一些前端框架的 ...
- Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理
面试问题:Java里的代理设计模式(Proxy Design Pattern)一共有几种实现方式?这个题目很像孔乙己问"茴香豆的茴字有哪几种写法?" 所谓代理模式,是指客户端(Cl ...
- 设计模式-Proxy(结构型模式)
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Proxy.h #pragma once class Subject { public: virtual ~Subject ...
- 设计模式--Proxy模式
这篇主要介绍代理模式相关内容,主要是一些基本概念普及. 代理模式 1.什么是代理模式? 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.[DP] 通俗的说就是指客户端并不直接调用 ...
随机推荐
- 「SCOI2016」妖怪 解题报告
「SCOI2016」妖怪 玄妙...盲猜一个结论,然后过了,事后一证,然后假了,数据真水 首先要最小化 \[ \max_{i=1}^n (1+k)x_i+(1+\frac{1}{k})y_i \] \ ...
- bzoj4540 序列 (单调栈+莫队+rmq)
首先,如果我知道[l,r],要转移到[l,r+1]的时候,就是加上以r+1为右端点,[l,r+1]为左端点的区间的最小值,其他情况和这个类似 考虑这玩意怎么求 右端点固定的话,我左端点越往左走,这个最 ...
- 关于Autosar中DCM(14229UDS)模块的理解
阅读本篇文章希望达到的目的是: UDS是干什么的, ISO14229是如何定义规则的, 希望接下来的阅读让你不虚此行. 1. UDS是干什么的?UDS全称是Unified Diagnostic Ser ...
- 洛谷P3265 装备购买
这个大毒瘤题....居然反向卡精度.... 别的题eps要开小,这个毒瘤要开大... 我一开始是1e-12,挂的奇惨无比,50分...... 然后改成1e-7,就70分了... 1e-5 90分 1e ...
- snmpwalk
什么是snmpwalk?snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户. snmpwalk的作用 ...
- SetCapture() & ReleaseCapture() 捕获窗口外的【松开左键事件】: WM_LBUTTONUP
今天在窗口上绘图的时候,遇到一个问题:在特殊情况下,当用户在窗口中按下鼠标左键,然后移动到窗口外松开鼠标左键,这时程序中只能捕获到 WM_LBUTTONDOWN(按下) 和 WM_MOUSEMOVE( ...
- HTTPS笔记:使用 SSLEngine 为 aioserver 服务器提供 SSL 访问支持
现在 HTTPS 的普及率是越来越高,闲来无事,花了二三天时间,为五年前写的 aioserver 服务器提供了 SSL 访问支持. 查看网上资料,为了提高服务器的高并发,建议使用:SSLEngine ...
- How MVC pattern Flows
以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)
- 网上找的Backbone.js
// Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...
- 将分支代码合并到master和将master代码合并到dev
两种合并分支的方法: 都保证在合到的那个分支上面:A合并到B,即保证当前在B分支上. A merge B是把A中的改动放到B分支上,B merge A是把B中的改动merge到A中,例如把master ...