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)