In my earlier post I had discussed about the context/problem/solution of the Layers pattern and it's multiple implementation scenarios.In this part we will focus on the steps we need to follow in order to implement this pattern and it's benefits and challenges.
Implementation Steps
- Define the abstraction criteria
- First we need to determine the criteria based on which we will define the layers
- Define the levels of abstraction
- Once the criteria for abstraction is determined we need to define the levels of abstraction.This is a very critical task as we need to find the right level of abstraction and each abstraction level will correspond to one layer.So here we are deciding how many layers we will have.
- Name the layers and assign tasks to them
- Here we need to name the layers for better clarity throughout the design process.We also need to clearly identify what tasks each of the layers will perform.The individual layers should have high level of cohesion in terms of the tasks they perform.
- Specify the services
- This is another critical step in the process.On principle the layers should be separated from each other as far as possible and no component should spread across layers.So we need to specify the services accordingly.We have to try to put as much services in the higher layers as possible than the layers down below.This is to avoid low level programming.
- Refine the layers
- Here we go back and repeat the previous steps in an iterative fashion.
- Identify the interfaces
- The interfaces for each of the layers should be clearly defined.The individual layers should be blackbox to the others and only expose proper interfaces for communication.Proper definition of these interfaces are required to help in replacement of one the layers without affecting the others.
- Structure the layers
- The internal structure and components of the individual layers needs to properly designed as well.
- Define the communication modes
- We need to define how the individual layers are going to communicate to each other, synch/async,callbacks etc.
- Decouple adjacent layers
- The layers should be decoupled from each other as much as possible.But here one layer will have some dependency on the adjacent layer(immediately up or down in the chain of communication).We should try to reduce this coupling by defining the interfaces in such as way that internal implementation of one layer is totally hidden from the others.
- Define the error handling strategy
- This one of the major challenges of layered approach,how we are going to send the error information from one layer to another.Here the rule of thumb is to handle most of the errors in the lower layers and convert them into more generalized errors to be send to the higher layers.
Benefits
- Higher reusability
- Exchangeability, the individual layers can be replaced without affecting the entire system
- Localization of dependencies
- Higher testability as individual layers can be tested in isolation
Challenges
- It's a difficult task to determine the right granularity of layers
- Possibility of cascaded changes throughout the system when functionality of one of the layers changes.
- Lower Performance due to the overhead of communication across so many layers
References
