swift - The Facade Pattern

swift - The Facade Pattern,第1张

概述Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。它是为子系统中的一组接口所提供的一个一致的界面。 client: import Foundation; let facade =PirateFacade(); let prize = facade.getTreasure(TreasureTypes.SHIP); if (pri

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;

}

}

总结

以上是内存溢出为你收集整理的swift - The Facade Pattern全部内容,希望文章能够帮你解决swift - The Facade Pattern所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1210255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存