Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Need opinion on correctness of strategy pattern usage

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

I am beginning to apply patterns in my work. I have to make a functionality for export data in pdf and csv format. I am planning to use strategy pattern here. Following is in my mind.
I will have an interface IExportable with a ab method export()



and two implementing classes :


and



A factory will give me instance of appropriate Exporter. A ExportUtil will contain IExportable ref and a generateExport() to do export :



Am I correct to pick Strategy pattern here? Is my design okay? Can it be improved?

Thanks for help
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The organization seems fine to me. I have a few issues with the names you used though.

Minor: IExportable - I prefer NOT using "I" to designate an interface. You don't see this convention in the standard Java API. This convention is a carry over from styles used in other languages which I don't particularly agree with. Not a big deal if your coding conventions dictate that you use "I" to designate interfaces. I would point out that it doesn't give me any useful information that I couldn't find out with a good IDE.

Major: Semantics of being "Exportable" doesn't fit the use. If an object is "Exportable" I would expect it to be the thing that is converted to PDF or CSV, not the thing that performs the conversion. That is, I would expect a Graph to be Exportable or a Table to be Exportable. What you have now is saying "You can export a PDFExporter" and "You can export a CSVExporter" which is not true. The PDFExporter can export something to PDF and the CSVExporter can export something to CSV.

It would make more sense to name the interface as Exporter. This would fit common conventions that qualify the interface name with something that indicates what the implementation specifically does. The name Exporter can easily be determined to be the interface while PdfExporter and CsvExporter (or alternatively, PdfExporterImpl and CsvExporterImpl) are specific implementations.

You still might want to keep the Exportable interface to define a common method that an Exporter implementation would use to get the data it needs to export.

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are saying right, Junilu, we have to use exporter to export something to either pdf, csv or others not to export the exporter. And this something is exportable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic