javaarchitecturesoftware-designthree-tier

How does the Three Tier architectural style works? Some handy example


I have been provided with a Java Application on which to apply the Three-Tier architectural style; One of the use cases to do is the login. I've studied all the theory and rules that apply to this architectural style, but I need to understand the logic of the objects co-operation between the various levels and how patterns work together on each level to realize this (and the others) use case.

First, I've created the three basical packages: Presentation, Application and Data. In addition, I included one more package about the Boundary classes, The various GUIs from which requests will be sent.

In the Presentation layer, I just put a Front Controller, that encapsulates the presentation logic required by clients using the application.

In the Data layer, I put a DatabaseConnection class (Class that communicates with the database and that is responsible for loading the driver, connecting to the database, querying etc.) and DAOs classes (Data Access Object, that interfaces with the database).

The real problem is that I don't know what to put in the Application level, which represents the main part of the application, defining the domain model of the application, that is: their entities, their relationships, and application logic. It should not contain any reference to how the data will be presented to the user or how they will be saved.

So, I currently have this hierarchy:

Main ---> Boundary > Presentation > Application > Data > Database

In accordance with this architecture, how can I make a simple login? Bearing in mind that each level can communicate ONLY with the underlying level; For example, a class in Boundary layer cannot comunicate directly with a class in Data layer, Boundary's classes can comunicate with Presentation's classes only. If necessary, you can post a pseudocode, which makes the idea of the steps to do.


Solution

  • Your Boundary calls only basic methods on the Presentationlayer.

    Let's say the User clicks a Button to create a User the flow would be the following: Boundary calls method createUser(String name, int age) on the FrontController (Presentationlayer). The Controller can check some basic (UI-Related) things and would then call a similar method on the Applicationlayer.

    The Applicationlayer can now process some further checks (for example: is the Current Active User allowed to create a User?). The Applicationlayer takes the given informationen (name and age), creates a DAO based on that and invokes the method to create the User on the Datalayer (DAO).

    The Datalayer simply inserts the given information.