单例设计模式逐步讲解003

常用的单例类有:

cocoa Touch框架:UIApplication;NSUserDefaults;NSNotification;NSBundle;

cocoa框架:NSFileManager;NSWorkspace;NSApplication.

 

The UIApplication class provides a centralized point of control and coordination for apps running on iOS. Every app must have exactly one instance of UIApplication (or a subclass of UIApplication). When an app is launched, the UIApplicationMain function is called;当应用程序被启动后,就会唤醒UIApplicationMain的功能;UIApplicationMain among its other tasks, this function creates a singleton UIApplication object. Thereafter you access this object by invoking唤起 the sharedApplication class method.

A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches调度 action messages forwarded to it by control objects (UIControl) to the appropriate target objects. In addition, the UIApplication object maintains a list of all the windows (UIWindow objects) currently open in the app, so through those it can retrieve any of the app’s UIView objects. The app object is typically assigned a delegate, an object that the app informs of significant runtime events—for example, app launch, low-memory warnings, and app termination—giving it an opportunity to respond appropriately.

Apps can cooperatively handle a resource such as an email or an image file through the openURL: method. For example, an app opening an email URL with this method may cause the mail client to launch and display the message.

The programmatic interfaces of UIApplication allow you to manage behavior that is specific to the device. Use this object to do the following:

Control the app’s response to changes in interface orientation.

Temporarily suspend incoming touch events.

Turn proximity sensing (of the user’s face) off and on again.

Register for remote notifications.

Trigger the undo-redo UI (applicationSupportsShakeToEdit).

Determine whether an installed app can open a URL (canOpenURL:).

Extend the execution of the app so that it can finish a task in the background.

Schedule and cancel local notifications.

Coordinate the reception of remote-control events.

Perform app-level state restoration tasks.

UIApplication defines a delegate that must adopt抛弃 the UIApplicationDelegate protocol and implement部署 some of the protocol methods.

Subclassing Notes
Most apps should never need to subclass the UIApplication class. Most apps use an app delegate to manage interactions between the system and the app.

The only situation where you might need to subclass UIApplication is when you are implementing a custom event or action dispatching mechanism. In that situation, you might override the sendEvent: or sendAction:to:from:forEvent: methods to implement that mechanism. However, the cases where this is required are very rare and should be avoided whenever possible.

 

+ sharedApplication
Returns the singleton app instance.

Declaration声明
SWIFT
class func sharedApplication() -> UIApplication
OBJECTIVE-C
+ (UIApplication *)sharedApplication
Return Value返回值
The app instance is created in the UIApplicationMain function.

Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit

时间: 2024-11-08 15:06:06

单例设计模式逐步讲解003的相关文章

单例设计模式逐步讲解004

获得app实例的静态方法: 单例类: UIApplication:+sharedApplication NSUserDefaults:+standardUserDefaults NSNotificationCenter:+defaultCenter NSFileManager:+defaultManager NSBundle:+mainBundle   每次要获得单例类的实例方法的时候就要使用静态方法: UIApplication.sharedApplication NSUserDefaults

单例设计模式逐步讲解

A singleton class returns the same instance no matter how many times an application requests it. 单例设计每次只会返回相同的实例:A typical class permits callers to create as many instances of the class as they want, 请求者可以创建很多的实例只要他们想的话:whereas with a singleton class

单例设计模式逐步讲解002

A singleton class returns the same instance no matter how many times an application requests it. 单例设计每次只会返回相同的实例:A typical class permits callers to create as many instances of the class as they want, 请求者可以创建很多的实例只要他们想的话:whereas with a singleton class

IOS开发之单例设计模式

本文将从四个方面对IOS开发中的单例设计模式进行讲解: 一.什么是单例设计模式 二.我们为什么要用单例设计模式 三.单例设计模式的基本用法 四.自定义单例设计模式代码的封装 一.什么是单例设计模式 所谓单例,即是单个的实例化对象,保证一个类有且仅有一个实例.通常情况下,当我们对一个类实例化时(如:alloc.new等),并不能保证每次实例化的对象是唯一的实例.那么为了保证该类可在多次实例化的过程中保证内存地址不变,就需要引入单例设计模式. 二.我们为什么要用单例设计模式 1.Singleton

单例设计模式

一:单例设计模式的定义 单例设计模式,顾名思义,就是在整个程序运行过程中,只向外界提供一个对象,这样做可以避免资源的浪费,例如 我们打开回收站或者ppt时,只会启动一个窗口. 单例模式的java实现: 1:饿汉式 1 /** 2 * 3 */ 4 package com.hlcui.singleton; 5 6 /** 7 * @author Administrator 饿汉式单例类 8 */ 9 public class SingletonDemo { 10 // 1:内部创建一个对象 11

iOS 中的单例设计模式

单例设计模式:在它的核心结构中只包含一个被称为单例类的特殊类.例如文件管理中的NSUserDefault,应用程序中的UIApplication,整个应用程序就这一个单例类,负责应用程序的一些操作,单例在那个文件下都能取得到. 通过单例设计模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节省系统资源.如果希望在系统中某个类的对象只能存在一个,单例模式是最好的选择. 下面来点实在的,创建单例代码上 方法1:基于线程安全创建一个单例 .h做一下声明 + (id)

设计模式之单例设计模式

一.何为单例设计模式 单例模式,顾名思义就是单个实例,程序中某个类只有一个实例存在.通常实在需要共享某个资源避免资源损耗的情况下使用到的. 二.单例设计模式的代码实现 一说到单例模式的概念,我们首先会想到下面的这种的写法 public class SingleInstance { private static SingleInstance singleInstance; /** * 单例模式 * @return */ public static SingleInstance getSingleI

设计模式整理_单例设计模式

单例设计模式,它确保一个类只有一个实例,并提供一个全局访问点. 由于单例设计模式对应的类只能创建一个对象,因此它所对应的方法必须是static(不是static只有创建对象才能调用). 以下是单例模式的一个经典实现:采用了延迟加载对象的例子. public class Single1 { private static Single1 single; //利用一个静态变量来记录Single1的唯一实例,这里没有直接声明,采用了延迟加载模式. private Single1(){} //把构造器声明

【学习笔记】单例设计模式笔记

单例设计模式是常见的设计模式之一.通过单例实现所需求类在系统中只存在唯一一个实例. 单例设计模式分两种:懒汉单例设计模式和饿汉单例设计模式,两者设计思路一致,实现有微小不同. 实现代码: 1 public class HungryMan { 2 3 private HungryMan(){};//私有的构造方法保证HungryMan类无法在外部使用构造方法实例化 4 private static final HungryMan hungryMan=new HungryMan();//在类内定义一