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
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.
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. 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?? 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.
You basically ask why not use Map<String, Object>
instead of domain objects.
I can't imagine too many things which are worse for maintainability than Map<String, Object>
. Heara are just a few reasons:
Map<String, Object>
it's hard to know where this pseudo-class is actually used. Unlike "find references" of a normal class.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.