Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:53:50+00:00 2026-05-16T23:53:50+00:00

I have the following two classes and I am starting to see a pattern

  • 0

I have the following two classes and I am starting to see a pattern that even with my little Java background is screaming for a fix. Every new Object is going to require a set of Actions and the number of classes could grow out of hand. How do I refactor this into a generic DeleteAction class?

I know some of the answers will be use Hibernate, or JPA, or some Framework, but at the moment I can’t utilize any of those tools. Oh, and our server only has jdk 1.4 (don’t ask!). Thanks.

public class DeleteCommitmentAction implements ControllerAction {

  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    CommitmentListDAO clDAO = new CommitmentListDAO();
    CommitmentItemForm ciForm = new CommitmentItemForm(clDAO);
    CommitmentItem commitmentItem = ciForm.deleteCommitmentItem(request);

    RequestDispatcher view = request.getRequestDispatcher("views/commitmentView_v.jsp");
    view.forward(request, response);
  }
}

.

public class DeleteProgramAction implements ControllerAction {

  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    ProgramDAO prgDAO = new ProgramDAO();
    ProgramForm prgForm = new ProgramForm(prgDAO); 
    ProgramForm prg = prgForm.deleteProgram(request);

    RequestDispatcher view = request.getRequestDispatcher("views/programView_v.jsp");
    view.forward(request, response);
  }
}

The approach that I think I need to take is to make interfaces. Starting with the DAO, I have created the following interface.

public interface GenericDao {
  public void create(Object object, STKUser authenticatedUser) throws DAOException;
  public void retreive(String id, STKUser authenticatedUser) throws DAOException;
  public void update( final Object object, STKUser authenticatedUser) throws DAOException;
  public void delete(String id, STKUser authenticatedUser) throws DAOException;
}

And then in my DeleteAction class I tried this

GenericDao gDAO = new GenericDao();

but Eclipse is stating “Cannot instantiate the type GenericDao” So now I am lost.

Update: Based on Péter Török’s answer, here is what I have:

This is the servlet specific for handling operations on Commitment Items:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String schema = General_IO.getSchemaPath("TPQOT_463_COMMITMENT", request.getServerName());
    CommitmentListDAO clDAO = new CommitmentListDAO();
    CommitmentItemForm ciForm = new CommitmentItemForm(clDAO);
    CommitmentItem commitmentItem = new CommitmentItem();

    // I think this is the Application Controller Strategy
    actionMap.put(null, new ListCommitmentsAction());
    actionMap.put("list", new ListCommitmentsAction());
    actionMap.put("view", new ViewCommitmentItemAction(schema));
    //actionMap.put("delete", new DeleteCommitmentAction(schema));
    // Change to the Generic DeleteAction and pass in the parameters
    actionMap.put("delete", new DeleteAction(ciForm, commitmentItem, schema,  "views/commitmentDeleteConfirm_v.jsp",  "views/commitmentView_v.jsp" ));
    // When happy with this approach, change other actions to the Generic Versions.


    actionMap.put("sqlConfirmDelete", new DeleteCommitmentConfirmAction());
    actionMap.put("edit", new EditCommitmentItemAction(schema));
    actionMap.put("sqlUpdate", new UpdateCommitmentItemAction1(schema));
    actionMap.put("new", new NewCommitmentFormAction(schema));
    actionMap.put("sqlInsert", new InsertCommitmentItemAction1(schema));

    String op = request.getParameter("method");
    ControllerAction action = (ControllerAction) actionMap.get(op);

    if (action != null) {
        action.service(request, response);
    } else {
        String url = "views/errorMessage_v.jsp";
        String errMessage = "Operation '" + op + "' not a valid for in '" + request.getServletPath() + "' !!";
        request.setAttribute("message", errMessage);
        request.getRequestDispatcher(url).forward(request, response);
    }
}

And here is the Generic DeleteAction:

public class DeleteAction implements ControllerAction {

  private Form form;
  private Object obj;
  private String schema = null;
  private String xPage;
  private String yPage;

  public DeleteAction(Form form, Object item, String schema, String yPage, String xPage) {
    this.form = form;
    this.item = item;  //passed in javabean??
    this.schema = schema;
    this.xPage = xPage;
    this.yPage = yPage;
  }

  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    item = form.delete(request);

    /* Database schema is described in xml files.
    Hash maps of field names, sizes, and titles; foreign key names, titles, 
    lookup tables; and primary keys information are used to dynamically 
    build HTML forms in the views.
    */      
    HashMap test = ReadTableSchema.returnSchema(schema);
    HashMap hshFields = (HashMap) test.get("hshFields");
    HashMap hshForeignKeys = (HashMap) test.get("hshForeignKeys");
    HashMap hshPrimaryKeys = (HashMap) test.get("hshPrimaryKeys");

    request.setAttribute("hshFields", hshFields);
    request.setAttribute("hshPrimaryKeys", hshPrimaryKeys);
    request.setAttribute("hshForeignKeys", hshForeignKeys);

    request.setAttribute("item", item);
    request.setAttribute("form", form);
    request.setAttribute("pageName", "Delete");

    //Check for deletion authorization if successful forward to the confirmation page
    if (form.isSucces()) {
      request.setAttribute("message", "Please confirm permanent deletion of the data below.");
      RequestDispatcher view = request.getRequestDispatcher(yPage);
      view.forward(request, response);
    } else {
      // Not authorized to delete the data so just re-display
      RequestDispatcher view = request.getRequestDispatcher(xPage);
      view.forward(request, response);
    }
  }
}

then here is the interface (right now just for delete) that will be used by all forms.

public interface CRUD {
    public Object delete(HttpServletRequest request);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:53:50+00:00Added an answer on May 16, 2026 at 11:53 pm

    You can’t instantiate an interface, you need a concrete subclass for that. However, creating concrete subclasses just increases the number of classes, which you are trying to avoid. It is better to use composition instead of inheritance.

    Namely, if you manage to make a common interface for the forms, and hide the actions deleteCommitmentItem, deleteProgram etc. behind one single method, you can parametrize your action instances with the required form (or a factory to provide this), e.g.:

    public class GenericAction implements ControllerAction {
      private Form form;
      private String page;
    
      GenericAction(Form form, String page) {
        this.form = form;
        this.page = page;
      }
    
      public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    
        Item item = form.performDelete(request);
    
        RequestDispatcher view = request.getRequestDispatcher(page);
        view.forward(request, response);
      }
    }
    
    ...
    CommitmentListDAO clDAO = new CommitmentListDAO();
    CommitmentItemForm ciForm = new CommitmentItemForm(clDAO);
    GenericAction deleteCommitmentAction = new GenericAction(ciForm, "views/commitmentView_v.jsp");
    
    ProgramDAO prgDAO = new ProgramDAO();
    ProgramForm prgForm = new ProgramForm(prgDAO); 
    GenericAction deleteProgramAction = new GenericAction(prgForm, "views/programView_v.jsp");
    

    Thus you need no new classes for new kinds of actions, just instantiate GenericAction with different parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two classes: import java.io.*; import java.util.*; public class User {
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
Suppose we have following two classes: class Temp{ public: char a; char b; };
I have the following two classes: TcmTPDataPanel = class(TcmTPBasePanel) Database: TnxDatabase; Session: TnxSession; private
I have the following two classes: package { import flash.display.Sprite; import flash.events.Event; public class
I have the following two classes, one inherits from the other Class A{ void
I have the following two classes: public class Address { public string AddressLine1 {
I have the following two classes. class Settings { function __CONSTRUCT() { echo Settings
Let's say I have the following two classes: public class Person { public string
Let's say, I have the following two classes: class A(object): def __init__(self, i): self.i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.