• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generics Question

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

Can any help me with desiging a generic interface?

I have the following classe structure:

Super class: PdfSection
Subclasses : PdfDynamicSection & PdfStaticSection (extends PdfSection)

Subclasses : Section1,Section2,Section3 etc.. (extends PdfDynamicSection)
Subclasses : Section5,Section6,Section7 etc.. (extends PdfStaticSection)

I want a generic interface called ISectionWriter which will write any of the sections to the document



But I need two implementations of ISectionWriter, one for writing static content and one for dynamic content



Im getting confused on how to declare the above implementations with Generics and was hoping someone could help. So basically I want all subtypes of DynamicSection to use PdfDynamicSectionImpl and all
Subtypes of Static Section to use the PdfStaticSectionImpl

Can any advise on how I do it please?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want your implementations to be generic? I think you've basically got two options - which is best will depend on how you want to use them.

For non-generic implementations:
And if you need to genericise them:
etc
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darren Carter wrote:I want a generic interface called ISectionWriter which will write any of the sections to the document
...
But I need two implementations of ISectionWriter, one for writing static content and one for dynamic content


OK, well first, you seem to have missed out the fact that there are at least three interfaces you haven't told us about: DocumentSection, StaticSection and DynamicSection; and I presume that your "pdf" variants extend those.

But simply put, have your pdf implementations implement ISectionWriter with a bounded type. Probably most flexible is:

Winston

[Edit] Too slow.
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Thnks for the replies.. Ive got moved onto some production issue at the moment but will do this after! Thnaks for the help!
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
Thnaks for assiting with this, but im confused again :-(
Basically I have a list of Sections which can be of subtype class StaticSection or DynamicSection. So I could have 10 sections that are subtypes of StaticSection and say another 10 that are subtypes of DynamicSection. I need one method to write static content to the pdf and sperate method for writing the dynamic content.

Could you advise the best way to achieve the above? Could I have a class where I pass in the section and depending on the supertype, it calls the required write mehod?

Thanks for the help!







 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darren Carter wrote:Could you advise the best way to achieve the above? Could I have a class where I pass in the section and depending on the supertype, it calls the required write mehod?


You could, but it's poor OO programming, and it's called "dispatch code". Seems to me you were on the right lines to start with; just didn't have all the 'I's dotted and 'T's crossed.

Alternatively, have the type itself decide how it gets written. Possibly best of all.

Winston
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,

Thanks for taking time to respond :-)

Are you suggesting that while Ill loop through my List of Sections I lookup the the superclass of the section (then calling the correct implementation depending on its type?

Thanks
Darren.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darren Carter wrote:Are you suggesting that while Ill loop through my List of Sections I lookup the the superclass of the section (then calling the correct implementation depending on its type?


No. In fact, the exact opposite. Presumably, a PDF section is written differently from the section for some other type of document, so you have implementations of your Writer for PDF's, and maybe also RTF, Word, Excel, and whatever other types of document you deal with.

Assuming you have a collection of Sections, it appears that you want to do something like:and the easiest way to do that is to have the Section know what type of Writer it is written with, for example:Note that this is just an incomplete example based on what you've shown us so far, and depending on exactly what you want, you may have to do things differently. For example, it might be better to first organise your Sections into a Document (in this case, a PDFDocument) and then simply write that out in one go.

The whole point of polymorphism is to AVOID lookups; and to do that, an implementation class must KNOW how it's handled.

HIH. Come back if you have any more questions.

Winston
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,

Thank you very much! - This is very imfomative and has put me back on the right track.

Its nice to know that there is help out there!

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darren Carter wrote:Thank you very much! - This is very imfomative and has put me back on the right track.
Its nice to know that there is help out there!


You're welcome. Some of us still remember banging our heads against the terminal - and in my days they were made of glass and metal.

Winston
 
It's just a flesh wound! Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic