设计模式与并发编程技术解析
1. 设计模式之适配器模式
适配器模式在软件开发中扮演着重要角色,它能让组件在不同场景下复用,而无需改变原有实现。
1.1 适配器模式的参与者
- 原始服务(Original):即原本的服务。
- 适配器(Adapter):依据旧服务提供的功能来实现新接口。
1.2 示例代码
以下是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