1

I understand the Java and C++ code examples of how a factory pattern works, though I've never coded in Java and haven't coded in C++ for almost 2 decades. I just need to understand the concept in order to follow some API documentation.

What I'm having trouble understanding is the use case. Most explanations say that if you use a factory method to create objects of a derived class based on the value of one of its arguments, it makes it easier to expand the number of derived classes. At a high level, I understand the words, but I haven't found a code example of when the absence of a factory is a problem. The only attempt I could find of showing the problem at a code level is here.

There are a number of issues with example, as noted in the comments to this question. However, I'd like to set them aside and focus on the problem that the factory is meant to solve.

The cited code is separated into (i) library code for a Vehicle base class and (ii) client code to instantiate a specific Vehicle subclass representing (say) a TwoWheeler or a ThreeWheeler. The problem solution is that if you add a new subclass to the set of existing subclasses, you don't need to rejig any client-side code. But the example Client class is too trivial to see what the problem is. The exact Vehicle subclass specification is hard coded into the Client class definition. Anywhere where Client is used to create an object, one could just as easily create an object of the specified subclass.

I get that the example has to be simple so that one can see through the code and syntax, and understand the reasoning. But the logic depicted still seems to focus on the mechanics of a factory, and it doesn't depict a real need for the factory.

Can anyone point to an online code-level example showing an realistic need for a factory?

I'm still working through a series of links that I've saved, but frankly, there seems to be a tendency to show mechanics of the factory pattern at the code level, while explaining the justifying need only at a narrative level. Hopefully, a code-level example of a realistic need for a factory is still small.

Thanks.

user2153235
  • 388
  • 1
  • 11

2 Answers2

0

In my humble opinion the use case is maintenance and encapsulation. The Factory will return not the class itself but an object implementing the interface implementation. You could switch the class as you want to replace it with mocks or new interface implementations without changing your whole code base (as long as the used class implements the interface). If you use a new interface implementation you just have to update a single file.

// Car.java
public interface ICar {
    void drive();
}

// VWGolf.class
public class VWGolf implements ICar {
    @Override
    public void drive() {
        System.out.println("VW Golf cruising around!");
    }
}

// VWPolo.class
public class VWPolo implements ICar {
    @Override
    public void drive() {
        System.out.println("VW Polo cruising around!");
    }
}

// Mustang.java
public class Mustang implements ICar {
    @Override
    public void drive() {
       System.out.println("Mustand driving around!");
    }
}

// CarFactory.java
public class CarFactory {
    public ICar getVW(boolean isTestEnvironment){
        if (isTestEnvironment) {
            return new VWPolo();
        } else {
            return new VWGolf();
        }
    }

   public ICar getMustang(){
        return new Mustang();
    }
}
Ben
  • 696
  • 9
  • 19
  • Hi, Ben, would it be possible to refer to the cited code to explain what you mean by the interface implementation being returned by the factory? My read of the code is that the factory returns an object. If one considers the object to be an interface implementation, then we return to question -- the object instantiation doesn't need a factory. – user2153235 Apr 07 '20 at 15:19
  • Even if one changes the subclass being instantiated, this merely requires an edit at the point of instantiation. Many factory examples enumerate the subclasses using `enum` to give them meaningful names. It seems no different, however, to edit an enum name than to edit a subclass name in the code. – user2153235 Apr 07 '20 at 15:20
  • Hi @user2153235, I've updated my post a little bit to make clear what I meant. About your second question: Just because you change the subclass, the enum or factory method does not have to change. Additionally, the biggest advantage is that you could decide in runtime which interface implementation is used in one location. In the factory method you could decide (e.g. if you are in a test environment) to use another implementation of the interface. – Ben Apr 08 '20 at 06:32
  • I hate to post this comment, as I see you've spent some time assembling the code-level example, but your answer seems to be why I posted my question to begin with. That is, I find code level examples of the mechanics of a factory, and high level narration explaining the problem being solved. Is there an online example showing code without a factory to illustrate what the problem is? Hopefully, the reasoning is the same across languages (C++, on which my knowledge is almost 2 decades old, and Java, on which I know little). – user2153235 Apr 08 '20 at 21:06
  • I can't picture it based on high level narration. With a factory, you change the subclass to be instantiated by changing the value of an argument for the factory method. Without a factory, you change the name of the subclass that you are instantiating. The amount of editting needed seems to be the same in both cases. I'm sure that real situations that call for a factory are not as simple as this, which is why this question is about identifying the problem at a code level in the absence of a factory. – user2153235 Apr 08 '20 at 21:06
0

I'm a little late to this discussion but hopefully this helps future readers.

Below is a good link with a simple example use case of a toy manufacturer. Although the example code is php I think the design pattern is clear and it starts with a simple design pattern and a story about why the simple pattern will no longer work for the toy manufacturer: https://www.binpress.com/factory-design-pattern/

The description of the Abstract Factory Design pattern was still a little confusing for me but after reading the following link, which has a code example in C++, I understand it a little more: https://iq.opengenus.org/abstract-factory-pattern-cpp/

sog
  • 493
  • 4
  • 13