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

  • Home
  • SEARCH
  • 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 5938081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:38:02+00:00 2026-05-22T15:38:02+00:00

In java i am writing some DTO objects, which all inherit from AllocationDTO .

  • 0

In java i am writing some DTO objects, which all inherit from AllocationDTO.
A list of these objects are then passed into a DAO object for saving to the database.

Depending on which subtype of AllocationDTO is being saved the saving logic changes (e.g. which table in the database to save to etc.)

I find myself using code as such:

for (AllocationDTO x : listOfAllocationDtos) {
    if (x instanceof ManagerAllocationDTO) {
         Manager m = (x(ManagerAllocationDTO)).getManager();
         // save manager etc to managerallocs
    } else if (x.getId() == AllocationDTO.TYPE_SPECIAL1) {
         // save to specialAlloc1 table
    } else if (x.getId() == AllocationDTO.TYPE_SPECIAL2) {
         // save to specialAlloc2 table
    }
}

The ManagerAllocationDTO has an extra field relating the allocation to a manager, but for the specialalloc1/2 cases I have not made a subtype because the only difference in the data is the table it is saved to.

My question is a bit of a soft design question – is this the best way to go about doing this?

  • 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-22T15:38:03+00:00Added an answer on May 22, 2026 at 3:38 pm

    One way to seperate different instances, without instanceOf and if-else-cascade is to use the Visitor Design pattern.

    • new Interface: AllocationVisitor with one method for each concrete subclass of AllocationDTO:

      • visit(TYPE_SPCIAL1 dto)
      • visit(TYPE_SPCIAL2 dto)
    • AllocationDTO: abstract void acceptVisitor(AllocationVisitor visitor)

    • Each concreate subclass of AllocationDTO implements the acceptVisitor() this way:
      void acceptVisitor(AllocationVisitor visitor){visit(this);} //the correct visit method is choosen by compile time type.
    • Your DTO implements the AllocationVisitor Interface (with an inner class), create an instance of this, and passes it to the elements of listOfAllocationDtos.

    DAO:

    AllocationVisitor saveVisitor = new AllocationVisitor() {
      visit(TYPE_SPCIAL1 dto) {//what ever you need}
      visit(TYPE_SPCIAL2 dto) {//what ever TYPE_SPCIAL2 needs}
    }
    for (AllocationDTO x : listOfAllocationDtos) {
      x.visit(saveVisitor);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about writing some code to allow access to a Java
Please suggest some good resources to start writing Java Web services.
Me and some friends are writing a MORPG in Java, and we would like
I am writing a desktop application written in Swing developed using Java 1.5. Some
I'm in Eclipse writing Java. I come from Visual Studio with Resharper writing C#.
I'm writing a Java Tree in which tree nodes could have children that take
I am writing a Java applet which loads dll's created in unmanaged C++. I
I'm writing some software in which I need to compute bivariate densities of an
Hello All I am writing some software that will allow users to create their
I'm writing a Java application (it is monitoring some files on disk and based

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.