欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 中介者模式(Mediator Pattern)、桥接模式(Bridge Pattern) 和 策略模式(Strategy Pattern)

中介者模式(Mediator Pattern)、桥接模式(Bridge Pattern) 和 策略模式(Strategy Pattern)

2025/2/24 0:38:48 来源:https://blog.csdn.net/opentogether/article/details/144811618  浏览:    关键词:中介者模式(Mediator Pattern)、桥接模式(Bridge Pattern) 和 策略模式(Strategy Pattern)

中介者模式(Mediator Pattern)桥接模式(Bridge Pattern) 和 策略模式(Strategy Pattern) 都是常见的设计模式,它们解决不同类型的问题。我们将通过 Swift 示例来说明它们的使用场景,并附上简洁的图示。


1. 中介者模式(Mediator Pattern)

目的:中介者模式通过引入一个中介者对象,来减少类与类之间的直接耦合,避免多对多的依赖关系,使得系统中的类之间通过中介者进行交互。

使用场景

  • 当多个类之间的交互复杂且不易管理时,通过引入中介者模式可以减少各个类之间的依赖关系。
  • 适用于 UI 组件之间的交互,例如聊天系统中多个用户的消息传递,或多个模块之间的消息通知。

Swift 示例

假设我们有一个聊天系统,多个用户之间互相发送消息,使用中介者来协调消息的传递:

// 中介者协议
protocol ChatMediator {func sendMessage(_ message: String, from user: User)func addUser(_ user: User)
}// 用户类
class User {let name: Stringvar mediator: ChatMediator?init(name: String) {self.name = name}func sendMessage(_ message: String) {mediator?.sendMessage(message, from: self)}func receiveMessage(_ message: String) {print("\(name) received message: \(message)")}
}// 中介者实现
class ChatRoom: ChatMediator {private var users: [User] = []func addUser(_ user: User) {users.append(user)user.mediator = self}func sendMessage(_ message: String, from user: User) {for u in users where u !== user {u.receiveMessage(message)}}
}// 使用中介者模式
let chatRoom = ChatRoom()let user1 = User(name: "Alice")
let user2 = User(name: "Bob")
let user3 = User(name: "Charlie")chatRoom.addUser(user1)
chatRoom.addUser(user2)
chatRoom.addUser(user3)user1.sendMessage("Hello, everyone!")  // Alice 发送消息,Bob 和 Charlie 会收到消息

图示

      +------------+| ChatRoom   |+------------+|+---------+----------+|         |          |
+---+     +---+      +---+
| A |     | B |      | C |
+---+     +---+      +---+

2. 桥接模式(Bridge Pattern)

目的:桥接模式的核心思想是将抽象与实现分离,使得二者可以独立扩展。通过桥接模式,你可以将类的功能分解为多个维度的类,并将这些维度的类组合起来,减少子类的数量。

使用场景

  • 当你需要将一个类的抽象部分与其实现部分解耦,使得二者可以独立地变化时。
  • 适用于设备控制系统、图形绘制系统等场景。

Swift 示例

假设我们有一个图形绘制系统,需要同时支持不同的形状(如圆形、方形)和不同的绘制方式(如矢量绘制、位图绘制):

// 实现接口
protocol DrawingAPI {func drawCircle(radius: Double)
}// 不同的绘制方式(实现)
class VectorDrawing: DrawingAPI {func drawCircle(radius: Double) {print("Drawing a vector circle with radius \(radius)")}
}class RasterDrawing: DrawingAPI {func drawCircle(radius: Double) {print("Drawing a raster circle with radius \(radius)")}
}// 抽象部分
protocol Shape {var drawingAPI: DrawingAPI { get }func draw()
}// 圆形类
class Circle: Shape {var drawingAPI: DrawingAPIvar radius: Doubleinit(drawingAPI: DrawingAPI, radius: Double) {self.drawingAPI = drawingAPIself.radius = radius}func draw() {drawingAPI.drawCircle(radius: radius)}
}// 使用桥接模式
let vectorDrawing = VectorDrawing()
let rasterDrawing = RasterDrawing()let circle1 = Circle(drawingAPI: vectorDrawing, radius: 5)
circle1.draw()  // 输出: Drawing a vector circle with radius 5let circle2 = Circle(drawingAPI: rasterDrawing, radius: 10)
circle2.draw()  // 输出: Drawing a raster circle with radius 10

图示

  +------------------+|  Shape           |+------------------+^|+------------+| Circle     |+------------+|+-------------+| DrawingAPI  |+-------------+/         \/           \
+--------+   +--------+
| Vector |   | Raster |
+--------+   +--------+


3. 策略模式(Strategy Pattern)

目的:策略模式用于将一系列的算法封装起来,让它们可以相互替换。策略模式让算法的变化独立于使用算法的客户端。

使用场景

  • 当你有多种算法,且希望在运行时决定使用哪种算法时。
  • 适用于需要动态选择算法或者行为的场景,比如支付方式选择、排序策略等。

Swift 示例

假设我们有一个应用,允许用户选择不同的排序算法:

 

图示

// 排序策略协议
protocol SortStrategy {func sort(_ data: [Int]) -> [Int]
}// 具体的排序算法
class QuickSort: SortStrategy {func sort(_ data: [Int]) -> [Int] {return data.sorted()}
}class MergeSort: SortStrategy {func sort(_ data: [Int]) -> [Int] {return data.sorted()}
}class BubbleSort: SortStrategy {func sort(_ data: [Int]) -> [Int] {var array = datafor i in 0..<array.count {for j in 0..<array.count-i-1 {if array[j] > array[j+1] {array.swapAt(j, j+1)}}}return array}
}// Context 使用策略模式
class SortContext {private var strategy: SortStrategyinit(strategy: SortStrategy) {self.strategy = strategy}func setStrategy(_ strategy: SortStrategy) {self.strategy = strategy}func executeStrategy(data: [Int]) -> [Int] {return strategy.sort(data)}
}// 使用策略模式
let context = SortContext(strategy: QuickSort())let data = [5, 2, 9, 1, 5, 6]
let sortedData = context.executeStrategy(data: data)
print(sortedData)  // 输出: [1, 2, 5, 5, 6, 9]// 切换排序算法
context.setStrategy(BubbleSort())
let bubbleSortedData = context.executeStrategy(data: data)
print(bubbleSortedData)  // 输出: [1, 2, 5, 5, 6, 9]

lua

       +-----------------+| SortContext     |+-----------------+|+-------+--------+|                |
+-----------+     +-----------+
| QuickSort |     | BubbleSort |
+-----------+     +-----------+|                |+------------+   +------------+| MergeSort  |   | SortStrategy|+------------+   +------------+


总结对比表:

模式目的/特点使用场景Swift 示例
中介者模式降低类之间的耦合,通过中介者来协调交互多个对象之间存在复杂交互时,避免直接引用其他对象聊天系统中用户通过中介者交换消息
桥接模式将抽象和实现分离,允许它们独立变化抽象部分和实现部分变化频繁的场景图形绘制系统,不同的形状和绘制方式
策略模式将算法封装成独立的策略类,使得算法可以互换需要根据不同情境使用不同算法的场景排序算法的选择,可以动态切换不同的排序策略

这些设计模式帮助解决不同类型的耦合问题,允许我们编写更加灵活、可扩展、可维护的代码。根据实际需求选择合适的模式,可以提升代码的可维护性和复用性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词