0

I'm making a project based on MVC pattern, but I have doubts about the location of the methods (insert, update and delete), I don't know if it is in the Model classes or is in another part (please look the code). Another doubt about this is the interfaces (class Car implements CarInterface { ... }), it's necessary or can I avoid it?

Controller:

class ServletCar extends HttpServlet {
   ...
   public void doPost( ... )
   {
       ...
       switch (action) {
           ...
           case 'insert':
               Car n = new Car();
               n.set( request );
               n.insert();
               ...
               break;
           ...
       }
       ... 
   }
}

View (Car.jsp):

...
<form action="ServletCar" ...>
    ...
</form>
...

Model:

class Car {
    // attributes
    ...

    // gets ands sets
    ...

    // functions i,u,d
    public void insert( ... ) { ... }
    public void update( ... ) { ... }
    public void delete( ... ) { ... }

    // other methods
    ...
}

3 Answers3

0

it is better to use interface, so if you'l change CarMobel to MotoModel, you'l be sure that u have properly named methods and it would be easily accessed

0

Here the Car does not seem to be a bean. It seems to be a data access object. It's always better to design a separate bean and dao objects.

Interface can be implemented in dao classes.

Also there should be a redirect to view. Are you using simple servlets mvc? In that case we need to put request dispatcher.

Parth Joshi
  • 452
  • 4
  • 6
0

you should create a service layer, so in your controller you only redirect to the proper service which make all the logic. Something like this:

class ServletCar extends HttpServlet {
   private CarService carService;
   public void doPost( ... )
  {
   ...
   switch (action) {
       ...
       case 'insert':
           carService.insert(carParam)
           break;
       ...
   }
   ... 
  }
}

public interface CarService {
    save(Car car);
    update(Car car);
    delete(Car car);
}

public class CarServiceImpl implements CarService {

    save (Car car) {
     ...
    }

}

In your controller just redirect to the service which have all business logic, in your service all business logic, data access,...

cralfaro
  • 5,822
  • 3
  • 20
  • 30
  • OK but I have another question according to your code, as you know is MVC, that means M is for Car, V is for JSP, and C is for ServletCar, in what package I will put CarService and CarServiceImpl ?? thanks. – Crishk Corporation Mar 14 '16 at 13:14
  • you can have a package called org.app.model, with all yours beans, and 2 more packages, org.app.persistence for yours DAO objects, repositories, and org.app.service for yours services – cralfaro Mar 14 '16 at 13:35