Layered architecture model is the most commonly used architectural patterns, also known as n-tier architecture model.Most architects, designers, developers are familiar with the model.IT with the traditional IT communications and organizational structure is very match, so most business application development naturally choose IT.
Layered architecture model of internal components are organized into hierarchical levels, each layer performs a specific role.Although the layered architecture model does not specify the number and types of layered, most will be divided into four layers: presentation layer (the presentation) (business), the persistence layer, business layer (persistence), the database layer (database).In some cases, the business layer and persistence layer will merge into a business, especially when the persistence logic embedded in the business layer components.So small application may only three layers, while larger and more complex applications may contain five layer even more layers.
Layered architecture model of each layer corresponds to the application of specific roles and responsibilities.For example, the persistence layer is responsible for handling user interaction and browser communication logic, the business layer is responsible for performing certain business rules associated with the request.Architecture of the abstraction layer will need to complete the work, to meet specific business request.Display layer, for example, do not need to know also don't need to concern how to get user data, it only needs to be in a specific format to show the information on the interface.Similar to the business layer does not need to be concerned with how to display data and the data come from, it just needs to get data from the persistence layer, business logic to the data processing (calculation, aggregation, etc.), and pass the results up to show the layer.
Figure 1 layered architecture model (layered architecture pattern)
A characteristic of layered architecture is the separation of concerns (separation of concerns).Layer of the components in the only responsible for the logic of this layer.Show layer only deals with logic, for example, the business layer only deals with the business logic.This classifying components made in the architecture is easy to build an effective roles and responsibilities of the module, also easy to develop, test, management and maintenance, thanks to clear component interfaces and formed a limited scope.
A key concept
Note in figure 1-2, architecture of each layer is marked as closed (closed).This is one of the most important concept in layered architecture model.Closed layer means that a request when moving between layer and layer, it must pass through the layer below it to reach down one floor.Display layer, for example, the request must first via the business layer and then after the persistence layer can eventually reach the database layer.
Figure 2 seal and request access to the process (closed the layers and request access.)
Why not allow show layer directly access the persistence layer or database?After all, the show direct access to the database layer than the database to read letter write layers of information transfer faster.The answer to this question is one of the key concepts: the isolation layer (the layers of the isolation).
Isolation layer concept means that changes in one layer in the framework will not affect other components within the layer and change isolated within the layer between components, may also affect the associated layer (such as persistence layer with SQL statements).If allows direct access to the persistent layer, then change the SQL statements in the persistence layer will affect the business layer and presentation layer at the same time, for there are a lot of mutual dependence between components, and the application is too tightly coupled.This architecture will be very difficult to change.
Isolation layer concept also means between layer and layer is independent and therefore only know a small amount of or don't know the inner workings of other layer.In order to understand the concept of power and importance, can consider to change display layer from the JSP to JSF when the amount of work needs to be done.Hypothesis is consistent with the contract between the presentation layer and business layer, then show the transformation layer, business layer completely unaffected, nothing to do with UI framework used in the presentation layer type.
Although closed prompted layer isolation, make changes within the framework architecture is independent, but sometimes will be open some layer is also meaningful.For example, you need to add a Shared services include general service component layer makes the business layer components called (such as the string handling and logging, etc.), create a service layer is usually a good idea, because the architecture for Shared services are limited in the business layer calls.If not to separate it into a layer, the presentation layer can also call these Shared services, leading to access restrictions are difficult to manage.
In this case, the new service layer should be placed under the business layer, display layer cannot direct access to it.However, this presents a problem, now the business layer need to pass to get to the persistence layer, service layer that is completely meaningless.This is an age-old problem layered architecture, by creating open layer in the architecture to solve.
Tag as shown in figure 3, the service layer to open (open), means that the request can across the open direct access to the next layer.Case, the business layer can directly reach the persistence layer instead of after service layer, it is more reasonable.
Figure 3 open layer and request process (open the layers and the request flow)
Using the layer of open and closed helps to define the concept of structure layer and the relationship between the request flow, also for the designers and developers to provide the necessary information to understand the structure of each layer in the access restrictions.No suitable to determine the architecture of each layer is open or closed, usually leads to architecture tight coupling and fragile, difficult to test, maintain, and deployment.
Under the assumption that users need to obtain customer information, as shown.Black arrows indicate the request flow to get the customer data to the database and the red arrows indicate the request flow back to the interface and display the data.Example, customer information, including customer data and order data.
The Customer Screen is used for receiving a request and show the user information, it does not know where the data is, how to obtain, design a few pieces of a database table.As long as it receives the request to the information of a user, it will pass the request to the Customer the Delegate module.This module knows the business layer which module can process the request, how to access the business layer module, as well as the data you need (contract).The business layer of the Customer business Object is in charge of the aggregation of the request all the necessary data (in the case is to obtain Customer information).It calls the object persistence layer data access Customer Dao and Order the Dao access to Customer data and Order data respectively.These two data access Object executing SQL statements, obtain the corresponding data, the data back to the business layer of the Customer Object.Once the Customer Object receives the data, it will be the data aggregated again back to the Customer the Delegate, back to the Customer Screen display to the user.
Figure 4 layered architecture
Matters needing attention
Layered architecture model is a reliable general, for most applications it is a goodstart, especially when I don't know what kind of structure is suitable for use.From the Angle of architecture after choose the model, there are some matters needing attention.
The first thing to note is called the sewage pool anti-patterns (architecture sinkhole anti - the pattern).The anti-pattern description is such a scenario, the request flow through the architecture of many layers, each layer is only a small amount of even no business logic.For example, suppose that show response to the user's request for customer information, show the transfer request to the business layer, business layer do nothing, just pass request directly to the persistence layer and persistence layer executing SQL statements to get the data.The data in the process of back without any processing.
Each layered architecture there may be some scenarios fall into the sewage pool anti-pattern.However, the key is how many percentage analysis accounts for such a request.Usually 80-20 law falls into the sewage pool can be used to analysis the model.When the proportion of anti-pattern is large, you might
consider some open, then pay attention to the lack of isolation layer, can make when a change is more difficult in the future.
Another note layered architecture can make the application become huge, even if the presentation layer and business layer into a separate unit of deployment.Don't need to think about this for some application, but also brings some hidden trouble deployment, such as robustness, reliability, performance and scalability.
The following table shows the layered architecture model of general architectural features of rating and analysis.
The overall flexibility
Analysis: the overall flexibility is the ability of rapid response to environmental changes.Although isolation layer isolation characteristics can be change within the layers, make some changes in the hierarchical structure is also laborious, because most of the model implementation of integrity and usually tight coupling between components.
Easy to deploy
Analysis: depends on how to implement the pattern, especially large applications, deployment will be a big problem.A component of small changes may need to redeploy the entire application (or most).Deployment plan or planned, implemented in non-working time or on weekends.Division architectural patterns, therefore, lead to can't continuous integration, reduces the flexibility of deployment.
Analysis: because the component belongs to a layer, when testing the other layers can be simulated or remove directly, so the model is relatively easy to test.Developers in isolation test can simulate a display layer when the business layer, also can use in test shows the layer to simulate the business layer. performance
Analysis: although some layers performance is good, but it determines the characteristics of a does not bring high performance, because each request processing through many layers.
Analysis: because of this pattern tends to lead to tight coupling, and patterns lead to implementation on the whole is difficult to expand.Can put the physical module deployment of each layer is divided into separate, or copy the entire application multiple nodes, but the overall size is too broad, and difficult to extend.
The development of ease
Analysis: the layered architecture on the development is relatively easy.Because everyone is very familiar with this architecture, and it is easy to implement.Because most of the company development projects by layer to distinguish skills, hierarchical structure model for most business applications development company of choice.The company's organizational structure and the relationship between the software architecture called Conway 's law.