• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generic for construct a class

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make a 'Log' interface.
Get you logger classes Order and Billing Log implement Log interface
 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ILog should be re-named Loggable...
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic