简体   繁体   English

序列化:从一个类转换为另一个类

[英]Serialization: Converting from One Class to another class

I have 2 classes FormatA & FormatB. 我有2类FormatA和FormatB。 They hold the same data in 2 different formats for easy access for processing. 它们以2种不同的格式保存相同的数据,以方便进行处理。

Now at some point in my application, I want to create FormatB from FormatA. 现在在我的应用程序中的某个时候,我想从FormatA创建FormatB。

I think there should be Serializer which Serializes from FormatA to FormatB and deserializes. 我认为应该有从FormatA序列化到FormatB并反序列化的Serializer。 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. 让类FormatA和FormatB具有转换为另一种格式的方法。

  2. Have utility to do the transformations with methods like transformToFormatA(formatB) and transformToFormatB(formatA) . 有实用程序可以使用诸如transformToFormatA(formatB)transformToFormatB(formatA)类的方法进行transformToFormatA(formatB)

Not sure which one is the correct way of doing this? 不确定哪种方法正确?

As you have mentioned FormatA and FormatB contain same data. 如前所述,FormatA和FormatB包含相同的数据。 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; 为此目的使用序列化可能是一个hack。

Instead you can keep data at one place and present in required format with asXXFormat method as per requirement. 相反,您可以将数据保留在一个位置,并根据要求使用asXXFormat方法以所需格式显示。 eg P(x,y) point can be used in Cartesian as well as Polar coordinates. 例如,P(x,y)点可以在笛卡尔坐标和极坐标中使用。 Class Point can expose itself as two views namely Cartesian and Polar. Class Point可以将自己公开为两个视图,即笛卡尔和极地。

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);
            }
        };
    }
}

您可以尝试使用Transformer Design Pattern或使用Java工具(例如Dozer)

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. 我知道你在寻找的是一个toObject方法,你的情况toClassAclass BtoClassBclass A那么你可以实现你在这些方法中希望的任何逻辑。 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 这类似于java中的toString函数,如果您考虑一下,String是一个对象,我们基本上是在将对象转换为“ /将对象的格式更改为字符串”

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. 由于您没有提到这些类的内容,因此,假设FormatA具有String属性,而FormatB具有Integer。 Conversion logic will be to turn the Integer into a String and vice versa. 转换逻辑是将Integer转换为String,反之亦然。 In order to do so you can use one of the following: 1. Non class (probably static) methods, one per conversion direction. 为此,可以使用以下方法之一:1.非类(可能是静态)方法,每个转换方向一个。 2. Class method, eg toA, toB 3. A sort of a copy constructor- FormatA(FormatB b) - and vice versa. 2.类方法,例如toA,toB。3.一种复制构造函数-FormatA(FormatB b)-反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM