[英]What really is the essence of POJOs/models or domain-driven objects in the situation in context considering business requirements changes?
I was told this chunk of code is a really nasty anti-pattern. 有人告诉我这段代码是一种非常讨厌的反模式。
@SuppressWarnings("unchecked")
private static Map<String, Object> getArgs(Object obj) {
return new HashMap<>((Map<String, Object>) obj);
}
public void buildAndUpdateCustomer(List<Object> list) {
for (Object obj : list) {
Map<String, Object> args = getArgs(obj);
daoProvider.updateCustomerName(args);
daoProvider.updateAgingMia(args);
}
}
public void buildAndUpdateTax(List<Object> list) {
for (Object obj : list) {
Map<String, Object> args = getArgs(obj);
daoProvider.updateTaxAmount(args);
}
}
public void buildAndUpdateLedgerBal(List<Object> list) {
for (Object obj : list) {
Map<String, Object> args = getArgs(obj);
daoProvider.updateLedgerBalance(args);
}
}
An argument for that is because: 这样做的原因是:
(Map<String,Object>) obj
包含不必要的强制转换(Map<String,Object>) obj
List<Object> list
My List<Object> list
could be a list of invoice tax amounts, list of customers, list of ledger balances et cetera...which could be rewritten like this. 我的List<Object> list
可以是发票税金额,客户列表,分类帐余额等的列表...可以这样重写。
public void buildAndUpdateCustomer(List<Customer> list) {...}
public void buildAndUpdateTax(List<Tax> list) {..}
public void buildAndUpdateLedgerBal(List<Ledger> list) {..}
This means having to create Customer,Tax and Ledger POJO/entity/domain objects for each method. 这意味着必须为每种方法创建Customer,Tax和Ledger POJO /实体/域对象。 I have got over 100 buildAndUpdate()
of these sort of methods updating and doing different things, do i have to create 100 of these POJO/entity/domain objects?? 我已经有超过100种的buildAndUpdate()
这类方法可以更新并执行不同的操作,我是否必须创建100个这些POJO /实体/域对象? Maybe this is bad practice but i feel like having to add classes all over the place bloats the entire code base and kills maintainability. 也许这是一种不好的做法,但我觉得必须在各处添加类,从而使整个代码库blo肿,并破坏了可维护性。
You basically ask why not use Map<String, Object>
instead of domain objects. 您基本上会问为什么不使用Map<String, Object>
而不是域对象。
I can't imagine too many things which are worse for maintainability than Map<String, Object>
. 我无法想象有太多事情比Map<String, Object>
对可维护性更Map<String, Object>
。 Heara are just a few reasons: Heara只是几个原因:
Map<String, Object>
it's hard to know where this pseudo-class is actually used. 至于源代码的挖掘,由于它只是Map<String, Object>
因此很难知道该伪类的实际使用位置。 Unlike "find references" of a normal class. 与普通类的“查找引用”不同。 Map<String, Object>
. 您不能在Map<String, Object>
强制执行规则。 Basically, you can create a instance which is invalid from the business logic point of view. 基本上,您可以创建一个从业务逻辑角度来看无效的实例。 So, yes, I'd definitely create a 100 domain objects. 所以,是的,我肯定会创建100个域对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.