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

来源:互联网 发布:国外户外品牌 知乎 编辑:程序博客网 时间:2024/06/08 01:09

设计模式是一种为了解决某一特定问题而提出来的方案。常见的设计模式有:单例模式、委托模式、观察者模式、职责链模式等等。

单例模式

单例模式:保证一个类仅有一个实例,并提供一个访问他的全局访问点。

下面用一个例子来说明单例模式的实现思路:

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

1.新建Student类,创建产生单例的方法

@interface Student : NSObject<NSCopying>//创建产生单例的方法+ (Student *)shareInstance;

2.在Student类.m文件中实现产生单例的方法

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

3.重写alloc、copy方法

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

4.在main函数中实现

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

5.运行结果为

//4个student对象都是同一个地址,表明单例模式创建成功0x7fa8f8c23d40,0x7fa8f8c23d40,0x7fa8f8c23d40,0x7fa8f8c23d40

委托模式

有两个对象,一个需要在另一个状态发生变化时知晓或者一个对象希望在运行时让另一个对象负责确定行为变化。解决此问题的方案就是委托模式。

委托模式:为其他对象提供一种委托以控制对这个对象的访问。

 /**     *       委托模式:两个对象间不能够直接联系,需要通过一个第三方对象,帮助他们联系,这一种模式,我们称为‘委托模式’。     ‘房东’--委托-->‘中介’--卖房-->‘消费者’      */

下面举一个‘房东’–委托–>‘中介’–卖房–>‘消费者’的例子来说明委托模式的使用

要做的事情就是:房东委托中介卖房给消费者,房子卖出后,中介需要给回一定的钱给房东,房东收到钱后,做出回应。

1.Landlord_Protocol协议

#import <Foundation/Foundation.h>@protocol Landlord_Protocol <NSObject>//HouseSaler必须实现的方法- (void)mustSaleHouse;- (void)payMoney;@end

2.Landlord类

#import <Foundation/Foundation.h>#import "Landlord_Protocol.h"@interface LandLord : NSObject//使用assign 或者 weak 是为了防止循环引用@property (nonatomic,assign)id<Landlord_Protocol>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 mustSaleHouse];        [self.delegate payMoney];    }    }- (void)receiveMoney{    NSLog(@"我是房东,我收到钱了");}

3.HouseSaler类

#import <Foundation/Foundation.h>#import "Landlord_Protocol.h"#import "LandLord.h"//遵循Landlord_Protocol协议@interface HourseSaler : NSObject<Landlord_Protocol>//声明一个landlord属性@property (nonatomic,assign)LandLord *landlord;//实现文件#import "HourseSaler.h"@implementation HourseSaler- (void)mustSaleHouse{    NSLog(@"我是中介,我跟房东签订了协议,我必须给房东卖房");}- (void)payMoney{    NSLog(@"我是中介,我卖掉房子了,我抽了佣金,剩下的钱给你");    [self.landlord receiveMoney];//在主函数中还需要把  landlord 对象赋值给 self.landlord 才能调用 receiveMoney 方法}

4.主函数中实现

    LandLord *landlord = [LandLord new];    HourseSaler *houseSaler = [HourseSaler new];    landlord.delegate = houseSaler;    houseSaler.landlord = landlord;    [landlord registerHouse];

5.运行结果

2016-08-18 12:01:22.645 OC_10_02[1750:74418] 我是房东,我已经将房子登记到中介处2016-08-18 12:01:22.646 OC_10_02[1750:74418] 我是中介,我跟房东签订了协议,我必须给房东卖房2016-08-18 12:01:22.646 OC_10_02[1750:74418] 我是中介,我卖掉房子了,我抽了佣金,剩下的钱给你2016-08-18 12:01:22.646 OC_10_02[1750:74418] 我是房东,我收到钱了
1 0
原创粉丝点击