- 복잡한 시스템에서 추상화된 인터페이스인 facade 를 만들어서 제공해서 서로 자세하게 알지 못하게 한다
- The facadeprovides simple methods to interact with the system. Behind the scenes, it owns and interacts with its dependencies, each of which performs a small part of a complex task.
public class OrderFacade {
public let inventoryDatabase: InventoryDatabase
public let shippingDatabase: ShippingDatabase
public init(inventoryDatabase: InventoryDatabase,
shippingDatabase: ShippingDatabase) {
self.inventoryDatabase = inventoryDatabase
self.shippingDatabase = shippingDatabase
}
public func placeOrder(for product: Product,
by customer: Customer) {
let count = inventoryDatabase.inventory[product, default: 0]
guard count > 0 else {
return
}
inventoryDatabase.inventory[product] = count - 1
var shipments = shippingDatabase.pendingShipments[customer, default: []]
shipments.append(product)
shippingDatabase.pendingShipments[customer] = shipments
}
}
public struct Customer {
public let identifier: String
public var address: String
public var name: String
}
extension Customer: Hashable {
public var hashValue: Int {
return identifier.hashValue
}
public static func ==(lhs: Customer, rhs: Customer) -> Bool {
return lhs.identifier == rhs.identifier
}
}
public struct Product {
public let identifier: String
public var name: String
public var cost: Double
}
extension Product: Hashable {
public var hashValue: Int {
return identifier.hashValue
}
public static func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.identifier == rhs.identifier
}
}
public class InventoryDatabase {
public var inventory: [Product: Int] = [:]
public init(inventory: [Product:Int]) {
self.inventory = inventory
}
}
public class ShippingDatabase {
public var pendingShipments: [Customer: [Product]] = [:]
}