设计模式与并发编程深入解析
1. 设计模式 - 适配器模式
在软件开发中,适配器模式是一种非常实用的设计模式。它主要有两个参与者:
-原始服务(Original):即原始的服务,是已有的功能模块。
-适配器(Adapter):根据旧服务提供的功能来实现新的接口。
下面是一个具体的示例代码,AlarmToTimerC将中断驱动(异步)的Alarm接口转换为任务驱动(非异步)的Timer接口,并根据Alarm的单次事件实现Timer的周期性事件:
generic module AlarmToTimerC ( typedef precision_tag ) { provides interface Timer < precision_tag >; uses interface Alarm < precision_tag , uint32_t >; } implementation { uint32_t m_dt ; bool m_oneshot ; void start ( uint32_t t0 , uint32_t dt , bool oneshot ) { m_dt