外观模式

外观模式(Facade Pattern)

外观模式是一种结构模式,给一个复杂的系统提供一键简单的接口,它包含两种类型:

  1. facade:提供了简单的接口来跟系统交互,这允许开发者使用外观,而不是了解系统中的多个类并与之交互
  2. dependencies:被facade持有,每个dependency负责一个巨大任务中的一部分

使用举例

一个购物系统可能包含了消费者,产品,库存,运输订单等部件。我们不需要消费者理解每个部件和他们之间如何交互,只需要提供一个facade来暴露一些常见的方法,比如下订单和完成订单。

创建dependencies

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
// MARK: - Dependencies
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]] = [:]
}

创建facade

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
// MARK: - Facade
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) {
print("Place order for '\(product.name)' by '\(customer.name)'")
let count = inventoryDatabase.inventory[product, default: 0]
guard count > 0 else {
print("'\(product.name)' is out of stock!")
return
}
inventoryDatabase.inventory[product] = count - 1
var shipments =
shippingDatabase.pendingShipments[customer, default: []]
shipments.append(product)
shippingDatabase.pendingShipments[customer] = shipments
print("Order placed for '\(product.name)' " +
"by '\(customer.name)'")
}
}

使用外观模式

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
// MARK: - Example
let rayDoodle = Product(
identifier: "product-001",
name: "Ray's doodle",
cost: 0.25)
let vickiPoodle = Product(
identifier: "product-002",
name: "Vicki's prized poodle",
cost: 1000)
let inventoryDatabase = InventoryDatabase(
inventory: [rayDoodle: 50, vickiPoodle : 1]
)
let orderFacade = OrderFacade(
inventoryDatabase: inventoryDatabase,
shippingDatabase: ShippingDatabase())
let customer = Customer(
identifier: "customer-001",
address: "1600 Pennsylvania Ave, Washington, DC 20006",
name: "Johnny Appleseed")
orderFacade.placeOrder(for: vickiPoodle, by: customer)

其他例子

  1. SDWebImage中使用到了外观模式:https://juejin.im/post/5b6c0878e51d45348a3028d2
  2. 买汽车中的外观模式:https://www.jianshu.com/p/80f0455a5b08
  3. app登录系统中的外观模式:https://www.jianshu.com/p/d04b47cfa1cd