I think the logic of printBillingLog and printOrderLog function are similar.
So I want to use a generic way to reduce the function code.
I think the code as below , but I had got stock.
Now I don't know how to create OrderLog( the constructor's paramter is Order ) and BillingLog( the constuctor's paramter is Billing) in generic way. And how to call the setAction() method in each xxxLog entity.
Can somebody teach me how to solve the problem?
Billing、BillingLog、Order、OrderLog class as below
You'll need an additional interface:
You'll then get implementing classes BillingLogFactory implements LogFactory<Billing, BillingLog> and OrderLogFactory implements LogFactory<Order, OrderLog>. The NewPrintLog class then changes somewhat:
For a little more performance you can make singletons out of your factories:
You can then simply pass BillingLogFactory.getInstance() to your printLog method.