client:
import Foundation;
let facade =PirateFacade();
let prize = facade.getTreasure(TreasureTypes.SHIP);
if (prize !=nil) {
facade.crew.performAction(PirateCrew.Actions.DIVE_FOR_JEWELS,
callback: {secondPrizein
println("Prize: \(prize! + secondPrize) pieces of eight");
});
}
NSFileHandle.fileHandleWithStandardInput().availableData;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pattern:
//1
class TreasureMap {
enum Treasures {
case GALLEON; case BURIED_GOLD;case SUNKEN_JEWELS;
}
struct MapLocation {
let gridLetter: Character;
let gridNumber: UInt;
}
func findTreasure(type:Treasures) ->MapLocation {
switch type {
case .GALLEON:
return MapLocation(gridLetter:"D",gridNumber: 6);
case .BURIED_GOLD:
return MapLocation(gridLetter:"C",gridNumber: 2);
case .SUNKEN_JEWELS:
return MapLocation(gridLetter:"F",gridNumber: 12);
}
}
}
import Foundation;
class PirateShip {
struct ShipLocation {
let NorthSouth:Int;
let EastWest:Int;
}
var currentPosition:ShipLocation;
var movementQueue = dispatch_queue_create("shipQ",DISPATCH_QUEUE_SERIAL);
init() {
currentPosition = ShipLocation(NorthSouth: 5,EastWest: 5);
}
func moveToLocation(location:ShipLocation,callback:(ShipLocation) ->Void) {
dispatch_async(movementQueue,{()in
self.currentPosition = location;
callback(self.currentPosition);
});
}
}
//3
import Foundation;
class PirateCrew {
let workQueue = dispatch_queue_create("crewWorkQ",DISPATCH_QUEUE_SERIAL);
enum Actions {
case ATTACK_SHIP; case DIG_FOR_GOLD; case DIVE_FOR_JEWELS;
}
func performAction(action:Actions,callback:(Int) ->Void) {
dispatch_async(workQueue,{()in
var prizeValue = 0;
switch (action) {
case .ATTACK_SHIP:
prizeValue =10000;
case .DIG_FOR_GOLD:
prizeValue =5000;
case .DIVE_FOR_JEWELS:
prizeValue =1000;
}
callback(prizeValue);
});
}
}
//4
import Foundation
enum TreasureTypes {
case SHIP; case BURIED;case SUNKEN;
}
class PirateFacade {
let map = TreasureMap();
let ship = PirateShip();
let crew = PirateCrew();
func getTreasure(type:TreasureTypes) ->Int? {
var prizeAmount:Int?;
// select the treasure type
var treasureMapType:TreasureMap.Treasures;
var crewWorkType:PirateCrew.Actions;
switch (type) {
case .SHIP:
treasureMapType =TreasureMap.Treasures.GALLEON;
crewWorkType =PirateCrew.Actions.ATTACK_SHIP;
case .BURIED:
treasureMapType =TreasureMap.Treasures.BURIED_GOLD;
crewWorkType =PirateCrew.Actions.DIG_FOR_GOLD;
case .SUNKEN:
treasureMapType =TreasureMap.Treasures.SUNKEN_JEWELS;
crewWorkType =PirateCrew.Actions.DIVE_FOR_JEWELS;
}
let treasureLocation = map.findTreasure(treasureMapType);
// convert from map to ship coordinates
let sequence:[Character] = ["A","B","C","D", "E","F","G"];
let eastWestPos = find(sequence,treasureLocation.gridLetter);
let shipTarget = PirateShip.ShipLocation(NorthSouth:
Int(treasureLocation.gridNumber),EastWest: eastWestPos!);
let semaphore =dispatch_semaphore_create(0);
// relocate ship
ship.moveToLocation(shipTarget,callback: {locationin
self.crew.performAction(crewWorkType,{prize in
prizeAmount = prize;
dispatch_semaphore_signal(semaphore);
});
});
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
return prizeAmount;
}
}
原文链接:https://www.f2er.com/swift/326163.html