探索iOS以及OSX程序模板中的AppDelegate

By | 2014/05/31

初学者学习iOS的时候或者OSX的时候模板给出一个AppDelegate,初学者会感到莫名其妙,网上搜一把资料,某个接口是干什么用的,我实现这个功能就行了.Delegate这个需要理解清楚它,我们为什么使用Delegate,Delegate做什么用的?

首先帮助大家理解Delegate这个设计模式,iOS就是利用Delegate来实现多态,如果你理解了,那这篇文章可以跳过去不看了.

假设你有一个机器,机器有很多功能,其中有一个摇杆装置,它可以向上扭.你一扭它可以显示这个机器运作的时间.现在需要新需求,我需要它一扭就可以显示,钟楼厕所哪有空位.所以直接就把那个摇杆装置背后的实现换一下,就可以了这就是Delegate.然后扩展…

程序有一个切入点,就相当于那个摇杆,这个切入点有很多接口,程序开始,数据准备完毕,结束前,等等.所有可能的状态都添加到这一接口里面.我们发现为什么iOS程序我们不需要编写那个main方法,直接实现这个切入点就行了(就是那个代理!).于是整个程序就围绕代理展开实现,成了iOS程序的起点.被代理的对象,也就是主程序一直在背后,你看不到它,也不需要看到它,但是却实现了App的多态!

下面拿代码来.

代码我不会贴,麻烦会的同学帮我改一下,如果上边没有看懂,请查阅Delegate设计模式去理解这段话,配合源码理解

整个源码分为四个部分我会按顺序去贴

main – Application – ApplicationDelegate接口 – ApplicationDelegate接口实现

#include <cstdio>
#include <cstdlib>
#include “include/NSApplication.h”
#include “include/AppDelegate.h”

int main()
{
AppDelegate * pAppDelegate = new AppDelegate();
NSApplication &app = NSApplication::GetInstance();
app.SetDelegate(pAppDelegate);
app.Run();
return 0;
}

#ifndef NSAPPLICATION_H
#define NSAPPLICATION_H

struct IApplicationDelegate;

class NSApplication
{
public:
static NSApplication & GetInstance();
void SetDelegate(IApplicationDelegate * pDelegate);
void Run();
virtual ~NSApplication();
protected:
private:
NSApplication();
NSApplication(const NSApplication& other);
private:
char * m_pResource;
IApplicationDelegate * m_pDelegate;
};

#endif // NSAPPLICATION_H

#include “../include/NSApplication.h”
#include “../include/IApplicationDelegate.h”
#include <cstdio>

void NSApplication::SetDelegate(IApplicationDelegate * pDelegate)
{
m_pDelegate = pDelegate;
}

NSApplication::~NSApplication()
{
if(m_pDelegate)
{
m_pDelegate->ApplicationWillTerminate();
}
if(m_pResource)
{
delete []m_pResource;
}
if(m_pDelegate)
{
m_pDelegate->ApplicationDidEnd();
}
}

void NSApplication::Run()
{
this->m_pResource = new char[32];
if(m_pDelegate)
{
m_pDelegate->ApplicationOnStart();
}
}

NSApplication::NSApplication()
{
//ctor
this->m_pDelegate = NULL;
this->m_pResource = NULL;
}

NSApplication::NSApplication(const NSApplication& other)
{
//copy ctor
}

NSApplication & NSApplication::GetInstance()
{
static NSApplication app;
return app;
}

#ifndef IAPPLICATIONDELEGATE_H
#define IAPPLICATIONDELEGATE_H
struct IApplicationDelegate
{
virtual void ApplicationOnStart() = 0;
virtual void ApplicationWillTerminate() = 0;
virtual void ApplicationDidEnd() = 0;
};

#endif // IAPPLICATIONDELEGATE_H

#ifndef APPDELEGATE_H
#define APPDELEGATE_H
#include “IApplicationDelegate.h”

struct AppDelegate :public IApplicationDelegate
{
char startup_info[32];
char will_terminate_info[32];
char did_terminate_info[32];
AppDelegate();
virtual void ApplicationOnStart();
virtual void ApplicationWillTerminate();
virtual void ApplicationDidEnd();
~AppDelegate();
};

#endif // APPDELEGATE_H

#include “AppDelegate.h”
#include <cstring>
#include <cstdio>

AppDelegate::AppDelegate()
{
strcpy(startup_info,”Hello,World!\n”);
strcpy(will_terminate_info,”HeHe…\n”);
strcpy(did_terminate_info,”ByeBye!\n”);
}

void AppDelegate::ApplicationWillTerminate()
{
printf(“%s”,will_terminate_info);
}

void AppDelegate::ApplicationDidEnd()
{
printf(“%s”,did_terminate_info);
delete this;
}

void AppDelegate::ApplicationOnStart()
{
printf(“%s”,startup_info);
}

AppDelegate::~AppDelegate()
{
//dtor
}

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据