Swift delegate/proxy design mode

Source: Internet
Author: User

The delegation/proxy mode in Swift (hereinafter referred to as "proxy mode") is basically consistent with the OBJECT-C proxy model.

The basic idea of proxy mode is to give (entrust) another work that I need to do (such as a class or struct, etc.) to be dealt with by someone I have stipulated for the ability to do the job (objects of the required type).

The implementation of the proxy mode needs to be based on the protocol that we talked about in our previous article. We define a protocol to encapsulate the specified methods (the ability to do this), so that the classes that implement these protocols have these methods, and we can delegate the work to him to handle.

For example, I want to rent, first of all I have the ability to rent, but I do not have the time or other reasons to do this matter, this time I have to find a rental capacity of the people or institutions to help me rent. For example, I found Maya house and asked them to help me rent a rental. I just need to tell them what position I want, how big, what type, price range, etc., they can help me to rent. In this example I am a class to delegate, and the object I delegate has some requirements (that is, the ability to do the work as defined): To find a house as required. Maya House is the subject of acceptance, because he has the ability to meet my requirements. If Zhang San also have the ability to find the house I asked for, then Zhang San can also be used as my entrusted object to find a house, as far as I entrust to who can, as long as he satisfies the ability to find a house.


We use examples to achieve:

First, I need to define the protocol, to state my request, to have the ability to find a house.

Protocol rentprotocol{    //agreement content        //have a request for a house    func renting ()}

And then we're defining a Mayan house, a Zhang San, to let them all achieve this agreement

Class Mayahouse:rentprotocol {    func renting () {        println ("I am Maya House, I can find a house")}    }

Class Zhangsan:rentprotocol {    func renting () {        println ("I am Zhang San, I can find a house")}    }

Then we define one of my classes, I have a property, rentdelegate he needs to have the ability to find a house, and then I'm going to entrust him to find a house renting ()

Class myhouse{        var rentdelegate:rentprotocol?    }

Then we create a new Myhouse object and assign him the Maya House object and try to find a house.


        var maya=mayahouse ()        var Zhangsan=zhangsan ()


        var maya=mayahouse ()        var zhangsan=zhangsan ()                var myhouse=myhouse ()        Myhouse.rentdelegate=maya// We'll find out whether the assignment is Maya, or Zhangsan we're looking for a house, it's called the following line of methods, and this assignment is irrelevant        myhouse.rentdelegate?. Renting ()

We found that whether we were Maya or Zhangsan assigned to Myhouse (that is, we entrust Maya or Zhangsan) we can call rentdelegate.renting to find a house.

So we find that no matter who we entrust to, as long as he has the ability to find a house, and do not need to worry about how they find the house (that is, they have to implement the proxy renging () method, the specific method of implementation of the function we do not need to tube)


Let's try to find a house of our own function and not according to our requirements, that is, no implementation of the agreement assignment to Rentdelegate try

Class Lisi {    func renting () {                println ("I am John Doe, I can find a house")}    }

        var lisi=lisi ()                var myhouse=myhouse ()        myhouse.rentdelegate=lisi        myhouse.rentdelegate?. Renting ()

We will find that the compilation does not pass, Lisi is not the type we need

So it is important for the entrusted object to fulfill our stipulated agreement before we can


Okay, there's a problem. Welcome to Exchange

Apple Development Group: 414319235 Welcome to join the Welcome discussion question


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift delegate/proxy design mode

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.