behinddwalls
behinddwalls

Reputation: 618

Serialization: Converting from One Class to another class

I have 2 classes FormatA & FormatB. They hold the same data in 2 different formats for easy access for processing.

Now at some point in my application, I want to create FormatB from FormatA.

I think there should be Serializer which Serializes from FormatA to FormatB and deserializes. Now, based on the definition of Serialization, my thought process doesn't sound correct.

Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. Simple......Coverting an object to bytes and bytes back to object.

What is the right thing in this case to achieve the goal? I have few other possibilities:

  1. Let class FormatA & FormatB have methods for transforming to another format.

  2. Have utility to do the transformations with methods like transformToFormatA(formatB) and transformToFormatB(formatA).

Not sure which one is the correct way of doing this?

Upvotes: 0

Views: 1669

Answers (4)

Kedar Tokekar
Kedar Tokekar

Reputation: 418

As you have mentioned FormatA and FormatB contain same data. So it looks like - in the application same data is being used in two different formats. Using serialization for this purpose may be a hack;

Instead you can keep data at one place and present in required format with asXXFormat method as per requirement. e.g. P(x,y) point can be used in Cartesian as well as Polar coordinates. Class Point can expose itself as two views namely Cartesian and Polar.

public interface Cartesian {
    double getX();
    double getY();
}
public interface Polar {
    double getMagnitude();
    double getAngle();
}
public class Point{
    private double x;
    private double y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public Cartesian asCartesian(){
        return new Cartesian(){

            @Override
            public double getX() {
                return x;
            }

            @Override
            public double getY() {
                return x;
            }
        };
    }

    public Polar asPolar(){
        return new Polar(){

            @Override
            public double getMagnitude() {
                return Math.sqrt(x*x + y*y);
            }

            @Override
            public double getAngle() {
                return Math.atan2(y, x);
            }
        };
    }
}

Upvotes: 2

user265732
user265732

Reputation: 585

As you already understood, serialization is not the process of creating one class's object out of another class's object. You are looking for conversion. Since you didn't mention the content of these classes, let's say that FormatA have a String attribute while FormatB holds an Integer. Conversion logic will be to turn the Integer into a String and vice versa. In order to do so you can use one of the following: 1. Non class (probably static) methods, one per conversion direction. 2. Class method, e.g. toA, toB 3. A sort of a copy constructor- FormatA(FormatB b) - and vice versa.

Upvotes: 0

Blizzardman
Blizzardman

Reputation: 46

You can try using the Transformer Design Pattern as a possibility or use a Java tool such as Dozer.

Upvotes: 1

wonde
wonde

Reputation: 1181

I think what you are looking for is a toObject method, in your case toClassA in class B and toClassB in class A then you can implement whatever logic you want in those methods. This is similar to toString function in java , if you think about it, String is an object we are basically converting object/'changing the format' of the object to string

Upvotes: 0

Related Questions