1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
* 抽象产品(货币) */ protocol Currency { func symbol() -> String func code() -> String }
class Euro : Currency { func symbol() -> String { return "€" } func code() -> String { return "EUR" } }
class UnitedStatesDolar : Currency { func symbol() -> String { return "$" } func code() -> String { return "USD" } }
国家 - UnitedStates: 美国 - Spain: 西班牙 - UK: 英国 - Greece: 希腊 */ enum Country { case UnitedStates, Spain, UK, Greece }
具体工厂 */ enum CurrencyFactory { static func currencyForCountry(country:Country) -> Currency? { switch country { case .Spain, .Greece : return Euro() case .UnitedStates : return UnitedStatesDolar() default: return nil } } }
let noCurrencyCode = "No Currency Code Available"
CurrencyFactory.currencyForCountry(.Greece)?.code() ?? noCurrencyCode CurrencyFactory.currencyForCountry(.Spain)?.code() ?? noCurrencyCode CurrencyFactory.currencyForCountry(.UnitedStates)?.code() ?? noCurrencyCode CurrencyFactory.currencyForCountry(.UK)?.code() ?? noCurrencyCode
|