Swift language Essentials-talking about proxy mode (Delegate)

Source: Internet
Author: User

In iOS programming, we often talk about proxy agents, which is delegate, so what is a proxy?

Let's take a look at Cocoa's description of it:

Delegation is Cocoa's term for passing off some responsibilities of a object to another

Name implies:

An agent is the transfer of responsibility from one object to another.

In fact, if you have written a Java or C # program friends should know that we are in the design of the use of the interface and combination, the same truth.

Take a look at the following code:

First, we define a protocol

Protocol Savemediadelegate {    func save ()}

Then, we combine this protocol into our business logic class.

class MyComputer {    delegate : Savemediadelegate?         Func Saveinfo () {      //  Check to see if the delegate are there, then call it    DELEGATE? . Save ()    }}  

Then, we implement different forms of the Protocol

class savetodisk:savemediadelegate {    func save () {        println ("saving to disk ... "     }}}class  savetousb:savemediadelegate {    func save () {        println ("  saving to usb ... " )    }}

Then we inject our protocol implementation class into our business Class (Di-dependency injection)

Let MyComputer =//  does= savetodisk () myComputer. delegate =//  prints "Saving to disk ..."= savetousb () myComputer. delegate =//  prints "Saving to usb ..."

It's simple, it's that simple!

Swift language Essentials-talking about proxy mode (Delegate)

Related Article

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.