iOS设计模式之代理模式
代理模式
基本理解
- 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问。
代理模式的应用
- 远程代理:就是为一个对象在不同的地址空间提供据不代表。这样可以隐藏一个对象存在于不同地址空间的事实。
- 虚拟代理:是根据需要创建开销很大的对象,通过它来存放实例化需要很长时间的真实对象。
- 安全代理:用来控制真实对象访问时的权限。
*智能指引:是指当调用真实的对象时,代理处理另外一些事。
例子
ChildViewCongroller.h
//
// ChildViewController.h
// DelegateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol ChildDelegate <NSObject>
-(void)changeColor:(UIColor *)color;
@end
@interface ChildViewController : UIViewController
{
}
@property(assign,nonatomic)id <ChildDelegate>ChildDelegate;
@end
ChildVIewController.m
//
// ChildViewController.m
// DelegateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
//
#import "ChildViewController.h"
@interface ChildViewController ()
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 200, 50)];
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
// button.backgroundColor = [UIColor redColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:@"返回调用代理" forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)show {
[_ChildDelegate changeColor:[UIColor redColor]];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
在一个ViewController中去push出来ChildViewController。点击ChildViewController中的按钮改变根视图的背景色
ViewController.h
//
// ViewController.h
// DelegateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ViewController : UIViewController<ChildDelegate>
- (IBAction)showChild:(id)sender;
@end
ViewController.m
//
// ViewController.m
// DelegateDemo
//
// Created by zhanggui on 15/8/6.
// Copyright (c) 2015年 zhanggui. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - ChildViewDelegate Mehtod
-(void)changeColor:(UIColor *)color {
self.view.backgroundColor =color;
NSLog(@"change color.....");
}
- (IBAction)showChild:(id)sender {
ChildViewController *child = [ChildViewController new];
child.ChildDelegate = self;
[self.navigationController pushViewController:child animated:YES];
}
@end
这样通过代理就可以去实现。
附:
iOS设计模式之代理模式的更多相关文章
- C#设计模式(13)——代理模式(Proxy Pattern)
一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...
- Java设计模式之代理模式(静态代理和JDK、CGLib动态代理)以及应用场景
我做了个例子 ,需要可以下载源码:代理模式 1.前言: Spring 的AOP 面向切面编程,是通过动态代理实现的, 由两部分组成:(a) 如果有接口的话 通过 JDK 接口级别的代理 (b) 如果没 ...
- 设计模式之代理模式之二(Proxy)
from://http://www.cnblogs.com/xwdreamer/archive/2012/05/23/2515306.html 设计模式之代理模式之二(Proxy) 0.前言 在前 ...
- 夜话JAVA设计模式之代理模式(Proxy)
代理模式定义:为另一个对象提供一个替身或者占位符以控制对这个对象的访问.---<Head First 设计模式> 代理模式换句话说就是给某一个对象创建一个代理对象,由这个代理对象控制对原对 ...
- GOF23设计模式之代理模式
GOF23设计模式之代理模式 核心作用:通过代理,控制对对象的访问.可以详细控制访问某个(某类)对象的方法,在调用这个方法前做前置处理,调用这个方法后做后置处理(即:AOP的微观实现) AOP(Asp ...
- C#设计模式:代理模式(Proxy Pattern)
一,什么是C#设计模式? 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 二,代码如下: using System; using System.Collectio ...
- js设计模式——1.代理模式
js设计模式——1.代理模式 以下是代码示例 /*js设计模式——代理模式*/ class ReadImg { constructor(fileName) { this.fileName = file ...
随机推荐
- Eclipse卸载插件
Eclipse卸载插件 ### 本人Eclipse版本为:Eclipse Mars 1. 选择: Help -> Install New Software , 如下图:2. 点击 what is ...
- 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...
- Spring MVC 3.x 基本配置
WEB-INF/web.xml 例1 <?xml version="1.0" encoding="UTF-8"?> <web-app xmln ...
- 使用TFS+GIT实现分布式项目管理
前言 GIT是近来很流行的一种版本控制系统,是Linux内核之父Linus Torvalds为了管理Linux内核的开发而开发的一种开源的版本控制工具. GIT相比传统的版本控制工具最大的优点是实现了 ...
- 二叉查找树(三)之 Java的实现
概要 在前面分别介绍了"二叉查找树的相关理论知识,然后给出了二叉查找树的C和C++实现版本".这一章写一写二叉查找树的Java实现版本. 目录 1. 二叉树查找树2. 二叉查找树的 ...
- linux下如何修改weblogic console登陆的用户名和密码
1. 执行安装目录下config.sh ./config.sh 2.选择 2|Extend an existing WebLogic configuration 3. 别的一路跳过,到修改secur ...
- mysql 输出当前月所有日期与对应的星期
其实可以用存储过程,但想用另一种方法实现: 首先创建一个辅助表,可以设置CREATE TABLE `t4` ( `id` ) NOT NULL AUTO_INCREMENT, `num` ) DEFA ...
- 高级四则运算器—结对项目总结(193 &105)
高级四则运算器—结对项目总结 为了将感想与项目经验体会分割一下,特在此新开一篇博文. 界面设计 啥都不说,先上图震慑一下... 上面的三个界面是我们本次结对项目的主界面,恩,我也觉得挺漂亮的!你问我界 ...
- 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)
原文链接:Limitations with Retrying Execution Strategies (EF6 onwards) 当使用重试执行策略的时候,大体有以下两种局限性: 不支持以流的方式进 ...
- Python入门笔记(5):对象
一.学习目录 1.pyhton对象 2.python类型 3.类型操作符与内建函数 4.类型工厂函数 5.不支持类型 二.思考 1.Python解释执行原理? 2.Python对象机制? 3.Pyth ...