学习高校课程-软件设计模式-迭代者模式和中介者模式(lec10)

Iterator: Problem

Collections are one of the most used data types in programming
There should be a way to go through each element of the collection
More traversal algorithms to the collection blurs its primary responsibility
Some algorithms might be tailored for a specific application
The client code may not care about how the elements are stored
集合是编程中最常用的数据类型之一
应该有一种方法来遍历集合中的每个元素
对集合的更多遍历算法模糊了其主要职责
某些算法可能是针对特定应用而定制的
客户端代码可能不关心元素如何存储

Iterator: Solution

Main idea: extract the traversal behavior of a collection into a separate object called an iterator
主要思想:将集合的遍历行为提取到一个单独的对象中,称为迭代器

Mediator: Problem


Elements may interact with each other
Changes to one element may affect others
Elements become harder to reuse
– 元素可能会相互交互 – 对一个元素的更改可能会影响其他元素 – 元素变得更难以重用

Mediator: Solution

Mediator (aka Intermediary or Controller)
中介者(又名中介者或控制者)
– Cease direct communications among components
– Collaborate indirectly by calling a special mediator object
– 停止组件之间的直接通信
– 通过调用特殊的中介对象来间接协作

posted @ 2024-12-23 22:38  Mephostopheles  阅读(0)  评论(0编辑  收藏  举报