代理模式

基本理解

  • 代理模式(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. 析构函数和Dispose的使用区别

    老生常谈的问题了,MSDN也有非常详细的说明但看起来不是很系统.也曾经做过分析,但没有总结下来又忘了,这次整理一下MSDN和网上搜集的一些资料,以备不时只需. 下面是MSDN对这两个函数的建议使用方法 ...

  2. .NET 产品版权保护方案 (.NET源码加密保护) (转载)

    说 明:你希望自己用.net辛辛苦苦做出来的软件被人轻易破解吗?你希望自己花了大量人力物力用.net开发出来的产品被竞争对手轻易获取核心代码吗?这是 一篇比较详尽地介绍如何保护自己的.net源代码的文 ...

  3. Feathers JS – 基于 Express 构建数据驱动的服务

    Feathers 是一个轻量的 Web 应用程序框架,基于 NodeJS 最流行​​的 Web 框架——Express.这使得它很容易使用 socket.io 来创建 RESTful Web 服务和实 ...

  4. SQL查询语句去除重复行

    1.存在两条完全相同的纪录 这是最简单的一种情况,用关键字distinct就可以去掉 select distinct * from table(表名) where (条件) 2.存在部分字段相同的纪录 ...

  5. 2014.7.12 敏捷个人奥森健步走&敏友分享会.活动报道

    今天是个阳光明媚的日子,正式开起了敏捷个人2014年每月一次的健步走&读书分享活动. 周老师一大早8点就到了森林公园南门,一边看书一边等候敏友们的到来.时间走到了9点半,大家基本到齐了,我们准 ...

  6. phpBB论坛 代码 语法高亮 模块 Codebox Plus

    phpBB代码语法高亮模块 Codebox Plus Code-By.Org (https://www.phpbb.com/customise/db/mod/codebox_plus/) (https ...

  7. 解决android引用library project错误

    在andriod项目中引用另一个library project时,报 The container 'Android Dependencies' references non existing libr ...

  8. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...

  9. POSTMAN and HTTPie to test APIs

    http://blog.mashape.com/postman-httpie-test-apis/ We love working with APIs at Mashape, and we love ...

  10. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...