在Android上共享领域字段 [英] Sharing Realm fields on Android

查看:52
本文介绍了在Android上共享领域字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android上的领域不支持模型继承/多态性.

Realm on Android doesn't support model inheritance/polymorphism.

那么有什么方法可以在Android上共享字段?我们有5个模型,它们共享相同的同步相关字段和代码.我们在当前的SQLite模型中使用继承;如果我们切换到Realm,我们唯一的选择是在5个模型类中的每个模型类之间复制同步字段吗?

So is there any way to share fields on Android? We have 5 models that all share the same synchronization-related fields and code. We use inheritance in our current SQLite models; if we switch to Realm, is our only choice to duplicate the sync fields across each of the 5 model classes?

作为一种解决方法,我正在考虑让这些类实现带有共享字段的getter和setter的Syncable接口,至少让我共享同步功能.有更好的方法吗?

As a workaround, I'm thinking about having those classes implement a Syncable interface with getters and setters for the shared fields, which at least let me share sync functionality. Is there a better way?

要共享同步功能,我最好的猜测是制作一个静态Synchronizer类并将其传递给Syncable模型对象. Synchronizer方法将使用Syncable接口直接在模型对象的共享的,与同步相关的字段上进行操作,并在特定于类型的字段上进行委派操作.或者,我可以为每个模型对象提供其自己的Synchronizer实例...

To share sync functionality, my best guess is to make a static Synchronizer class and pass it Syncable model objects. Synchronizer methods will use the Syncable interface to operate directly on model objects' shared, sync-related fields and delegate operations on type-specific fields. Alternatively, I could provide each model object its own Synchronizer instance...

试图找到解决继承限制的正确方法实际上是在扩展我的OOP技能...我们非常感谢!

Trying to find the right way to work around the inheritance limitation is really stretching my OOP skills... help is appreciated!

推荐答案

当我发现realmObjects应该直接从RealmObject类继承(不支持继承)时,我遇到了同样的问题. 为了获得多态性的好处,我考虑了与您类似的解决方案,并结合了一些合成技巧,这些技巧可以避免属性重复.

I had the same issue when I found out that realmObjects should inherit directly form RealmObject class (no support for inheritance). In order to get back the benefits of polymorphism, I considered a similar solution to yours combined with some composition tricks that would avoid me attribute duplication.

通话很便宜,请告诉我代码."

"Talk is cheap show me the code."

代码示例

interface IPerson {
    String getName();
}

class Person extends RealmObject implements IPerson {

    String name;

    @Override
    public String getName() {
        return name;
    }
}

interface IWorker extends IPerson {
    int getSalary();
}

class Worker extends RealmObject implements IWorker {

    Person person;
    int salary;

    @Override
    public String getName() {
        return person.getName();
    }

    @Override
    public int getSalary() {
        return salary;
    }
}

一些好处

您不必在每个扩展类中重复属性.

Some benefits

You won't have to duplicate your attributes in each extending class.

多态又回来了!例如,现在您可以模拟转换(在此示例中使用getPerson()).

Polymorphism is back! For example, now you can simulate a cast (with getPerson() in this example).

当使用使用反射的序列化库(假设它是Gson)时,您的序列化模型将嵌入其父级属性.如果您使用的是经典继承,那将不是您想要的.

When using a serialization library that uses reflection (suppose it's Gson), your serialized models will have their parents attributes embedded. Not something that you would have had if you were using classic inheritance.

让我们假设约翰·多伊(John Doe)每月能赚500美元. (他是工人,是个人吗?).

Let's suppose John Doe is making 500$ a month. (He's a Worker and a Person right?).

通过经典继承,John Doe看起来像这样:

With classic inheritance, John Doe would look like this:

{
  "name":"John Doe",
  "salary": 500
}

但是有了这种继承的解决方法……:

But with this inheritance workaround ... :

{
  "person" : {
  "name":"John Doe"
  },
  "salary": 500
}

希望这会有所帮助!

很遗憾,必须复制主键.

PrimaryKeys unfortunately have to be duplicated.

您可能要检查 RealmFieldNamesHelper ,它是由Christian Melchior 使Realm查询的类型更加安全".

You might want to check RealmFieldNamesHelper, a library made by Christian Melchior "to make Realm queries more type safe".

这篇关于在Android上共享领域字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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