How do I save and load different types of objects?

While coding, I often come across this situation:

  • I have several objects ( ConcreteType1

    , ConcreteType2

    , ...) with the same base type AbstractType

    that has abstract methods save

    and load

    . Each object can (and should) store some data of a certain type by overriding the method save

    .
  • I have a list of objects AbstractType

    that contains various objects ConcreteTypeX

    .
  • I go through the list and method save

    for each object.

At this point, I think this is a good OO design. (Or am I wrong?) Problems start when I want to reload data:

Each object can load its own data, but I have to know the specific type in advance, so I can instantiate ConcreteTypeX

and call the method load

. Therefore, the loading method needs to know a lot about the specific types. I usually "solved" this problem by writing some kind of marker before the call save

, which is used by the loader to determine the correct one ConcreteTypeX

.

I was always in a bad mood. It looks like some kind of anti-pattern ...

Are there any better ways?

EDIT: Sorry for the confusion, I rewrote some of the text. I know serialization and maybe there is some perfect solution in Java / .NET / yourFavoriteLanguage, but I am looking for a general solution that might be better and more "OOP-ish" compared to my concept.

+1


source to share


3 answers


If you can't just use serialization, I would still definitely pull the object loading logic out of the base class. Your instinct is correct, allowing you to correctly identify the smell of the code . The base class should not change when you change or add derived classes.



The problem is that something has to load data and instantiate those objects. This sounds like a job for the Abstract Factory Pattern .

+2


source


Is this .NET or Java? If so why aren't you using serialization?



+3


source


There are better ways, but let's take a step back and take a conceptual look at it. What are all objects doing? Loading and saving. When you get an object from memory, you really don't have to worry about whether it gets its information from a file, database, or Windows registry. You just want the object to be loaded. This is important to remember because later on your maintanence programmer will look at the LoadFromFile () method and wonder, "Why is it called that way since it really doesn't load anything from a file?"

Secondly, you are faced with a problem that we all face, and it is based on division of work. You need a layer that handles getting data from a physical source; you need a layer that manages this data, and you want the layer to display this data. This is the essence of N-Tier Development . I have linked to an article that discusses your problem in detail and details how to create a data access layer to solve the problem. There are also numerous code projects here and here .

If you're looking for Java, just replace "java" with .NET and search for "Java N-Tier development". However, apart from syntactic differences, the design structure is the same.

0


source







All Articles