美文网首页
代理模式和通知入门

代理模式和通知入门

作者: 家丁三锅 | 来源:发表于2016-10-16 15:22 被阅读22次

代理模式

官方解释
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

翻译过来就是:
代理是一种简单而功能强大的设计模式,这种模式用于一个对象“代表”另外一个对象和程序中其他的对象进行交互。 主对象(这里指的是delegating object)中维护一个代理(delegate)的引用并且在合适的时候向这个代理发送消息。这个消息通知“代理”主对象即将处理或是已经处理完了某一个事件。这个代理可以通过更新自己或是其它对象的UI界面或是其它状态来响应主对象所发送过来的这个事件的消息。或是在某些情况下能返回一个值来影响其它即将发生的事件该如何来处理。代理的主要价值是它可以让你容易的定制各种对象的行为。注意这里的代理是个名词,它本身是一个对象,这个对象是专门代表被代理对象来和程序中其他对象打交道的。

举个栗子说就是,我想做租房子,但是不知道怎么租,所以我找房屋中介帮我租。

协议的编写规范:

1.一般情况下, 当前协议属于谁, 我们就将协议定义到谁的头文件中
2.协议的名称一般以它属于的那个类的类名开头, 后面跟上protocol或者delegate
3.协议中的方法名称一般以协议的名称protocol之前的作为开头
4.一般情况下协议中的方法会将触发该协议的对象传递出去
5.一般情况下一个类中的代理属于的名称叫做 delegate

6.当某一个类要成为另外一个类的代理的时候,一般情况下在.h中用@protocol 协议名称;告诉当前类 这是一个协议.


代码示例:

在SecViewController.h中

//第一步:定义一个协议       
@protocol SecViewControllerDelegate <NSObject>          
// 第二部:声明需要做的事的方法           
@required   //必须实现的方法          
- (void)changeBackViewColor:(UIColor *)color;          
@optional   //可选实现的方法        
- (void)hadText:(NSString *)text;         
@end
@interface SecViewController : UIViewController
// 第三步:用id类型存储改协议,命名为delegate(这里最好用weak类型)
@property (nonatomic, weak) id<SecViewControllerDelegate> delegate;
@end

在SecViewController.m文件中

- (void)back{
[self.delegate changeBackViewColor:[UIColor orangeColor]];
[self.delegate hadText:@"text-----text"];
[self.navigationController popViewControllerAnimated:YES];

}


>在ViewController.m文件中

> ```
>//继承协议
>@interface ViewController () <SecViewControllerDelegate>
>//成为代理
>SecViewController *svc = [[SecViewController alloc] init];
svc.delegate = self;
>// 实现代理方法    
> - (void)changeBackViewColor:(UIColor *)color
{
    self.view.backgroundColor = color;
}
- (void)hadText:(NSString *)text
{
    NSLog(@"%@",text);
}

通知方法

很简单,就是一个发通知,一个接收到这个通知然后做事情,但是用多了不好了,容易乱,项目中太多的通知,给人感觉就没有逼格了,哈哈,还是多用代理和Block

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBack" object:@"参数"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"ChangeBack"  object:nil];
}
- (void)change:(NSNotification *)notification {
   NSLog(@"%@",notification.object);
}

最后别忘了,在不用的时候给销毁

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:@"ChangeBack" ];  
}

MarkDown文本和代码均可在github上下载:GitHub地址 : CoderVan

相关文章

  • 代理模式和通知入门

    代理模式 官方解释Delegation is a simple and powerful pattern in w...

  • 代理,通知,KVO的区别

    通知 NSNotification、通知中心 NSNotificationCenter 使用范围 KVO 代理模式...

  • Spring AOP源码解析

    欲了解AOP,需先了解Java动态代理;欲了解Java动态代理,先熟悉设计模式之代理模式。入门有道,先从简学。 1...

  • 代理和通知

    层级关系浅使用代理,深层次使用通知 通知 如果在一个类中想要执行另一个类中的方法可以使用通知 1.创建一个通知对象...

  • 代理 和 通知

    代理 1,准确的说是一种软件设计模式 2,iOS当中以@protocol 形式体现 3,传递方式一对一 (通知一对...

  • 2-代理

    模式 对象要做某事,自己不去完成,让另一个对象去完成 通知与代理的区别 共同点: 利用通知和代理都能完成对象之间的...

  • 设计模式[8]-代理模式-Proxy Pattern

    1. 代理模式 代理模式(Proxy Pattern)是常用的结构性设计模式,和装饰者模式非常相似,代理类和被代理...

  • iOS 代理和通知

    代理和通知的区别 效率:代理比通知高; 关联:delegate是强关联,委托和代理双方互相知道。通知是弱关联,不需...

  • 2019-01-27 面试题

    [TOC] 1 设计模式是什么? 你知道哪些设计模式,并简要叙述。 单例模式 通知模式 代理模式 工厂模式 2 多...

  • iOS通知中心传值

    通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便、便捷,一个简单的Demo实现通知的跳转传值....

网友评论

      本文标题:代理模式和通知入门

      本文链接:https://www.haomeiwen.com/subject/qpaoyttx.html