swift - The Flyweight Pattern

The flyweight pattern is applied when a number of similar objects all rely on the same set of datavalues. Rather than create a new set of data valued for each of the objects,the flyweight patternshares one set between all of them,minimizing the amount of memory required to store the data andthe amount of work required to create them.


client:

let ss1 = Spreadsheet();

ss1.setValue(Coordinate(col: "A",row: 1),value: 100);

ss1.setValue(Coordinate(col: "J",row: 20),value: 200);

println("SS1 Total: \(ss1.total)");


let ss2 = Spreadsheet();

ss2.setValue(Coordinate(col: "F",row: 10),value: 200);

ss2.setValue(Coordinate(col: "G",row: 23),value: 250);

println("SS2 Total: \(ss2.total)");


println("Cells created: \(1300 + ss1.grid.count + ss2.grid.count)");



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pattern:

//1

func == (lhs: Coordinate,rhs: Coordinate) -> Bool {

return lhs.col == rhs.col && lhs.row == rhs.row;

}


class Coordinate : Hashable,Printable {

let col:Character;

let row:Int;

init(col:Character,row:Int) {

self.col = col; self.row = row;

}

var hashValue: Int {

return description.hashValue;

}

var description: String {

return "\(col)(\row)";

}

}


class Cell {

var coordinate:Coordinate;

var value:Int;

init(col:Character,row:Int,val:Int) {

self.coordinate = Coordinate(col: col,row: row);

self.value = val;

}

}


class Spreadsheet {

var grid:Flyweight;

init() {

grid = FlyweightFactory.createFlyweight();

}

func setValue(coord: Coordinate,value:Int) {

grid[coord] = value;

}

var total:Int {

return grid.total;

}

}



//2

import Foundation;


protocol Flyweight {

subscript(index:Coordinate) -> Int? { get set };

var total:Int { get };

var count:Int { get };

}


extension Dictionary {

init(setupFunc:(() -> [(Key,Value)])) {

self.init();

for item in setupFunc() {

self[item.0] = item.1;

}

}

}


class FlyweightFactory {

class func createFlyweight() -> Flyweight {

return FlyweightImplementation(extrinsic: extrinsicData);

}

private class var extrinsicData:[Coordinate: Cell] {

get {

struct singletonWrapper {

static let singletonData = Dictionary<Coordinate,Cell> (

setupFunc: {() in

var results = [(Coordinate,Cell)]();

let letters:String = "ABCDEFGHIJKLMnopQRSTUVWXYZ";

var stringIndex = letters.startIndex;

let rows = 50;

do {

let colLetter = letters[stringIndex];

stringIndex = stringIndex.successor();

for rowIndex in 1 ... rows {

let cell = Cell(col: colLetter,row: rowIndex,

val: rowIndex);

results.append((cell.coordinate,cell));

}

} while (stringIndex != letters.endindex);

return results;

}

);

}

return singletonWrapper.singletonData;

}

}

}


class FlyweightImplementation : Flyweight {

private let extrinsicData:[Coordinate: Cell];

private var intrinsicData:[Coordinate: Cell];

private let queue:dispatch_queue_t;

private init(extrinsic:[Coordinate: Cell]) {

self.extrinsicData = extrinsic;

self.intrinsicData = Dictionary<Coordinate,Cell>();

self.queue = dispatch_queue_create("dataQ",disPATCH_QUEUE_CONCURRENT);

}

subscript(key:Coordinate) -> Int? {

get {

var result:Int?;

dispatch_sync(self.queue,{() in

if let cell = self.intrinsicData[key] {

result = cell.value;

} else {

result = self.extrinsicData[key]?.value;

}

});

return result;

}

set (value) {

if (value != nil) {

dispatch_barrier_sync(self.queue,{() in

self.intrinsicData[key] = Cell(col: key.col,

row: key.row,val: value!);

});

}

}

}

var total:Int {

var result = 0;

dispatch_sync(self.queue,{() in

result = reduce(self.extrinsicData.values,0,{total,cell in

if let intrinsicCell = self.intrinsicData[cell.coordinate] {

return total + intrinsicCell.value;

} else {

return total + cell.value

}

});

});

return result;

}

var count:Int {

var result = 0;

dispatch_sync(self.queue,{() in

result = self.intrinsicData.count;

});

return result;

}

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...