WIKI:In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern, whereby an algorithm's behaviour can be selected at runtime.Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.[1]
从以上可以看出,策略模式,是对不同算法的适配,支持在运行期调用。
对于C来说,所谓策略模式,就是定义一个函数钩子,并根据不同的场景赋值,在运行期调用该钩子即可指向不同的处理流程。
举例1
1)提供消息注册机制,每个节点包括消息类型和函数钩子
2)注册消息类型及其处理函数
3)根据消息类型调用处理函数
举例2
设备驱动框架,注册各设备的init、open、close、ioctrl函数。
调用者无视设备差异,调用这些函数。这个机制也可看做策略模式。
策略模式是一种软件设计模式,允许在运行时选择算法。在C语言中,它通过定义函数钩子并根据场景指派不同处理流程来实现。例如,可以创建消息注册机制,注册消息类型及对应处理函数,或在设备驱动框架中注册设备操作函数,调用者无需关注设备差异。
443

被折叠的 条评论
为什么被折叠?



