Design interfaces with/without specific classes implementation

Go To


Ok, i'm designing a web app. so i'm in the point of creating the controllers, first i create the interface with this method signature.

public String user(String code);

in the implementation i'm using spring, so in order to return the value it is required to pass a second parameter to store the value, like this.

public String user(Model model, String code){
      String name = userservice.findUserName(code);

      return "userView";

ok, so as you can see there is a problem because my implementation class is not overriding the method in the interface, but i don't want to add parameters and dependencies to the interfaces project, because i want the interfaces (design) be technology neutral.

Hope some one can give me some advice about. Thank you.

2012-04-04 20:46
"i don't want to add parameters and dependencies to the interfaces project," This sentence requires clarification. Typically in spring once you define a controller method, you add an annotation wiring it to a RequestMapping. We can discuss that, but it sounds like you want your controller to be free of such configuration? That sentence makes it really unclear - Nathaniel Ford 2012-04-04 20:54
You are right, my question was not clear. what i want is to design my interfaces for the web layer, but not to include specific classes from a framework (such as spring) but to allow the the person that implements to use whatever technology decides to use - OJVM 2012-04-09 15:26


I'd suggest you look at some example Spring MVC apps to get an idea of coding conventions, etc.

Controllers don't tend to be written to interfaces - I've never seen it done anyway. You want your service and DAO classes to be though.

So your code for the user method looks like classic service layer, although naming conventions are usually more like getUser or getUserForId. So if you call this from the Controller then you don't have to worry about the Model arg, like this:

model.addAttribute("user", userService.getUserForId(code));

where userService is defined as:

UserService userService;

and UserService is the interface and userService injected with an implementation automagically by Spring/IoC container.

2012-04-05 10:31
by nickdos
I have seen that the web layer (controllers) are not usually designed by interfaces, i do not understand that, because it is supposed to be a good practice (like in the service o data layer). I want to do this, because my system will have some views (jsp's), but also will provide some services to other systems (WITH REST probably) so i am trying to abstract the main operations but not including a specific technology - OJVM 2012-04-09 15:38
Controllers tend to be pathway-specific. I think what you're really reaching for is to define the interfaces for your services? Those services can be agnostic to the framework, as nickdos describes here. Think of controllers really as 'web controllers', wherein their specific function is to take in a web request, call the appropriate service, and package the web response - Nathaniel Ford 2012-04-09 17:05
Tank you for your answer, i am putting into practice those concepts - OJVM 2012-06-05 14:24