简体   繁体   中英

Design interfaces with/without specific classes implementation

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);

      model.addAttribute(name);
      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.

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:

@Inject
UserService userService;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM