
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)");
总结以上是内存溢出为你收集整理的swift - The Prototype Pattern全部内容,希望文章能够帮你解决swift - The Prototype Pattern所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)