5

I have been using factory pattern since a year now. Sometime I only feel that the real advantage would just be clean code. Let me explain,

interface A {

    public void test();
}

class B implements A {
    public void test() {

    }
}

class C implements A {
    public void test() {

    }
}

class Factory {
    public static A getObject(String name){
        if(name.equalsIgnoreCase("B")){
            return new B();
        }else if(name.equalsIgnoreCase("C")){
          return new C();
        }
        return new B(); 

    }
}

public class Test {

    public static void main(String[] args) {
        A a = Factory.getObject(args[0]);

        // if i dint use factory pattern
        A nofactory=null;
        if(args[0].equalsIgnoreCase("B")){
            nofactory= new B();
        }else if(args[0].equalsIgnoreCase("C")){
            nofactory= new C();
        }
    }
}

From above code I feel factory pattern just beautifies the code, please let me know if my understanding is wrong.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
Ashwin
  • 47
  • 2
  • 11
  • 4
    Look at JDBC as classic school example. It's one and all factory pattern. It allows you to use the same JDBC code on different databases by just swapping the JDBC driver (whose implementation is loaded via factories). – BalusC May 28 '13 at 13:13
  • 1
    Related/possible dupe: http://stackoverflow.com/a/7550752 – BalusC May 28 '13 at 13:14
  • 3
    What is the question? – Adam Arold May 28 '13 at 13:19
  • 1
    "Just" beautifies the code? What more would you require? It could be argued that all patterns are about beautifying code. – Marko Topolnik May 28 '13 at 13:23
  • 1
    @MarkoTopolnik Although I would argue against that. Some patterns are robust solutions to basic problems. They make your code scalable, not just more beautiful. Although I do love beautiful code =) – Ludwig Magnusson May 28 '13 at 13:26
  • @LudwigMagnusson Doesn't *Factory* do the same? So if you accept a definition of "beautify" that covers what *Factory* does, then we can argue that all other patterns are about the same kind of "beautifying". – Marko Topolnik May 28 '13 at 13:32

3 Answers3

2

If interface A and classes B and C are in a library and your code is the main method that uses the library, that means that classes D, E etc can be added to the library and you can use them without changing your code. The responsibility of choosing which implementation to use is moved to the library.

Also, your example is extremely simple. Other times the input may be more complex than a string that exactly matches the type. The input could be a file with a specific format that needs a certain implementation to be read for instance.

Ludwig Magnusson
  • 13,964
  • 10
  • 38
  • 53
  • So from answeres i can make out factory pattern separates concern of creating object from the composing object which also adds abstraction too – Ashwin May 28 '13 at 13:21
  • I don't _really_ get what you mean, but yes the concern of creating the object is left to the factory and hidden from other code that can trust the factory to return a proper object. – Ludwig Magnusson May 28 '13 at 13:31
1

Rather than beautification consider the following example

say you are implementing the Test Class and your partner is implementing the B,C,D .... (others you don't know).. how can you manage this?

Factory pattern helps you to code one part of your application without ever needing to know The other parts of it.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

I think in the DP book there is Intent section which is highly important even more than a pattern's structure itself. Your sample is trivial enough. In more complex cases Factory helps you incapsulate creation details. Taking your sample. Imagine there are "new B()" and "new C()" scattered round your code. Then you need to change B to say SuperB type and you have to change it in every place of the code. In Factory you will change it only in one place.

Arseny
  • 7,251
  • 4
  • 37
  • 52