gpt4 book ai didi

android - 整洁的架构 - 进行数据模型映射的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 00:12:16 25 4
gpt4 key购买 nike

我读过这个令人印象深刻的 article关于具有整洁架构和 MVP 的 android 中的数据建模。

现在我想重构我域中的一些现有模型,以便它们不包含可打包代码(android 代码)并且经过简化以在特定 View 中工作。你知道有时我们必须改变模型,以便它在 View 中工作,例如为了在 RecyclerView 中获得选定的位置,我们会向模型添加一个名为“selectedPosition”的字段。有很多次我们需要更改模型,然后我们最终得到的模型不纯粹并且有点难以维护。

具体来说,我有 3 个模型数据用于我正在使用的 3 个支付系统。 3 个模型中的所有字段都不同。他们对字段有不同的名称。有人可以向我展示用于使所有 3 个模型使用通用模型的架构示例吗?

最佳答案

数据模型

我确信您的 3 种支付系统的 3 种模型具有共同的特征。所以你可以把这个功能放在界面上。您的每个模型都必须实现此接口(interface)。在您的情况下,它应该显示一个数据模型。

例如:

class Info {
int id;
String cardNumber;
.......
}

interface ITransactionable { //or abstract class with the common func and prop
void addUserWithCard(String cardNumber, String name);
boolean makeTransaction(\*some params*\);
Info getPaymentUserInfo();
}

class Model1/2/3 implements ITransactionable {
\*In each case methods doing different job but give you same result,
you can take info for your models from servers, clouds, db...*\
}

领域模型

领域模型代表您的业务逻辑,操纵您的数据模型。

class DonationService {
ITransactionable paymentService;
DonationService(ITransactionable paymentService) {
this.paymentService = paymentService
}
Info makeDonation(int money) {
paymentService.addUserWithCard("4546546545454546", "Vasya");
paymentService.makeTransaction(money);
return paymentService.getPaymentUserInfo();
}
........
}

每一层都必须为下一层提供 API 之类的东西。

演示文稿

例如可以用每笔交易的数据填充recyclerView。并从 View 中获取事件,例如获取有关交易的详细信息或进行新交易。

您可以查看它以了解它是如何实现的:https://github.com/android10/Android-CleanArchitecture

关于android - 整洁的架构 - 进行数据模型映射的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47968093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com