Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Someone asked me if I can create factory pattern in java without using If-else construct. So I come with the following. Please provide your inputs if this seems a good example for using factories.

public enum EnumButtonFactory {

RADIO(RadioButton.class),
SUBMIT(SubmitButton.class),
NORMAL(NormalButton.class);

private Class<? extends Button> button;

EnumButtonFactory(Class<? extends Button> b) {
    this.button = b;
}

public Button get() {
    try {
        return button.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
}
share|improve this question
1  
Can you provide an example for the use case? What do you want to replace with this implementation. This could help to get the direction. Because the typical factory pattern does not necessarily need if-then-else. –  tb- Jan 31 '13 at 13:31
    
You could use a hash and pull the correct factory out of the hash based on some criteria. You could also do something like a chain of responsibility. @tb I think the problem the OP is trying to solve is a builder or abstract factory that needs to pull up one of several specific factories. –  Amy Blankenship Feb 6 '13 at 0:58

3 Answers 3

This is a good example for demonstrating that you can implement factory pattern without conditional statements. Of course, no piece of code is good for every imaginable requirement.

In real world cases factory pattern usually needed because some aspect of the object building is not trivial. One such case is when you do not have the concrete classes at compile time. e.g. when you are a library/framework developer and the user will have the concrete classes. In that case you would want to externalize the class names to the program configuration. This program requires that you have the SubmitButton.class et al at compile time, and is dependent of those concrete classes.

share|improve this answer
    
I somewhat agree but typically Factory pattern solves the problem of object creation and needs a sub class to be there. When you say that some classes are not known at run time, can you elaborate that how will have your factory return those objects? IMHO the case you are talking about fits into the domain of Proxy pattern. –  Ashish Feb 1 '13 at 16:38

I think its not appropriate to call it Factory pattern. Most people refer to Factory Method Pattern as Factory Pattern. I have seen some references where its called as Simple Factory Pattern to clearly distinguish the two. Also its not a GOF pattern.

You can use different approaches but idea is that you are creating objects based on some parameter. In one case i had used reflection to implement Simple Factory Pattern because i had around 100 odd classes so putting if/else would have been a daunting task.

  String  qualifiedClassName = "Library1"; //fully qualified class name
  Library library = (Library) Class.forName(qualifiedClassName).newInstance(); 
  library.execute();  //call method after creating object

Library class is super class and Library1 is one of the sub class.

share|improve this answer

A static createInstance method would be safer instead of newInstance. Also, agreeing with rai.skumar, I would rather call it Factory Strategy or something similar. But actually I find your idea quite smart and elegant :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.