简体   繁体   English

如何保存从抽象类扩展的不同类

[英]How to save different classes extended from an abstract class

I have an application with many DisplayFields (rendered on a canvas) eg text, boolean, date etc. These DisplayFields have many variables in common such as color, shadow parameters, fill etc. 我有一个带有许多DisplayField(在画布上渲染)的应用程序,例如文本,布尔值,日期等。这些DisplayField具有许多共同的变量,例如颜色,阴影参数,填充等。

Because of these common values, I have defined BasicField as an abstract class. 由于这些通用值,我已将BasicField定义为抽象类。

From this abstract class I extend DisplayFieldText, DisplayFieldBoolean etc. Each of these is a separate class with additional variables specific to the field's function. 从这个抽象类中,我扩展了DisplayFieldText,DisplayFieldBoolean等。它们都是一个单独的类,具有特定于该字段功能的其他变量。

Now I need to save all DisplayFields to disk. 现在,我需要将所有DisplayField保存到磁盘。 So each field needs to save its core (Abstract Class variables) as well as its own parameters. 因此,每个字段都需要保存其核心(抽象类变量)以及其自己的参数。

How do I go about this without lots of duplication of code? 我如何在没有大量重复代码的情况下解决这个问题?

Caveat: I'm not sure how you're going about saving your data to disk, so this might not apply. 警告:我不确定您打算如何将数据保存到磁盘,因此这可能不适用。

I'm going to assume that you're currently saving to disk using some structure that looks something like this: 我假设您当前正在使用某种类似于以下内容的结构保存到磁盘:

class DisplayField {
    private int data;

    public void saveData() {
        // save data here
    }
}

This is a perfectly acceptable way to save your data, but when you introduce the parent class it would get kind of messy: 这是保存数据的一种完全可接受的方法,但是当您引入父类时,它会变得有些混乱:

abstract class BasicField {
    public String sharedData1;
    public double sharedData2;
}

class DisplayField extends BasicField {
    private int data;

    public void saveData() {
        // save parent's data every time in every subclass!! Not fun!
        // save DisplayField's private data
    }
}

A good way to solve this problem is to use polymorphism. 解决此问题的一个好方法是使用多态。 Since all the subclasses of BasicField are going to need to save their data, you can define the saveData() method in BasicField, and call it in the subclasses. 由于BasicField的所有子类都需要保存其数据,因此您可以在BasicField中定义saveData()方法,然后在子类中调用它。 This has a lot of other side-benefits, for example if you have a BasicField object, you know that it will be able to save its data. 这还有很多其他好处,例如,如果您有BasicField对象,则知道它将能够保存其数据。 Also if you add more data to the BasicField later, you will only have to change a couple lines of code in BasicField instead of modifying all of its subclasses. 同样,如果以后向BasicField添加更多数据,则只需在BasicField中更改几行代码,而无需修改其所有子类。

abstract class BasicField {
    public String sharedData1;
    public double sharedData2;

    public void saveData() {
        // save sharedData1
        // save sharedData2
    }
}

class DisplayField extends BasicField {
    private int data;

    public void saveData() {
        super.saveData();
        // save DisplayField's private data
    }
}

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

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