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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:05:36+00:00 2026-05-21T22:05:36+00:00

OK, first of all of this is done in Java: I deserialize a text

  • 0

OK, first of all of this is done in Java:
I deserialize a text file. Each line results in an object from a totally different class hierarchy.
Then I give these objects to another object, lets call it a “Writer”, which has a method named “write(x)”, which then serializes part of that object into another format.
Because each deserialized object is from a different class hierarchy, I cannot process each object with the same writer (the contents is also slightly different each time, so that’s OK). But what bothers me is, that in the client which is steering this whole process, I have to differentiate between the different objects.

Is there a way, so that the “Writer” can decide for himself, what to do, without the client doing that work for him…

Maybe some pseudo code will help.

for(Line line : lines) {
  x = one of several kinds of objects is instantiated;
  w = one of several kinds of Writers is instantiated, analog to x;
  w.write(x);
}

Ideally my code would look like that, but in reallity, each line (except for the last) is a switch statement, because I have to differentiate the objects…

I hope this is not to confusing.

  • 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-21T22:05:37+00:00Added an answer on May 21, 2026 at 10:05 pm

    Usually, in such cases you should create an interface Writer:

    public interface Writer {
    
      void write(Object o);
    
    }
    

    ,

    abstract class AbstractWriter, which does some common work and then a number of classes which extend this AbstractWriter.

    Then create a Factory class, which will create a necessary writer accordingly to your object type:

    public class Factory {
    
      private Factory() {}
    
      public static Writer createWriter(Object o) {
        Writer result = null;
        if (o.getClass() == MyObject1.class) {
           result = new MyFirstObjectWriter();
        } else if (...) {
           ...
        } else {
           throw new IllegalArgumentException("Unsupported object class" + o.getClass());
        }
        return result;
      }
    }
    

    Then the client code will look like this:

    for (Line line : lines) {
      Object o = readObject(line);
      Factory.getWriter(o).write(o);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry if this has been asked before. I've done a pretty
First of all, this is my code (just started learning java): Queue<String> qe =
First this is all done on Mac OS X, ver 10.6.4 I copied a
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
First of all: This question is not directly programming related. However, the problem only
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
Well, first of all sorry about this question it must be pretty straight forward
First of all there is a partial question regarding this, but it is not
First of all I'm not sure this is even possible, however I need to

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.