• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using factory to get an instance for calling a static method

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

I want to use design pattern factory that will return a converter class.
This converter class will call a static method to convert a file from one format to another.

the factory is like that :




After I get an instance of ConversionFile I just call a static method convert() to convert the file.
It works, I could be happy but I am not so satisfied because not so clean.

In fact, it is not clean to call a static method from an instance... so I would like to have something cleaner.

Do you know how can the factory return not an instance but a kind of Class so that I call the static method on a class and not on an instance ?

It cannot return a Class because the Class Class has no convert() method.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a pretty informative article on Wikipedia about the Factory pattern, it has examples in Java.

http://en.wikipedia.org/wiki/Factory_method_pattern

Hunter
 
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a reason you have to return an instance variable? If you're trying to preserve how many converters exist and there's really no reason to return a handle to an instance you probably don't need to give an instance back to to the caller. In other words, something like:



Hopefully I'm understanding what you're trying to do.

Cheers,
Shawn
 
Jean-Christophe Blanchard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Shawn this method completely fit the needs.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic