zoukankan      html  css  js  c++  java
  • (Object-C)学习笔记 --OC的懒加载和单例方法

    OC的懒加载

      什么是懒加载:

        懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.

        注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化。

      懒加载的好处

        (1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

        (2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

      懒加载的例子:

     1 #import "MusicTableViewController.h"
     2 #import "TRMusicGroup.h"
     3 
     4 @interface MusicTableViewController ()
     5 
     6 @property(nonatomic,strong)TRMusicGroup *group;
     7 
     8 @end
     9 
    10 
    11 @implementation MusicTableViewController
    12 
    13 - (TRMusicGroup *)group
    14 {
    15     if (!_group) {
    16         _group = [TRMusicGroup fakeData][0];
    17     }
    18     return _group;
    19 }
     

    OC的单例方法

      单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。
      单例模式可能是OC当中用的最多的一种模式。

    实现单例模式有三个条件

    1、类的构造方法是私有的

    2、类提供一个类方法用于产生对象

    3、类中有一个私有的自己对象

    针对于这三个条件,OC中都是可以做到的

    1、类的构造方法是私有的

    我们只需要重写allocWithZone方法,让初始化操作只执行一次

    2、类提供一个类方法产生对象

    这个可以直接定义一个类方法

    3、类中有一个私有的自己对象

    我们可以在.m文件中定义一个属性即可

    看一下普通方法写单例:

    TRStudent.h

    1 #import <Foundation/Foundation.h>
    2 
    3 @interface TRStudent : NSObject
    4 //属性
    5 @property (nonatomic, strong) NSArray *stuArray;
    6 //单例方式一
    7 + (id)sharedStudent;
    8 @end
    TRStudent.m
     1 #import "TRStudent.h"
     2 
     3 @implementation TRStudent
     4 
     5 static TRStudent *_student = nil;
     6 + (id)sharedStudent {
     7     if (!_student) {
     8         //初始化实例对象Instance Object
     9         _student = [[TRStudent alloc] init];
    10     }
    11     return _student;
    12 }
    13 
    14 - (NSArray *)stuArray {
    15     if (!_stuArray) {
    16         _stuArray = @[@"shirley", @"bob"];
    17     }
    18     return _stuArray;
    19 }
    20 
    21 //重写alloc方法,完善单例的创建
    22 + (instancetype)alloc {
    23     //父类的alloc方法/返回一个单例对象
    24     if (!_student) {
    25         _student = [super alloc];
    26     }
    27     return _student;
    28 }
    29 //有的书会重写这个方法
    30 //+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    31 //    
    32 //}
    33 
    34 @end
    15年之后大部分都用GCD来写单例:

    TRStuden.h

    1 #import <Foundation/Foundation.h>
    2 
    3 @interface TRStudent : NSObject
    4 
    5 //使用gcd一次性任务创建单例
    6 + (id)sharedStudentByGCD;
    7 
    8 @end
    TRStuden.m
     1 #import "TRStudent.h"
     2 
     3 @implementation TRStudent
     4 
     5 static TRStudent *_studentByGCD = nil;
     6 + (id)sharedStudentByGCD {
     7 
     8     static dispatch_once_t onceToken;
     9     dispatch_once(&onceToken, ^{
    10         _studentByGCD = [[TRStudent alloc] init];
    11     });
    12     
    13     return _studentByGCD;
    14 }
    15 
    16 //重写alloc方法
    17 + (instancetype)alloc {
    18     static dispatch_once_t onceToken;
    19     dispatch_once(&onceToken, ^{
    20         _studentByGCD = [super alloc];
    21     });
    22     
    23     return _studentByGCD;
    24 }
    25 
    26 @end
    总之单例的写法就是这样了,类方法里有一个自己的私有对象。
    We're here to put a dent in the universe!code改变世界!-----------------------------------------------------by.不装b就无法活下去的晨曦
  • 相关阅读:
    数组的处理
    bootstrap 列的排序
    bootstrap 偏移
    mysql数据库创建、删除数据库
    Spring 加载配置文件的方式
    MyBatis DAO层传递参数到mapping.xml 几种方式
    Python 正则表达式
    Python 之定时器
    Python学习
    获取鼠标经过位置的X、Y坐标
  • 原文地址:https://www.cnblogs.com/firstaurora/p/5185827.html
Copyright © 2011-2022 走看看