What do we do first, when designing a large complex system?We try to break it into smaller manageable pieces i.e. we try to decompose the system.This is the context in which Layers architectural pattern comes into picture.While doing so normally we come across the following challenges:
- Each component or subsystem should have clearly defined responsibilities and similar responsibilities needs to be grouped together for better clarity and maintainability.
- The components should have clearly defined interfaces for communicating with their other counterparts.
- Each subsystem should be replaceable with minimum impact on the other parts of the system.
Now to tackle the challenges mentioned above we normally divide our system into multiple layers stacked one on top of the other and lower layers providing services to the higher layers as shown below:
Here client issues the request to the topmost layer of the application and then request is delegated by higher layers to the respective lower layers to do a part of the processing.This is the most common implementation scenario for the Layers pattern.Something like this we do in the web applications we develop nowadays, where the request is passed by the web layer to business and data access layers.This is the top down communication.But there can be other variations to this as well.
We can also have bottom up communication where the lowermost layer receives the input and then pushes the request up in the hierarchy as shown below:
A very common example is a low level device driver program receiving an input signal and then translating the request and passing it on to higher layers for processing.
In some situations the request can only travel upto a certain number of layers.For example the request is received by Layer N and then sent to Layer N-1.If Layer N-1 is not able to process the request then it is not sent down further.A very common example of this is an cache.If item is not found in the cache then request is aborted.This can be bottom up as well.
In some scenarios we can have two stacks of layers working together as shown in the figure below.
This is a combination of top down and bottom up communication.Request is received by topmost layer of one stack and then passed on top down to the lower most layer.Then request is sent from this lowermost layer to the lowermost layer of the other stack.This stack processes the request bottom-up across it's multiple layers.The most common example is the TCP/IP protocol stack.
So we have seen in what all different scenarios layering an application might help us.In the next post we will take look at the steps for implementing this patterns and it's advantages/disadvantages.
