加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift - The Facade Pattern

发布时间:2020-12-14 01:52:51 所属栏目:百科 来源:网络整理
导读:Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。它是为子系统中的一组接口所提供的一个一致的界面。 client: import Foundation; let facade = PirateFacade (); let prize = facad

Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。它是为子系统中的一组接口所提供的一个一致的界面。


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);

}

}

}


//2

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;

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读