[抄书]The Layers pattern

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

Pattern Oriented Software Architecture V1

From Mud to Structure

******************************

The Layers pattern (31)

******************************

helps to structure applications that can be

decomposed into groups of subtasks in which each group of

subtasks is at a particular level of abstraction.

_______________________________

Example

Networking protocols are probably the best-known example of layered

architectures.

_______________________________

Context

A large system that requires decomposition.

_______________________________

Problem

Imagine that you are designing a system whose dominant

characteristic is a mix of low- and high-level issues, where high-level

operations rely on the lower-level ones.

Such systems often also require some horizontal structuring that is

orthogonal to their vertical subdivision. This is the case where several

operations are on the same level of abstraction but are largely independent

of each other.

_______________________________

Solution

Structure your system into an appropriate number of layers and

place them on top of each other.

________________________________

Structure

The main structural characteristic of the Layers pattern is that the

services of Layer J are only used by Layer J + 1-there are no further

direct dependencies between layers. This structure can be compared

with a stack. or even an onion. Each individual layer shields all lower

layers from direct access by higher layers.

________________________________

Dynamics

Scenario I is probably the best-known one. A client Issues a request

to Layer N. Since Layer N cannot cany out the request on its own. it

calls the next Layer N - 1 for supporting subtasks. Layer N - I provides

these. In the process sending further requests to Layer N-2. and so

on until Layer I 1s reached. Here, the lowest-level servlces are finally

performed. If necessary, replies to the different requests are passed

back up from Layer 1 to Layer 2, kom Layer 2 to Layer 3, and so on

until the final reply arrives at Layer N.

A characteristic of such top-down communication Is that Layer J

often translates a slngle request from Layer J+1 Into several requests

to Layer J- 1. This is due to the fact that Layer J is on a hlgher level of

abstraction than Layer J- 1 and has to map a hlgh-level service onto

more prlmlUve ones.

Scenario II illustrates bottom-up communicaUon-a chaln of actions

starts at Layer 1, for example when a device driver detects input. The

driver translates the lnput into an Internal format and reports it to

Layer 2. which starts lnterpreting it, and so on. In thls way data

moves up through the layers until It arrives at the highest layer. While

top-down lnformauon and control flow are often described as

'requests'. bottom-up calls can be termed 'notifications'.

As mentioned in Scenario I, one top-down request often fans out to

several requests in lower layers. In contrast. several bottom-up noUfications

may either be condensed into a slngle notificauon higher In

the structure. or remain in a I : I relationship.

Scenario III descrlbes the situation where requests only travel

through a subset of the layers. A top-level request may only go to the

next lower level N- 1 lf this level can satisfy the request. An example

of this is where level N- 1 acts as a cache. and a request from level N

can be satisfied without being sent all the way down to Layer 1 and

from here to a remote server. Note that such caching layers mafntain

state information, while layers that only forward requests are often

stateless. Stateless layers usually have the advantage of being

simpler to program, particularly with respect to re-entrancy.

Scenario IV describes a situation similar to Scenario 111. An event is

detected in Layer 1, but stops at Layer 3 instead of traveling all the

way up to Layer N. In a communication protocol, for example, a resend

request may arrive from an impatient client who requested data

some time ago. In the meantime the server has already sent the

answer, and the answer and the re-send request cross. In this case,

Layer 3 of the server side may notice this and intercept the re-send

request without further action.

Scenario V involves two stacks of N layers communicating with each

other. This scenario is well-known from communication protocols

where the stacks are known as 'protocol stacks'. In the following

diagram, Layer N of the left stack issues a request. 'The request moves

down through the layers until it reaches Layer 1, is sent to Layer 1 of

the right stack, and there moves up through the layers of the right

stack. The response to the request follows the reverse path until it

arrives at Layer N of the left stack.

____________________________________

Implementation p38

Not all the following steps are mandatory-it depends on your

applicauon.

1 Define the abstraction criterion for grouping tasks into layers.

2 Determine the number of abstraction levels according to your

abstraction criterion.

3 Name the layers and assign tasks to each of them

4 Specih the services.

5 Refine the layering.

6 Specify an interface for each layer.

7 Structure indivzdual layers.

8 Specth the communication between adjacent layers.

9 Decouple adjacent layers.

10 Design an error-handling strategy.

______________________________________

Example Resolved

TCP/IP

_______________________________________

Variants

Relaxed Layered System

This is a variant of the Layers pattern that

is less restrictive about the relationship between layers.In a Relaxed

Layered System each layer may use the services of all layers below it,

not only of the next lower layer. A layer may also be partially opaquethis

means that some of its services are only visible to the next higher

layer, while others are visible to all higher layers. The gain of flexibility

and performance in a Relaxed Layered System is paid for by a loss of

maintainability.

Layering Through Inheritance

This variant can be found in some object-oriented systems. In this variant

lower layers are implemented as base classes. A higher layer requesting

services from a lower layer inherits from the lower layer's

implementation and hence can issue requests to the base class

services. An advantage of this scheme is that higher layers can modify

lower-layer services according to their needs. A drawback is that such

an inheritance relationship closely ties the higher layer to the lower

layer. If for example the data layout of a C++ base class changes, all

subclasses must be recompiled. Such unintentional dependencies

introduced by inheritance are also known as the fragile base class

problem.

__________________________________

Known Uses

Virtual Machines.

APIs.

Information Systems (IS)

Presentation 、Application logic、Domain layer、Database

Windows NT

__________________________________

Consequences

Reuse of layers.

Support for standardization.

Dependencies are kept local.

__________________________________

See Also

Composite Message.

A Microkernel architecture

The PAC architectural pattern

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航