Chris Johnston

Web development and design with a little VFX thrown in for fun
  • Home
  • About Me
  • Contact Me
  • Projects
  • Resume

Java 101: Layered Architecture

Published by Chris Johnston on October 2, 2005 12:38 am under Application Development, Articles, Java

I am going to try and start writting a series of very short tutorials on some of the things that I had trouble with when I was learning enterprise Java. The first of these is on what having a Layered Architecture means and why you should implement one. Hopefully this will be the first article in a series.

Layered Architecture

Layered Architecture is a term that you hear all the time in the world of Java, but what does it mean? In a nutshell, it is about putting code into layers—one layer for all the presentation code, one layer for the persistence code, one layer for the business rules, one layer for the object model, one layer for services, and other layers. It is a way of organizing your code that carries several benefits when done correctly.

Organized Code

Why can’t I just put all my code in the same place? This works well for a few classes, but once you have a few more classes, and then a few more, and a few 100 more, you need to organize those classes somehow. Layers is one way to do this. In Java, teh word layers translates to packages. So, in order to create these layers, you simply put your code into different packages. As an example, you could create a package called com.foo.presentation and put all your presentation code into it. Here is an example list of packages for a new application:

  • com.foo.presentation
  • com.foo.business
  • com.foo.persistence
  • com.foo.objectmodel
  • com.foo.services

Abstraction

The better reason for organizing your code into layers is for abstraction and encapsulation. This allows you to keep dependencies at a minimum and allows you join the layers through abstraction instead of through direct instantiation. Basically this means that you persistence layer can exist independently of any of the layers above. And the layers above call the code in the persistence layer through an interface instead of through concrete classes. In a component diagram, this means that you would have dependency arrows going down through the layers instead of both ways. Below is a diagram showing what I mean.

Component Diagram of a Layered Architecture

As you can see from the diagram, all the arrows either travel down through the layers or to the two side layers. These downward arrows represent the direction of dependencies through the application. The advantage of this is that if I build an application correctly, I should be able to swap out any of the layers and replace with entirely different code without breaking any of the other layers. So for example, if I am using Hibernate as my persistence layer, if I set up the correct interfaces I should be able to swap in something like Top Link or iBatis and DAOs and the entire application should continue to work. This is the power that a layered architecture gives you.

This is a very simple introduction to the concept of layered architecture, there are entire books that can be written on the subject. The best of which is probably Applying UML and Patterns : An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition) by Craig Larman. This is one of those books that every software developer should read. It introduces not only Object-Oriented analysis and design using UML, but also design patterns and speaks at length about layered architectures in software development.

Feel free to leave comments on what I have written. If you have questions or issues with/about what I have written, please let me know through the comments section.

Resources and Links

  • MVC and Layered Architectures in Java
  • Model-View-Controller — and implementation of a layered architecture

1 Comment so far

  1. David law on August 8th, 2007

    The model is well used to categorize device functionality and the certain layers form the primary focus of study for this course.

Posting your comment.

  • Search

  • Categories

    • .NET (2)
    • Agile (41)
    • Apple Mac (15)
    • Application Development (124)
    • Articles (4)
    • ColdFusion (2)
    • Demo/Tutorial (3)
    • Eclipse (1)
    • Flash (6)
    • General (567)
    • Git (1)
    • Google (1)
    • Hibernate (4)
    • J2EE (39)
    • Java (111)
    • Java Frameworks (5)
    • Links (1)
    • Linux (33)
    • Miscellanous (2)
    • NetBeans (3)
    • News (10)
    • Open Source (6)
    • Photography (2)
    • Programming (33)
    • Python (1)
    • Ruby (27)
    • Ruby on Rails (14)
    • Ruby on Rails Web Apps (1)
    • Software (14)
    • Spring (4)
    • Teaching (1)
    • TeamDocs (6)
    • Technology (2)
    • Test Driven Development (1)
    • Thoughts (33)
    • ThoughtWorks (8)
    • Tips and Tricks (1)
    • User Experience (1)
    • Web Design (7)
    • Web Development (37)
    • Wicket (1)
  • Archives

    • September 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (7)
    • March 2009 (2)
    • February 2009 (6)
    • January 2009 (4)
    • December 2008 (3)
    • October 2008 (1)
    • September 2008 (2)
    • August 2008 (6)
    • July 2008 (4)
    • June 2008 (1)
    • May 2008 (8)
    • April 2008 (7)
    • March 2008 (2)
    • February 2008 (1)
    • January 2008 (5)
    • December 2007 (3)
    • November 2007 (4)
    • October 2007 (5)
    • September 2007 (2)
    • August 2007 (3)
    • July 2007 (6)
    • June 2007 (5)
    • May 2007 (5)
    • April 2007 (5)
    • March 2007 (6)
    • February 2007 (9)
    • January 2007 (16)
    • December 2006 (6)
    • November 2006 (15)
    • October 2006 (17)
    • September 2006 (27)
    • August 2006 (22)
    • July 2006 (14)
    • June 2006 (10)
    • May 2006 (18)
    • April 2006 (3)
    • March 2006 (6)
    • February 2006 (15)
    • January 2006 (7)
    • December 2005 (11)
    • November 2005 (8)
    • October 2005 (18)
    • September 2005 (24)
    • August 2005 (18)
    • July 2005 (21)
    • June 2005 (14)
    • May 2005 (23)
    • April 2005 (18)
    • March 2005 (34)
    • February 2005 (27)
    • January 2005 (27)
    • December 2004 (15)
    • November 2004 (17)
    • October 2004 (20)
    • September 2004 (10)
    • August 2004 (21)
    • July 2004 (9)
    • June 2004 (11)
    • May 2004 (4)
    • April 2004 (15)
    • March 2004 (12)
    • February 2004 (7)
    • January 2004 (17)
    • December 2003 (11)
    • November 2003 (8)
    • October 2003 (12)
    • September 2003 (12)
    • August 2003 (12)
    • July 2003 (23)
    • June 2003 (22)
    • May 2003 (14)
    • April 2003 (9)
    • March 2003 (22)
    • February 2003 (24)
    • January 2003 (32)
    • December 2002 (11)
    • November 2002 (16)
    • October 2002 (10)
    • September 2002 (9)
    • August 2002 (13)
  • Pages

    • About Me
    • Contact Me
    • Projects
    • Resume

Copyright © 2010 Chris Johnston
WordPress Theme based on Light Theme