I have a UI with Create , Update , Delete and Select Operations , so for this i thought to design it using Command Pattern
MyServlet
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
InsertCommand insertCommnd = new InsertCommand();
DeleteCommand deleteCommnd = new DeleteCommand();
// Create the DTO Employee with request parameters and pass it to invoker
if(req.getParameter("action").equals("insert"))
Invoker invoker = new Invoker();
invoker.setCommand(insertCommnd );
invoker.pressButton(emp);
}
//*Invoker*
public class Invoker
{
private Command command;
public void setCommand(Command command)
{
this.command = command;
}
public void pressButton(Employee emp)
{
command.execute(emp);
}
}
//Command.java
public interface Command
{
public void execute();
}
DeleteCommand.java
public DeleteCommand implements Command
{
Employee emp;
public DeleteCommand(Employee emp)
{
this.emp = emp;
}
public void execute()
{
// SQL Query to delete Records
}
}
InsertCommand.java
public InsertCommand implements Command
{
Employee emp ;
public InsertCommand(Employee emp)
{
this.emp = emp;
}
public void execute()
{
// SQL Query to insert Records
}
}
Similarly there is a Update Records command
My question is , how can i handle this in case of Select Operations as it returns a ArrayList from Database ?
Also please share your ideas on this design ,as i am new to Designing Software .
You are in the right approach meaning that you are trying to use design patterns in your code.
In your case you are using the wrong pattern.You should be using a
DAOpattern instead.But saying wrong doesn’t mean that your approach can’t be used.It is just that
DAOis the standard for DB.In your case your problem is that your
executemethod returnsvoidand you need a method (extra) that returns aList.You could create a second method for select that returns a
Listand give it a generic name and in your other commands simply throw anUnsupportedMethodexceptionE.g.