The relationship between protocol and delegate in programming metaphor

Source: Internet
Author: User

The relationship between protocol and delegate in programming metaphor

Opening:
When you are still suffering from the traditional old-fashioned explanation, the technical jargon is confusing, the programming metaphor turned out, and hit a soy sauce back.

The end of the sale of Moe,

Protagonist

Protocol/delegate between love and hatred.

Little Story

Shoushou recent whim, registered a company Iceana, intends to engage in iOS development, thought: Anyway, now the company has no business, a computer a person enough. In this way, the code Shoushou write, Design Shou Shou engaged in, even drag to cook all have to do their own!
Important Shou Shou development of a software was recognized by the listed companies, immediately gave 5 million investment. Shoushou thought, the company to do seems to rely on a person's time is over! TM at least first recruit personal cleaning, cooking! So he conceived the next, feel direct official work too expensive, or find a part-time! posted a job posting with the following:

    1.会打扫卫生    2.会烧饭做菜    3.会发传真

Shoushou think very far, the company will be bigger, management must be done, so he decided to give this post set for the medic .... Code cleaner. In this way the recruitment released for several days, Xiao Huang came to interview, after a interview, Shoushou very satisfied: He met all the contents of the job posting. Shoushou told him: You are part-time, but I want you to clean up, when to cook, on call. What do you like to do normally, I'll call you cleaner, OK! Xiao Huang said yes. So this contract is signed!

One day Shoushou think the company is a mess, also did not say the name, "cleaner, to clean the health, sweep clean on the line, as to how to sweep casually you." Xiao Huang received a summons to clean up immediately. At noon, Shoushou hungry, inform cleaner: first cooking. Xiao Huang immediately after cooking told Shoushou finished, Shoushou said, burn the dishes. So the small yellow and busy opened. When you're done with these little yellows, you are free to do what?

And Shoushou's life is still going on.

Metaphor Correspondence relation
    • Shoushou: Class A
    • Small yellow: Class B
    • Job Posting: Protocol xxx:class{}
    • Cleaner:var weak delegate:xxx?
    • Sign contract: in class B a.delegate = Self class A self.delegate = B
    • Call Little yellow work: Self.delegate.ToZuoFan () self.delegate.ToTuoDi ()

Example code:

Class A content is indicative code is not operational:

protocol ToDoSomething:class{    func 做饭(controller:A)    func 拖地(controller:A)}class A{    var weak delegate:ToDoSomething?//也许发布了招聘启事没人呢 就是nil了    //以下两个函数满足地脏了 和要吃饭了 自动触发!    func 地脏了就要清理(){        self.delegate.拖地(self)  //self就是类A    }    func 要吃饭了就要做饭(){        self.delegate.做饭(self)  //同上    }}

Class B content schematic code:

//表示B遵循这个协议 必须要实现来证明他会class B:ToDoSometing{    //B知道A在发布招聘启事 应聘且成功了签订合同    //先取到A对象    A.delegate = self       //设定好B就是A的应聘者且签合同    func 做饭(controller:A){        //具体怎么做 怎么烧 烧怎样 B来决定    }    func 拖地(controller:A){        //具体怎么打扫为什么它来定    }}

Note:
This article is purely a blind analogy, read please forget thank you!

The relationship between protocol and delegate in programming metaphor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.