微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift - The Prototype Pattern

import Foundation


class Sum : NSObject,NScopying {

var resultsCache: [[Int]];

var firstValue:Int;

var secondValue:Int;

init(first:Int,second:Int) {

resultsCache = [[Int]](count: 10,repeatedValue:

[Int](count:10,repeatedValue: 0));

for i in 0..<10 {

for j in 0..<10 {

resultsCache[i][j] = i + j;

}

}

self.firstValue = first;

self.secondValue = second;

}

private init(first:Int,second:Int,cache:[[Int]]) {

self.firstValue = first;

self.secondValue = second;

resultsCache = cache;

}

var Result:Int {

get {

return firstValue < resultsCache.count

&& secondValue < resultsCache[firstValue].count

? resultsCache[firstValue][secondValue]

: firstValue + secondValue;

}

}

func copyWithZone(zone: NSZone) -> AnyObject {

return Sum(first:self.firstValue,

second: self.secondValue,

cache: self.resultsCache);

}

}


var prototype = Sum(first:0,second:9);

var calc1 = prototype.Result;

var clone = prototype.copy() as! Sum;

clone.firstValue = 3; clone.secondValue = 8;

var calc2 = clone.Result;


println("Calc1: \(calc1) Calc2: \(calc2)");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐