C + + simulation implements OBJECTIVE-C protocol and proxy mode

Source: Internet
Author: User



Objective-c's protocols and proxies are one of the most widely used features, and can be said to be ubiquitous in the development of the Apple series. In fact, many people do not know the principle behind it. In fact, simply put, this is the classic use of proxy patterns in design patterns. The simple point of Proxy mode is to provide an agent for other objects to control access to this object, and in OC the image point is that if a completes a thing, but he can not complete, so he find a agent B for him to complete this matter, they have an agreement (protocol), b inherit the protocol to accomplish the thing a agent gave him.



For example: There is a baby, he himself does not eat and bathe and so on some things, so the baby has a nanny, so the baby and the nanny agreed to a deal, the agreement that the nanny needs to do something, and the nanny is the agent, namely: Baby and Nanny There is an agreement between the nanny, the agreement, The nanny would then need to implement the terms of the agreement as an agent.



So back to Objective-c, for a very simple example, TableView is one of the most used controls, and its usage is believed to be clear for every child who develops iOS. In fact, this is also a proxy commissioned by the classic application.



Initialize the TableView control first, and specify a good proxy and delegate


_tableview = [[UITableView alloc] init];
[_tableview setDelegate: self];
[_tableview setDataSource: self];
[self.view addSubview: _tableview];

_data = [[NSMutableArray alloc] initWithObjects: @ "China", @ "America", @ "UK", nil];


When you really want to show the data to the interface, you just need to overload the function below.



-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath



As for why it is called to tableview this function, the developer does not care at all. And get data in the personal and so on, these do not need to do. Back to the original example, in fact, TableView is quite a baby, he would not put the data on the interface, so he found a nanny agent, is the main interface. The main interface inherits and implements this Protocol (in fact the protocol is the TableView function) to play the whole series of functions.



If you use C + + to emulate the implementation, its simple code can look like this:


// proxyproj.cpp: defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>

class NSObject
{
public:
    virtual void callfun () {}
    virtual void tableview (int nShow) ()
    NSObject * delegate;
};

DWORD WINAPI UIViewControlerThreadProc (LPVOID lpParam)
{
    // do something
    // ......

    NSObject * pSelf = (NSObject *) lpParam;
    pSelf-> delegate-> tableview (100);
    return 0;
}

DWORD WINAPI proxyThreadProc (LPVOID lpParam)
{
    // do something
    // ......

    NSObject * pSelf = (NSObject *) lpParam;
    pSelf-> callfun ();
    return 0;
}

class proxy: public NSObject
{
public:
    virtual void callfun ()
    {
        CloseHandle (CreateThread (NULL, 0, proxyThreadProc, this, 0, NULL));
    }
};

class TableView: public proxy
{


};

class UIViewControler: public proxy
{
public:
    void init ()
    {
        TableView * tabView = new TableView;
        tabView-> delegate = this;
        CloseHandle (CreateThread (NULL, 0, UIViewControlerThreadProc, tabView, 0, NULL));
    }
    void tableview (int nShow) {printf ("Show Number% d \ n", nShow);}
};

int _tmain (int argc, _TCHAR * argv [])
{
    UIViewControler ui;
    ui.init ();
    getchar ();
    return 0;
} 





Uiviewcontroler is equivalent to the main interface, and INIT is the initialization process. In which the proxy is initialized for TableView, and then through the proxy callback to the inside of the TableView function. As the main interface side, only need to overload the TableView function, there is no need to care about the internal implementation.



C + + emulation implements OBJECTIVE-C protocol and proxy mode


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.