John
John

Reputation:

How to learn "separation of concern" in java

In another question, someone told me to implement the following in my java program. But, I am very new to Java and I do not know how to start to convert my simple program into this structure:

Data Access Layer (read/write data)
Service Layer (isolated business logic)
Controller (Link between view and model)
Presentation (UI)
dependency injection. 
program to the interface:

Does that come inside some framework? Should I start learning Spring and this structure will evolve naturally? Or, can I implement above technologies one by one without using a framework?

Upvotes: 3

Views: 6443

Answers (6)

Nishant
Nishant

Reputation: 55866

In short:

  • Data Access Layer is a module of your application that provides interface to your data. Data may be in SQL database, XML, file wherever. You write interfaces and classes that provide interface to access data usually as VO or DTO via DAOs
  • Service Layer contains most of the use-case logic. Service layer interacts with Data Access Layer to perform tasks in given use case. I did not find a good article on introductory service layer. You may see here and there
  • Controller is the one that interacts with Service Layer and/or Data Access Layer and/or other controllers in order to perform a specified client's tasks.

    For example, a sign-off button controller will request a sign-off action/service to invalidate user's sessions on all services that user is logged on to, then it will choose an appropriate view or log-off web-page to forward user to.
  • Presentation is your user interface. It can be a web-page made of HTML or Java Swing window or anything that user interacts with. GUI commonly known term for it. This is what your users will be interacting with using mouse clicks, scrolls, swipes, drag-and-drop. These actions are mapped with controller which performs action based on what user performed on UI.
  • Dependency Injection is a way to wire various components. There are a lot of resources on web. You can look in Martin Fowler's this article. It's basically a mechanism that allows components to behave much like plug-and-play devices, if you know what plug goes where.

    Spring is a good implementation of dependency injection. You may not want to write your own framework, and at this stage, you should rather not. There is a Spring MVC framework that can do things for you.

But I suggest you start from very basic. Instead of jumping on jargon, read from basic. Start with a good book on application development using Java. You can also look into

Upvotes: 1

duffymo
duffymo

Reputation: 308988

You can implement them without a framework if you wish, but you give up whatever benefits the framework offers you.

The layering you cite is correct and independent of any framework; it's just programming to interfaces and separation of concerns. You're free to do it without Spring if you wish to minimize the number of new technologies you want to learn right now.

If you don't know what persistence is, then you shouldn't jump into Spring. Persistence means storing data in relational databases using SQL to most people. If you don't know that, I'd recommend starting there.

All the patterns books in the world won't help you if you've never used the underlying technologies.

If you've never done any of this, I'd recommend sticking to straight JDBC, servlets, and JSPs using only JSTL (no scriptlets). Anything beyond that will just be confusing.

If you had a Foo model object, with persistence, service, and view tiers, the interfaces might look like this:

package model;

/**
 * A model object that's interesting from your problem's point of view
 */
public class Foo
{
}

package persistence;

/**
 * CRUD operations for a Foo 
 */
public interface FooDao
{
    Foo find(Long id);
    List<Foo> find();
    void saveOrUpdate(Foo foo);
    void delete(Foo foo);
}


package service;

/**
 * Just a data service that wraps FooDao for now, but other use cases would 
 * mean other methods.  The service would also own the data connection and manage 
 * transactions.
 */
public interface FooService
{
    Foo find(Long id);
    List<Foo> find();
    void saveOrUpdate(Foo foo);
    void delete(Foo foo);
}

package view;

/**
 * A class that owns services, validates and binds input from UI, and handles routing 
 * to the next view once service is complete.
 */
public interface FooController
{
   ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response);    
}

These are just interfaces, of course. You'll need to provide implementations.

Upvotes: 2

Hurda
Hurda

Reputation: 4715

If your program is really simple this separation might be done by using one calss for each category.

Data Access Layer (read/write data) -> one class for presisting laoding
Service Layer (isolated business logic) -> one calss with bussiness logic
Controller (Link between view and model) -> in simple swing app this merges with UI
Presentation (UI) -> one class for one widnow
dependency injection -> not used in small apps 
program to the interface -> Your service class should use interface tah is used by other class instead of directly your serivce implementation:

if it's not as simple program you might want to have package for each category.

BUT - don't overdesign! These concepts are ment to help you manage large scale applications, not to ruin you in your programming begginigs!

Upvotes: 0

Sandman
Sandman

Reputation: 9692

It is very hard to answer this question. First of all, I don't know what your program looks like. Second, I don't think 'converting' it is something that can be done, or should be done for that matter. What you're talking about are architectural concepts that the developers usually have in mind while designign the application.
If these concepts interest you, I suggest reading a bit about Model-View-Controller pattern (MVC) and service-oriented Architecture (SOA).
These are general concepts that do not apply specifically to Java. However, they are widely used in Java enterprise development. Various frameworks allow you to create applications utilizing these concepts. For example, Spring Web MVC, as others have pointed out, is part of the Spring Framework that lets you create web applications that adhere to the MVC pattern.

Upvotes: 0

kvista
kvista

Reputation: 5059

You can implement all of this is you want -- it's been done many times before, but nothing prevents you from doing it again.

What would be a better use of your time is to make sure you understand the separation of concerns you listed above (which are generally right) and identify the most efficient integration of existing frameworks to leverage (e.g., Hiberante, Spring, Guice, etc). There are multiple answers for that one (and no shortage of opinions!), but all things being equal, the less frameworks you have to integrate, the easier and better fitting it's likely to be.

Spring has a very well known framework which covers many of these things, so it would be wise to start there. It also allows you to work with other frameworks (i.e., you can use selective parts of Spring). For example, you can use Spring for dependency injection and use a different MVC framework.

Upvotes: 0

Brandon Joyce
Brandon Joyce

Reputation: 3110

You might want to check out Domain Driven Design. The Code samples are in Java. The things you listed are design related more than any specific technology.

Upvotes: 1

Related Questions