OC中的单例模式和委托模式

    单例模式
    **什么时候使用单例模式:**
    在一个工程中,一些类只需要一个实例变量,我们就可以将这些类设计成单例模式
    **单例模式的作用?**
        当一个'类A'被设计成单例模式时,有'类A'构造出的实例对象之于其他类来讲为全局实例对象.即在每一个类中由'A'构造出的实例对象,都未相同的对象.
    **在OC中如何将一个类设计成单例模式**
    1.在要被设计成单例的类的.h文件中声明一个构造单例方法
    2.实现该方法

创建个Student 类

在Student.h 文件中给个方法:

#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCopying>
+(Student *)shareInstance;
end

在Student.m 文件


#import "Student.h"
//声明一个静态的实例对象,只会在第一次执行
static Student *st = nil;
@implementation Student
//实现该方法
+(Student *)shareInstance{
    //第一次进来是nil,
    if (st == nil) {
        st = [[Student alloc]init];
    }
    return st;    
}

//为了防止alloc 或new 创建新的实例变量
+(id)allocWithZone:(struct _NSZone *)zone{
    /**
     *  @synchronized的作用创建一个互斥锁,保证此时没有其他线程对self对象进行修改,起到线程保护作用.
     */
    @synchronized (self) {
        if (st ==nil) {
            st  =[super allocWithZone:zone];
        }
        return st;
    }
}

//为了防止copy产生新的对象,需要遵循NSCopying
-(id)copyWithZone:(NSZone *)zone{
    return self;
}
@end

回到控制器

    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);

这就是单例模式..


~~~创个新的工程.
委托模式:
两个对象间不能够直接联系,需要通过一个第三方对象,帮助它们联系,这一种模式,我们称之为’委托模式’.
例如:’房东’ –’委托’—>’中介’–’卖方’–>’消费者’

创建2个类 房东类(Landlord)和中介类(HouseSaier).和创建一个中介的委托.

在委托中给两个方法;

#import <Foundation/Foundation.h>
@protocol LandlordProtocol <NSObject>
-(void)mustSalehHouse;//卖房的方法
-(void)payMoney;    //给钱的方法
@end

在HouseSaier.h文件.导入需要的头文件和委托

#import <Foundation/Foundation.h>
#import "LandlordProtocol.h"
#import "Landlord.h"
@interface HouseSaier : NSObject<LandlordProtocol>
@property(nonatomic,strong)Landlord *landlord;
@end

在HouseSaier.m文件实现

#import "HouseSaier.h"
@implementation HouseSaier
-(void)mustSalehHouse{
      NSLog(@"我是中介,我跟房东签订了协议,我必须给房东卖房");
}
-(void)payMoney{
    NSLog(@"我是中介,我卖掉房子了,我抽了佣金,剩下的钱给你");
    [self.landlord receiveMoney];
}
@end

回到Landlord.h文件中

#import <Foundation/Foundation.h>
#import "LandlordProtocol.h"
@interface Landlord : NSObject
//这里使用assign 或者 weak 是为了防止循环引用
@property(nonatomic,assign)id<LandlordProtocol> delegate;
-(void)registerHouse;
//表示房东收到钱了
-(void)receiveMoney;
@end

Landlord.m文件

#import "Landlord.h"
@implementation Landlord
-(void)registerHouse{
    NSLog(@"我是房东,我已经将房子登记到中介处");
    //如果委托存在.并且遵循知道协议
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(LandlordProtocol)]) {
        //执行卖房方法
        [self.delegate mustSalehHouse];
        //卖了房吧钱给房东方法
        [self.delegate payMoney];
    }
}
-(void)receiveMoney{
    NSLog(@"我是房东,我收到钱了");
}
@end

所有的方法都写完了就去控制器实现吧~~

#import "ViewController.h"
#import "Landlord.h"
#import "HouseSaier.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    Landlord *landlord =[[Landlord alloc]init];
    HouseSaier *houseSaier =[HouseSaier new];
    landlord.delegate = houseSaier;
    houseSaier.landlord =landlord;
    [landlord registerHouse];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值