序列化:从一个类转换为另一个类 [英] Serialization: Converting from One Class to another class

查看:138
本文介绍了序列化:从一个类转换为另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2类FormatA&格式B。它们以2种不同的格式保存相同的数据,以便于访问以进行处理。

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

现在在我的应用程序中,我想从FormatA创建FormatB。

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

我认为应该有从FormatA序列化到FormatB并反序列化的Serializer。现在,基于序列化的定义,我的思维过程听起来不正确。

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. 让类FormatA& FormatB具有转换为另一种格式的方法。

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

具有实用程序,可以使用 transformToFormatA(formatB) transformToFormatB(formatA)

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?

推荐答案

如前所述,FormatA和FormatB包含相同的数据。因此,看起来-在应用程序中,相同的数据正以两种不同的格式使用。为此目的使用序列化可能会很麻烦;

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;

相反,您可以将数据保留在一个位置,并根据需要使用asXXFormat方法以所需格式显示。例如P(x,y)点可以在笛卡尔坐标和极坐标中使用。

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

这篇关于序列化:从一个类转换为另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆