OC学习日记014-单例模式和委托模式

本文介绍了OC中的单例模式和委托模式。单例模式用于确保类只有一个实例并提供全局访问点,通过示例展示了如何创建和使用单例。委托模式则是一种设计模式,通过定义协议和委托对象,实现对象间的通信和行为传递,文中以房东委托中介卖房为例进行了详细解释。
摘要由CSDN通过智能技术生成

设计模式(用来解决某一特定问题的):
观察者模式|单例模式|委托模式|工厂模式

单例模式:

什么时候使用单例模式?

 在一个工程中,一些类只需要一个实例变量的时候,我们就可以将这些类设计成单例模式

单例模式的作用?

 当一个‘类A’被设计成单例模式时,由‘类A’构造出的实例对象至于其他类来讲为全局实例对象,即在每一个类中由‘A’构造出的实例对象,都为相同的对象。
 单例模式的实现思路:一个类只能创建一个实例和一个获得该实例的方法
 在OC中如何将一个类设计成单例模式?
 1.在要被设计成单例的类的.h文件中声明一个构造单例方法
 2.实现该方法
新建Student类,在Student.h文件中 声明一个shareInstance方法:
 @interface Student : NSObject<NSCopying>
+(Student *)shareInstance;
@end
在Student.m文件中我们要首先声明一个静态实例对象,然后实现单例方法:
//声明一个静态的实例对象,只会在第一次执行
static Student *st=nil;

@implementation Student
//实现该方法
+(Student *)shareInstance{
    if (st == nil) {
        st =[[Student alloc]init];
    }
    return st;
}
//为了防止alloc 或 new 创建新的实例变量
+(id)allocWithZone:(struct _NSZone *)zone{
    /*
    创建一个互斥锁,保证此时没有其他线程对self对象进行修改,起到线程保护作用。
     */
    @synchronized (self) {
        if (st == nil) {
            st=[super allocWithZone:zone];
        }
    }
    return st;
}
//为了防止copy产生新的对象,需要遵循NSCopying

-(id)copyWithZone:(NSZone *)zone{
    return self;
}
@end
最后我们在主函数中测试建立不同的Student类的对象,我们会发现,它们尽管名字是不同的,它们实际上指针指向的地址还是一样的:
    Student *student1=[Student shareInstance];
    Student *student2=[[Student alloc]init];
    Student *student3=[Student new];    
    Student *student4=[student3 copy];    NSLog(@"%p,%p,%p,%p",student1,student2,student3,student4);

委托模式

委托模式其实我们在上一篇文章协议也是有提到的,其实我们可以把它理解为协议的一种使用形式。

概念:

 两个对象间不能够直接联系需要通过一个第三方对象,帮助他们联系,这一种模式,我们称之为‘委托模式’。

例子:

下面我们以房东委托中介卖房这种形式去说明一下:
我们先把他们之间的关系列出:

房东-委托->‘中介’-卖房->‘消费者’

这里写图片描述
LandLord *landLord=[[LandLord alloc]init];
HouseSaler *houseSaler=[[HouseSaler alloc]init];
landLord.delegate=houseSaler;
houseSaler.landlord=landLord;
[landLord registerHouse];
@protocol LandLord_Protocol
-(void)mustSalerhHouse;
-(void)payMoney;
@end

import

import “LandLord_Protocol.h”

@interface LandLord : NSObject
//这里使用assign或者weak是为了防止循环引用
@property (nonatomic,assign)id delegate;
-(void)registerHouse;
//新增方法,表示房东收到钱
-(void)receiveMoney;
@end

import “LandLord.h”

@implementation LandLord

-(void)registerHouse{
NSLog(@”我是房东,我已经把房子登记到中介处”);
//如果委托存在,并且遵循制定协议
if (self.delegate && [self.delegate conformsToProtocol:@protocol(LandLord_Protocol)]) {
[self.delegate mustSalerhHouse];
[self.delegate payMoney];
}
}

-(void)receiveMoney{
NSLog(@”我是房东,我收到钱了”);
}
@end

import

import “LandLord_Protocol.h”

import “LandLord.h”

@interface HouseSaler : NSObject
@property (nonatomic,strong)LandLord *landlord;
@end

import “HouseSaler.h”

@implementation HouseSaler

-(void)mustSalerhHouse{
NSLog(@”我是中介,我跟房东签订了协议,我必须给房东卖房”);
}

-(void)payMoney{
NSLog(@”我是中介,我卖掉房子了,我抽了佣金,剩下的钱给你”);
[self.landlord receiveMoney];
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值