swift - The Prototype Pattern
来源:互联网 发布:c 递归算法 全排列 编辑:程序博客网 时间:2024/06/07 22:08
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 {
returnfirstValue < 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-原型模式(Prototype pattern)
- swift - The Builder Pattern
- swift - The Adapter Pattern
- swift - The Decorator Pattern
- swift - The Composite Pattern
- swift - The Facade Pattern
- swift - The Flyweight Pattern
- swift - The Proxy Pattern
- swift - The command pattern
- swift - The Factory Method Pattern
- swift - The Chain Responsibility pattern
- Prototype Pattern
- prototype pattern
- Prototype Pattern
- Swift-中介者模式(The Mediator Pattern)
- [Design Pattern]:Prototype
- 原型模式(Prototype Pattern)
- CUDA学习笔记三
- 我们为什么要使用补码?
- 我适合慢思考
- 怎样阅读文献
- O-C相关-09-id 类型与应用
- swift - The Prototype Pattern
- ArrayAdapter的简单使用
- 2D-slam 激光slam: 开源代码的比较HectorSLAM Gmapping KartoSLAM CoreSLAM LagoSLAM
- 《中国人的性格》读书笔记
- (4.1.28)android之网络通信:HTTP、Socket、SOAP
- 用Swift封装图片折叠效果
- Stack-栈基础
- Hexagon处理器的条件执行机制
- jsoup替换网页中的链接demo