学习高校课程-软件设计模式-备忘录模式和观察者模式(lec11)

Memento: Problem

Example: supporting undo in a text editor app
Problem 1: private fields cannot be accessed
Problem 2: when fields are made public, refactoring would be problematic
Problem 3: the fields of the snapshot class need to be public, exposing all the editor’s states
示例:在文本编辑器应用程序中支持撤消
问题一:私有字段无法访问
问题2:当字段公开时,重构会出现问题
问题3:快照类的字段需要是public的,暴露所有的编辑的状态

Memento: Solution

Essence of the problems: broken encapsulation
Memento: delegating the creation of state snapshots to the actual owner of the state – the originator object
Storing the copy of state in a special object: memento
问题本质:破损封装
备忘录:将状态快照的创建委托给状态的实际所有者——发起者对象
将状态副本存储在特殊对象中:备忘录

Observer: Problem

Two types of objects: Customer and Store
The customer is interested in a particular product which should become available soon
• Problem 1: the customer visits the store frequently and checks product availability
• Problem 2: the store sends tons of emails to all customers each time
a new product becomes available

两种类型的对象:客户和商店
客户对某种即将上市的特定产品感兴趣
问题 1 :顾客经常到店检查商品库存情况
问题 2:每次有新产品上市时,商店都会向所有顾客发送大量电子邮件

Observer: Solution

posted @ 2024-12-24 11:27  Mephostopheles  阅读(1)  评论(0编辑  收藏  举报