[ Team LiB ] Previous Section Next Section

Structure

Figure 4.3 illustrates the static structure of the Layers pattern. Layers are numbered beginning with one. N represents the total number of layers. Application code works in terms of Layer1, the most abstract data access layer. Each ConcreteLayerM implements its respective LayerM interface in terms of the next, less abstract LayerM+1 interface. ConcreteLayerN, the least abstract layer, interacts directly with the physical database driver.

Figure 4.3. The static structure of the Layers pattern.

graphics/04fig03.gif

As you define your own layering structures, remember that layers do not have to strictly conform to the structure that Figure 4.3 shows. Here are some variations you may employ in your own layered designs:

  • Describe a layer with multiple interfaces— It does not always make sense in object-oriented terms to define a layer with only one interface. One example is a data accessor layer that exposes logical database operations as well as operations for fetching query results. You can represent this type of layer abstraction more appropriately using two interfaces: one for database operations and one for result set operations.

  • Define an abstraction using classes— In general, defining abstractions using interfaces is beneficial because it prevents higher level layers from depending on implementation details. This in turn prevents you from changing implementation details as easily. However, it is not imperative that you define each layer exclusively using interfaces. You may find it more convenient to define a particular abstraction using classes, especially for those objects that represent simple data structures like a single row of physical data or an exception.

  • Use non-strict layering— Layers do not necessarily need to form a strict linear structure. Some layers may depend on multiple instances of the same layer type or multiple types of layers, especially if their implementation changes significantly depending on the runtime course of events. You may also find it convenient to skip layers on certain occasions, like for an optimized operation or a drastic behavior change. Non-strict layering adds significant complexity to the overall application structure, so document these cases well.

    [ Team LiB ] Previous Section Next Section