惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Know Your Adversary
Know Your Adversary
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
博客园 - Franky
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Hacker News
The Hacker News
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
雷峰网
雷峰网
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Webroot Blog
Webroot Blog
L
LangChain Blog
C
Check Point Blog
N
News | PayPal Newsroom
L
LINUX DO - 热门话题
T
Tor Project blog
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog
S
Security Affairs
Schneier on Security
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Security Latest
Security Latest
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
N
Netflix TechBlog - Medium
Scott Helme
Scott Helme
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
人人都是产品经理
人人都是产品经理
W
WeLiveSecurity

博客园 - peter he

LPCTSTR c++ 资源文件操作 - peter he Unicode-enabling Microsoft C/C++ Source Code big-endian和little-endian HeapAlloc和GlobalAlloc以及VirtualAlloc三者之间的关系 电脑运行中死机解决方案<经典> The most effiective core chinese journals Some ideas 电池缩水 操作系统缩水 显卡缩水(时钟频率、显存大小、显卡接口、显存位宽) 非Firefox浏览器检测组件 个人习惯培养计划 房价和肉价反应的真实社会 C++ Review Series 答辩开始 洗心革面, 重新开始 从“老公”的称呼来历,看男人地位的变迁 嵌入式系统 Boot Loader 技术内幕
The difference between Delegate and Composite
peter he · 2007-10-13 · via 博客园 - peter he
  • Letme state a theory: polymorphic behavior is always delegation of some sort, and thus delegation (to impliment polymorphism) always has a layer of indirection. Please find a problem here, so we can make this more succinct/correct, or dis/prove it.

    Lets also try to say what delegation is NOT.

    From my above theory, a simple subroutine may have responsibility for the computation of some part of a program, but a subroutine call is not delegation because it is direct. It lacks a layer of indirection. A function-pointer IS however, since it can point to any function, and the decision of what function it points to must already have been made when it is dereferenced and called. Even a switch/case statement can be delegation, provided it adds a layer of indirection.

    So what does OO and polymorphic behavior have to do with delegation, if delegation is such a simple, ubiquitous thing? Perhaps this is the real core of the matter. OO is about resolving polymorphic behavior by delegating to objects.

  • Betteryet: delegation is done by the caller, while indirection is a function of (dispatch to) the callee
    [/list:o]

    按照上面的定义,在delegate和delegatee之间必须有一层indirection。画个简单的示意图:

    代码

    1. 1:   
    2. A --> B   
    3. 2:   
    4. delegate -----> delegatee   
    5.      ^                  |   
    6.      |   indirection    |   
    7.      --------------------   

    代码

    1. part1   
    2. delegateA {   
    3.    delegateeB b;   
    4. void methodA() { b.methodB();}   
    5. }   
    6. delegateeB {   
    7. void methodB() {}   
    8. }   

    上面的代码就不是delegation,而是composition,因为直接调用了delegatee的方法,这只是forwarding methods。

    代码

    1. part2   
    2. delegateA {   
    3.     delegateeB b;   
    4. void methodA() { b.methodB(this); }      
    5. void do() {}   
    6. }   
    7. delegateeB {   
    8. void methodB(delegateA a) { a.do(); }   
    9. }   

    part2好像又回到了part1,其实不然,delegate已经把自身pass给了delegatee,并且delegatee调用了delegate的方法,这就是一种indirection。

    在现实世界中,假如董事长A把权利授权给总经理B,总经理B一定会获取董事长A才拥有的权利,它会利用这些权利来替公司做事。这才是真正的delegation,也就是说delegatee一定会调用delegate的某些方法,因此你首先得把delegate传递给delegatee。