Tiny-OS Programming/八种常用设计模式

来源:互联网 发布:编程器使用教程 编辑:程序博客网 时间:2024/05/17 09:25

本文只是自己的一个零散的笔记,可能不太好懂,目的主要是为了自己抗遗忘^=^

( 原word附带了很多图片和代码,在此略~~)

 

Design patterns are “descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context”.

 

a>Behavioural:

<1>Dispatcher:

分发器:比如消息接受器。通常的做法一是通过switch语句建立分发表,其缺点是灵活性差;其二是采用函数指针回调的方式,向分发器注册自己的处理函数,其缺点是编译时检查性差。建议采取dispacher模式:

Using a parameterized interface based on a data identifier;

Adding a new handler requires a single wiring to the dispatcher;

If one message type is not wired, the default handle is discarding it;

Each handler present as a component which support a specific interface;

所以,一些很小的地方,平时不是太在意,其实可以花点时间思考一下,是不是有更好的设计方法。

<2>decorator

A decorator component is typically a module that provides and uses the same interface type.

They provide interface adds functionalities on top of the used interface.

这个显然,我们通过增加一个中间接口来提供灵活性,使对应用而言,它所使用的接口总是非常漂亮的。

 

<3>adapter

Convert the interface of a component to another interface, without modifying the original implementation. Allow tow components with different interface to inter-operate.

 

b>structural

<4>service instance

Multiple users have separate instances of specific service. The example is the Timer.

There is only one physical clock, but every Timer must work independently.

The service instance pattern:

Each user of a service has its own instance;

But instances share code and can access each other’s state;

A component following this pattern provides its service in a parameterized interface;

The service keeps all states of the clients (using an array) and can determine how big the array should be in the compile-time;

对于像Timer这样的服务提供模块,因为只有一个物理定时器,所以各个Timer实例均需了解其余Timer的状态否则会发生冲突。各个Timer必须在一个服务里进行统一调度,但是每个 Timer应该是独立的,所以对外表现应是多个独立的实例。解决办法就是设计参数化接口。

If you want to see a detailed example, just see the component TimerM in the directory of tos/system.

 

<5>placeholder(占位符)

A example are components of LedsC and NoLedsC. They provide the same interface, but one turns the leds on or off while the other do nothing.

我们可以通过宏定义来选择使用LedsC还是NoLedsC,好处是编译前选择可以节约编译的可执行文件的大小,同时进行不同的选择时,只需修改一个宏定义而无需去文件当中修改各个相关点。

A placeholder configuration represents the desired service through a level of naming indirection.

All components that need to use the service wire to the same placeholder. The placeholder itself is just “a pass through” of the service’s interface..

 

<6> facade

The facada-pattern provides a uniform access point to interfaces provided by many components.

A facada can be a nesC configuration.

 

c>namespace

<7>keyset

Limited resources mean names are usually stored as small integer keys.

通常一个程序中有很多关乎类型的标记,这些标记可以用枚举的方式声明在全局域或局部域,具体在哪个域视类型应有的可视范围而定。总之,一个只在局部使用的类型常量应该声明在局部域。声明在全局域容易造成理解上的困难。

Applicable when:

1. A program must keep track of a set of elements or data types;

2. The set is known and fixed at compile-time.

一个枚举可以声明一个key集合

 

<8>keymap

Map keys from one set to another.

This allows you to translate global, shared names to local, optimized names, or to efficiently subset to another set.

使用全局变量有时会导致效率降低。

Keymap还可以提供运行期的灵活性。即使在运行期,我们还可以通过改变映射关系来改变程序行为。That is a good idea, right?

Example: