File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generic for construct a class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generic for construct a class" Watch "Generic for construct a class" New topic
Author

Generic for construct a class

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 102
Dear all
I have a Class as below


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


My Way,My Pace
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
Make a 'Log' interface.
Get you logger classes Order and Billing Log implement Log interface
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 102
Manish Singh wrote:Make a 'Log' interface.
Get you logger classes Order and Billing Log implement Log interface


Thanks for your reply.
I think the meaning as below


But the original way that conctruct BillingLog and OrderLog as below.

I still didn't know how to construct the BillingLog and OrderLog in generic way.
Can somebody teach me how to solve it ?
Thanks

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

ILog should be re-named Loggable...
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Generic for construct a class
 
Similar Threads
Problem while One to One Relationship
Hibernate's update problem
please, help me to solve sun.jdbc.odbc.JdbcOdbcDriver cannot be cast to javax.sql.DataSource
Saving form bean with Array of objects (collection)
Encapsulation ?