《设计模式解析》 第19章 Observer 模式 复习题

本文探讨了模板方法模式的核心概念及其实现方式。该模式通过定义算法骨架并允许子类重新定义某些步骤,确保了算法结构不变的同时提供了灵活性。此外,还对比了模板方法模式与策略模式的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Review Questions

Observation

1:

The Template Method pattern makes the method call in a special way. What is that?

将方法的调用延迟到派生类中。

Interpretation

1:

According to the Gang of Four, the intent of the Template Method pattern is to "define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Redefine the steps in an algorithm without changing the algorithm's structure." What does this mean?

定义各个方法以及方法调用的顺序,而对于某些方法的实现,则让派生类去实现。这样,派生类可以根据自己的需要对这些方法进行定义,而不会影响到父类。

2:

The Gang of Four calls this a "Template Method." Why do they do this?

他定义了一个方法实现的模板,将各个方法的调用顺序都给出了,我们需要做的仅是根据自己的需要在派生类中实现这些方法的具体细节。

3:

What is the difference between the Strategy pattern (Chapter 9) and the Template Method pattern?

模板方法是改变算法的一部分。

策略模式是使用委托改变整个算法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值