• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using factory to get an instance for calling a static method  RSS feed

 
Jean-Christophe Blanchard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
Shawn Smith
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you Shawn this method completely fit the needs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!