swift - The Proxy Pattern
2015-08-28 23:51
369 查看
I describe the proxy pattern in this chapter, which is used when an object is required to act as an interface to another object or resource. There are three main ways in which the proxy pattern
is applied, and I describe each of them and show you how to implement them.
Xcode Foundation的经典delegate亦复如是。
我在实际工作中vc也仿照过Foundation的delegate:
button:内涵业务逻辑,底层实现;每个button是一个类,业务逻辑需要未知的参数和处理之后未知的结果反馈
UI:点击button之后界面的改变,UI实现未知的参数和未知的结果反馈,也就是实现这个代理
这样以来UI的定制,很灵活很容易,代码思路依然清晰如初。
哪个是主体哪个是代理并不重要关键是看定义所说which is used when an object is required to act as an interface to another object or resource.
这个代理模式是结构模式中的一种,所以使用这个模式之后代码结构会非常清晰。
client:
import Foundation;
let url = "http://www.apress.com";
let headers = ["Content-Length","Content-Encoding"];
let proxy =
AccessControlProxy(url: url);
for headerinheaders {
proxy.getHeader(header, callback: {header, valin
if (val !=nil) {
println("\(header):\(val!)");
}
});
}
UserAuthentication.sharedInstance.authenticate("bob", pass:"secret");
proxy.execute();
NSFileHandle.fileHandleWithStandardInput().availableData;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pattern:
//1
import Foundation;
protocol HttpHeaderRequest {
init(url:String);
func getHeader(header:String, callback:(String,String?)
-> Void );
func execute();
}
class AccessControlProxy :HttpHeaderRequest {
privatelet wrappedObject:HttpHeaderRequest;
requiredinit(url:String) {
wrappedObject =HttpHeaderRequestProxy(url: url);
}
func getHeader(header:String, callback: (String,String?)
-> Void) {
wrappedObject.getHeader(header, callback: callback);
}
func execute() {
if (UserAuthentication.sharedInstance.authenticated) {
wrappedObject.execute();
}else {
fatalError("Unauthorized");
}
}
}
privateclass HttpHeaderRequestProxy :HttpHeaderRequest {
let url:String;
var headersRequired:[String: (String,String?)
-> Void];
requiredinit(url:String) {
self.url = url;
self.headersRequired =Dictionary<String,
(String,String?) ->Void>();
}
func getHeader(header:String, callback: (String,String?)
-> Void) {
self.headersRequired[header] = callback;
}
func execute() {
let nsUrl =NSURL(string:url);
let request =NSURLRequest(URL: nsUrl!);
NSURLSession.sharedSession().dataTaskWithRequest(request,
completionHandler: {data, response, errorin
iflet httpResponse = responseas?NSHTTPURLResponse
{
let headers = httpResponse.allHeaderFieldsas! [String:String];
for (header, callback)in
self.headersRequired {
callback(header, headers[header]);
}
}
}).resume();
}
}
//2
class UserAuthentication {
var user:String?;
var authenticated:Bool =false;
private
init() {
// do nothing - stops instances being created
}
func authenticate(user:String, pass:String) {
if (pass =="secret") {
self.user = user;
self.authenticated =true;
}else {
self.user =nil;
self.authenticated =false;
}
}
classvar sharedInstance:UserAuthentication {
get {
struct singletonWrapper {
staticlet singleton =UserAuthentication();
}
returnsingletonWrapper.singleton;
}
}
}
is applied, and I describe each of them and show you how to implement them.
Xcode Foundation的经典delegate亦复如是。
我在实际工作中vc也仿照过Foundation的delegate:
button:内涵业务逻辑,底层实现;每个button是一个类,业务逻辑需要未知的参数和处理之后未知的结果反馈
UI:点击button之后界面的改变,UI实现未知的参数和未知的结果反馈,也就是实现这个代理
这样以来UI的定制,很灵活很容易,代码思路依然清晰如初。
哪个是主体哪个是代理并不重要关键是看定义所说which is used when an object is required to act as an interface to another object or resource.
这个代理模式是结构模式中的一种,所以使用这个模式之后代码结构会非常清晰。
client:
import Foundation;
let url = "http://www.apress.com";
let headers = ["Content-Length","Content-Encoding"];
let proxy =
AccessControlProxy(url: url);
for headerinheaders {
proxy.getHeader(header, callback: {header, valin
if (val !=nil) {
println("\(header):\(val!)");
}
});
}
UserAuthentication.sharedInstance.authenticate("bob", pass:"secret");
proxy.execute();
NSFileHandle.fileHandleWithStandardInput().availableData;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pattern:
//1
import Foundation;
protocol HttpHeaderRequest {
init(url:String);
func getHeader(header:String, callback:(String,String?)
-> Void );
func execute();
}
class AccessControlProxy :HttpHeaderRequest {
privatelet wrappedObject:HttpHeaderRequest;
requiredinit(url:String) {
wrappedObject =HttpHeaderRequestProxy(url: url);
}
func getHeader(header:String, callback: (String,String?)
-> Void) {
wrappedObject.getHeader(header, callback: callback);
}
func execute() {
if (UserAuthentication.sharedInstance.authenticated) {
wrappedObject.execute();
}else {
fatalError("Unauthorized");
}
}
}
privateclass HttpHeaderRequestProxy :HttpHeaderRequest {
let url:String;
var headersRequired:[String: (String,String?)
-> Void];
requiredinit(url:String) {
self.url = url;
self.headersRequired =Dictionary<String,
(String,String?) ->Void>();
}
func getHeader(header:String, callback: (String,String?)
-> Void) {
self.headersRequired[header] = callback;
}
func execute() {
let nsUrl =NSURL(string:url);
let request =NSURLRequest(URL: nsUrl!);
NSURLSession.sharedSession().dataTaskWithRequest(request,
completionHandler: {data, response, errorin
iflet httpResponse = responseas?NSHTTPURLResponse
{
let headers = httpResponse.allHeaderFieldsas! [String:String];
for (header, callback)in
self.headersRequired {
callback(header, headers[header]);
}
}
}).resume();
}
}
//2
class UserAuthentication {
var user:String?;
var authenticated:Bool =false;
private
init() {
// do nothing - stops instances being created
}
func authenticate(user:String, pass:String) {
if (pass =="secret") {
self.user = user;
self.authenticated =true;
}else {
self.user =nil;
self.authenticated =false;
}
}
classvar sharedInstance:UserAuthentication {
get {
struct singletonWrapper {
staticlet singleton =UserAuthentication();
}
returnsingletonWrapper.singleton;
}
}
}
相关文章推荐
- swift详解之十六-----------GCD基础部分
- 十二生肖swift1.2
- ios 多线程(NSOperation)(swift)
- swift详解之十五------------NSThread线程同步锁
- Swift之UINavgationController
- swift学习之路-数组
- 闭包 Closure
- swift 改变图片颜色
- Swift之UITableView
- swift 中UICollection的一些简单的用法
- iOS系统SDK实现毛玻璃效果(swift版 iOS8.0及其以上)
- Swift学习笔记 - 字符串
- swift 学习笔记
- swift tutorial 2---你的第一个项目
- Swift学习笔记 - 变量和常量
- swift学习之杂乱无章
- Swift 对象类型转换
- Swift中的UIKit动力学
- 《从零开始学Swift》学习笔记(Day 7)——Swift 2.0中的print函数几种重载形式
- Swift 随机字符串和字符串中间截取