Avijeet Dash and Satyabrata Dash
Introduction
Complex application need to be broken down into group of subtasks, where each group of subtasks is at a particular level of abstraction.
Layering is a common technique to break a complex system into many logical layers, which are at different levels of abstraction. Layering is based on the principle of ‘Abstraction’. Abstraction is a very basic requirement for dealing with complexity. Human mind can deal with complexity to a certain limit, beyond that it’s difficult to work with complex systems. It’s a common technique to break the system into different layers with varying levels abstraction to manage complexity. Each layer is a group of subtasks, which is pretty independent in it self. For example, lets consider a microprocessor chip. From the outset it’s the input and output ports that give it the microprocessor functionality. If we look slightly deep into it, we will see silicon wafers. If we look into the silicon we will see electrons and protons. To understand how microprocessor does addition if we try to understand electron movements, we will never get there. Thus it’s very critical to work at the right level of abstraction to design and orchestrate a complex system.
In 30 Seconds
Layering is "divide and conquer" the software complexities. Layer your application in such a way that different layers will have minimal inter-dependencies and it should follow the thumb rule that a layer should not depend on the layers above it.
Motivation
The benefit of abstraction is each layer can be seen in isolation and can be understood and implemented without knowing much about the other layers.
Layers can be substituted with alternate implementations of the same basic services. So each layer should have a clearly defined interface
As the dependency between layers is low, parallel development of these layers makes development faster.
Each layer can implement a standard in itself.
Why Layering?
Typically enterprise applications have three parts to it presentation, domain logic and data source. The earlier client-server systems used to have a two layer system where the data store used to keep data, data access logic and domain logic in the form of stored procedures and GUI components used to talk to database for the display of data. This had the problem of tight coupling of code with data. Which made incorporating change difficult and duplicate codes in presentation and domain logic. With the coming up of Internet, applications got structured to multiple layers. The latest J2EE Patterns publication suggest a 5 layer architecture
Core J2EE | Layers |
Client | Presentation that runs on client (html browser or fat client like java swing etc.) |
Presentation | Presentation part that runs on server (JSP, Servlets) |
Business | Domain Logic |
Integration | Data source (RDBMS, ODBMS) |
Resource | The external resource (Lagacy systems like ERP, Mainframes etc) |
Structure
From the structure two things to note are
- Each layer provides the services to the next layer above it
- Each layer delegates the sub tasks to the lower layer
Thus though the layers stand for themselves they collaborate with the layer above and below them using an agreed upon interface.
Complexities Simplified
One of the challenges is to define the abstraction criterion for grouping tasks into layers. Determining the number of abstraction levels depends on the abstraction criterion. This has the trade-offs when deciding whether to split particular aspects into two layers or combine them into one.
Error handling can be rather expensive for layered architectures with respect to processing time and programming effort. The best practice is to handle errors as much as in the same layer as possible, and only propagate generic errors to upper layers.
The performance of a layered application might be worse than a monolithic application, especially when different layers are distributed. So layers must follow the standard patterns like Session Façade, Data Transfer object patterns to communicate between two layers.
Related patterns
Conclusion
Layering is an architectural decision, which has to be made at the beginning of project. The three most common layers in an enterprise application are Presentation, Domain and Data source, although there can be more fine-grained layers depending on complexity of a system.
Resources: