代理模式

基本理解

  • 代理模式(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设计模式之代理模式的更多相关文章

  1. C#设计模式(13)——代理模式(Proxy Pattern)

    一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...

  2. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  3. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)

    原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...

  4. Java设计模式之代理模式(静态代理和JDK、CGLib动态代理)以及应用场景

    我做了个例子 ,需要可以下载源码:代理模式 1.前言: Spring 的AOP 面向切面编程,是通过动态代理实现的, 由两部分组成:(a) 如果有接口的话 通过 JDK 接口级别的代理 (b) 如果没 ...

  5. 设计模式之代理模式之二(Proxy)

    from://http://www.cnblogs.com/xwdreamer/archive/2012/05/23/2515306.html 设计模式之代理模式之二(Proxy)   0.前言 在前 ...

  6. 夜话JAVA设计模式之代理模式(Proxy)

    代理模式定义:为另一个对象提供一个替身或者占位符以控制对这个对象的访问.---<Head First 设计模式> 代理模式换句话说就是给某一个对象创建一个代理对象,由这个代理对象控制对原对 ...

  7. GOF23设计模式之代理模式

    GOF23设计模式之代理模式 核心作用:通过代理,控制对对象的访问.可以详细控制访问某个(某类)对象的方法,在调用这个方法前做前置处理,调用这个方法后做后置处理(即:AOP的微观实现) AOP(Asp ...

  8. C#设计模式:代理模式(Proxy Pattern)

    一,什么是C#设计模式? 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 二,代码如下: using System; using System.Collectio ...

  9. js设计模式——1.代理模式

    js设计模式——1.代理模式 以下是代码示例 /*js设计模式——代理模式*/ class ReadImg { constructor(fileName) { this.fileName = file ...

随机推荐

  1. JavaScript的深拷贝的实现

    JavaScript的数据类型 简单数据类型 string number boolean function null undefined 复杂数据类型 String Number Boolean Fu ...

  2. Hadoop第4周练习—HDFS读写文件操作

    1    运行环境说明... 3 :编译并运行<权威指南>中的例3.2. 3 内容... 3 2.3.1   创建代码目录... 4 2.3.2   建立例子文件上传到hdfs中... 4 ...

  3. 移动端 ios 长按复制兼容方案

    移动端页面,需要复制一段文字码. 在ios中,长按文字区域,默认选中的范围,超出了我长按的文字区域, 把上面的图片和下面的另一个div的文字也给我包含进来了,并不是我想要的! 举个例子: 如下图: 1 ...

  4. 黑客入门之IP地址及常用命令

    在网络上,只要利用IP地址就可以找到目标主机,因此,如果黑客想要攻击某个网络主机,就要先确定该目标主机的域名或IP地址. IP地址概述 所谓IP地址就是一种主机编址方式,给每个连接在Internet上 ...

  5. [C#] 谈谈异步编程async await

    为什么需要异步,异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源的访问有时很慢或会延迟. 如果此类活动在同步过程中受阻,则整个应用程序必须等待. 在异步过程中, ...

  6. Gradle学习系列之一——Gradle快速入门

    这是一个关于Gradle的学习系列,其中包含以下文章: Gradle快速入门 创建Task的多种方法 读懂Gradle语法 增量式构建 自定义Property 使用java Plugin 依赖管理 构 ...

  7. HBase Scan Timeout-OutOfOrderScannerNextException

    最近迁移数据时需要执行大Scan,HBase集群经常碰到以下日志: Exception in thread "main" org.apache.hadoop.hbase.DoNot ...

  8. Python入门笔记(14):Python的字符编码

    一.字符编码中ASCII.Unicode和UTF-8的区别 点击阅读:http://www.cnblogs.com/kingstarspe/p/ASCII.html 再推荐一篇相关博文:http:// ...

  9. YEdit

    YEdit YEdit is a YAML editor for Eclipse. See the wiki for more details Installation Use the Eclipse ...

  10. 不要迷恋那些没技术含量的Linux发行版

    昨天悲剧了,重装系统,一个手贱点了替换原系统,分区全给删了,将近三天的工作成果没有了.