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 7056461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:52:12+00:00 2026-05-28T03:52:12+00:00

In one of my projects, I have two data transfer objects RecordType1 and RecordType2

  • 0

In one of my projects, I have two “data transfer objects” RecordType1 and RecordType2 that inherit from an abstract class of RecordType.

I want both RecordType objects to be processed by the same RecordProcessor class within a “process” method. My first thought was to create a generic process method that delegates to two specific process methods as follows:

public RecordType process(RecordType record){

    if (record instanceof RecordType1)
        return process((RecordType1) record);
    else if (record instanceof RecordType2)
        return process((RecordType2) record);

    throw new IllegalArgumentException(record);
}

public RecordType1 process(RecordType1 record){
    // Specific processing for Record Type 1
}

public RecordType2 process(RecordType2 record){
    // Specific processing for Record Type 2
}

I’ve read that Scott Meyers writes the following in Effective C++ :

“Anytime you find yourself writing code of the form ‘if the object is of type T1, then do something, but if it’s of type T2, then do something else,’ slap yourself.”

If he’s correct, clearly I should be slapping myself. I don’t really see how this is bad design (unless of course somebody subclasses RecordType and adds in a RecordType3 without adding another line to the generic “Process” method that handles it, thus creating a NPE), and the alternatives I can think of involve putting the brunt of the specific processing logic within the RecordType classes themselves, which really doesn’t make much sense to me since there can in theory be many different types of processing I’d like to perform on these records.

Can someone explain why this might be considered bad design and provide some sort of alternative that still gives the responsibility for processing these records to a “Processing” class?

UPDATE:

  • Changed return null to throw new IllegalArgumentException(record);
  • Just to clarify, there are three reasons a simple RecordType.process() method would not suffice: First, the processing is really too far removed from RecordType to deserve its own method in the RecordType subclasses. Also, there are a whole slew of different types of processing that could theoretically be performed by different processors. Finally, RecordType is designed to be a simple DTO class with minimal state-changing methods defined within.
  • 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-28T03:52:12+00:00Added an answer on May 28, 2026 at 3:52 am

    The Visitor pattern is typically used in such cases. Although the code is a bit more complicated, but after adding a new RecordType subclass you have to implement the logic everywhere, as it won’t compile otherwise. With instanceof all over the place it is very easy to miss one or two places.

    Example:

    public abstract class RecordType {
        public abstract <T> T accept(RecordTypeVisitor<T> visitor);
    }
    
    public interface RecordTypeVisitor<T> {
        T visitOne(RecordType1 recordType);
        T visitTwo(RecordType2 recordType);
    }
    
    public class RecordType1 extends RecordType {
        public <T> T accept(RecordTypeVisitor<T> visitor) {
            return visitor.visitOne(this);
        }
    }
    
    public class RecordType2 extends RecordType {
        public <T> T accept(RecordTypeVisitor<T> visitor) {
            return visitor.visitTwo(this);
        }
    }
    

    Usage (note the generic return type):

    String result = record.accept(new RecordTypeVisitor<String>() {
    
        String visitOne(RecordType1 recordType) {
            //processing of RecordType1
            return "Jeden";
        }
    
        String visitTwo(RecordType2 recordType) {
            //processing of RecordType2
            return "Dwa";
        }
    
    });
    

    Also I would recommend throwing an exception:

    throw new IllegalArgumentException(record);
    

    instead of returning null when neither type is found.

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

Sidebar

Related Questions

I have a solution that contains two projects. One project is an ASP.NET Web
I have two sheets. One that lists all of my projects with columns for
I have two projects in my Solution. One implements my business logic and has
I have two visual studio projects, one written in C#, another written in fortran
In my system I have two different projects, one defining its interfaces and another
I have the following two projects in in Flex Builder 3: One AS3 library
I have several projects open in an Eclipse workspace. Like so: com.harbl.project.one com.harbl.project.two com.harbl.project.three
I have a solution with two projects. One for project for the production code
I have one solution in VS2010 containing two MVC 3 projects. These are two
My problem is that i have two projects in a solution (1x ASP ,

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.