1

I'm a newbie to design pattern. And I'm trying to learn some design patterns.

I read blogs online and most of them directly show me that: this is the simple factory and this is how we use it.

I understand inheritance and interfaces, and I'm using Java, of course, I don't have a lot of experience in design systems.

My question is that: what if we don't use the factory design pattern? I mean, can someone give a case that I'll get myself into a mess if I don't use factory design pattern?

Fan Zhang
  • 55
  • 5
  • 1
    This question appears to be off-topic because it is not about a real, actual coding problem that you're facing. – jamesmortensen Feb 23 '14 at 06:20
  • @jmort253 .I Googled a lot blogs, they always start with "How to use", instead of telling that what a mess it will be if we not use... – Fan Zhang Feb 23 '14 at 09:15
  • Welcome to StackOverflow. Patterns are a developers toolbox, so there are plenty of times where a factory pattern isn't the optimal solution. The important thing is that you have a grasp of the different patterns available, so you can pick the most appropriate one. Patterns become more and more useful as the system you're working on grows, so it's difficult for someone to give you a good answer. – spikeheap Feb 23 '14 at 10:18
  • @spikeheap thank you. You probably right, it takes time to learn... – Fan Zhang Feb 23 '14 at 23:01

3 Answers3

0

The factory patterns are useful when the life-cycle of the created objects need to be controlled beyond the scope of use. For example, you could create a factory for image resources which return references to the same underlying resource for multiple requests to, perhaps, avoid an expensive IO operation.

Like most patterns, there are trade-offs. Use patterns to solve problems you encounter and understand; avoid using patterns prophylactic-ally since that commonly results in more complex code which is less understandable and more susceptible to bugs.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
0

For example factory design pattern is useful when you need several "constructors" with the same parameter type but with different behavior. (this is not the only benefit)

You can take a look at this question Factory Pattern. When to use factory methods?

Community
  • 1
  • 1
VahiD
  • 1,014
  • 1
  • 14
  • 30
0

There are tons of tutorials online explain the simple factory. Few of them explain the question: “What if I don’t want to use simple factory design pattern”.

I’ll tell you what if you don’t use simple factory design pattern. Let’s say I’m rich and I own an auto warehouse where I have my BMW, Audi, Benz and so on.

When I want to drive the BMW, I can choose from engines: V4, V6 and V8. I can also choose ties, 18”, 19” and 20”. And so on. Almost every time I want to drive, I can customize my car. Let’s say, in this case, I only care about engines. Usually, I have to specify engines like: “BMWV4-v2.0-1058″, which is a V4 engine, 2.0L and identification number 1058. And I have a class Car, BMW, Audi and Benz are extends the Car. Here are my cars:

//Define the parent class. When we Init a Car, we need to specify the engine.
public class Car {
    protected String engine = null;
    
    public Car(){
        
    }
    
    public Car(String engine){
        this.engine = engine;
    }
    
    public void drive() {
        System.out.print("Driving Car with engine: "+this.engine);
    }
}
 
//Those are the implementation of cars
class BMW extends Car{
 
    public BMW(String engine){
        super.engine = engine;
    }
 
    @Override
    public void drive() {
        System.out.println("Driving BWM with engine: "+super.engine);    
    }
    
}
class Audi extends Car{
 
    public Audi(String engine){
        super.engine = engine;
    }
 
    @Override
    public void drive() {
        System.out.println("Driving Audi with engine: "+super.engine);    
    }
    
}
class Benz extends Car{
 
    public Benz(String engine){
        super.engine = engine;
    }
 
    @Override
    public void drive() {
        System.out.println("Driving Benz with engine: "+super.engine);    
    }
    
}

As we can see, the constructor of a car will take the engine type. And now, I want to Drive: //This is me, I'm rich and I want to drive my cars.

public class RichMan {
    public static void main(String[] args){
        //Monday, drive my BMW. Oh Yeah~
        Car MondayCar = new BMW("BMWV4-v2.0-1058");
        MondayCar.drive();
        //Tuseday, drive the Benz
        Car TuesdayCar = new Benz("BenzV6-v2.5-4315");
        TuesdayCar.drive();
        //Wed, drive the Audi, Let's roll
        Car WedCar = new Audi("AudiV8-v3.0-3513-supercharged");
        WedCar.drive();
    }
}

Every thing looks good. But the problem is: I have to remember my engines’ name. Sometimes, its not easy to remember them. Image that I’m super rich and have tons of cars. This is exactly the problem if we don't use factory pattern: We need to know every car. We must konw everything to create a car.

To work this out, I decide to hire someone working at my car warehouse. And the whole car warehouse is a Factory now. The Factory knows exactly what are the engines. And everyday, The factory will get the car I want ready.

public class MyCarFactory {
    private Car myCar;

    //Now, My factory will remember all the engine type
    public MyCarFactory(String car){
        if(car.equals("BMW")){
            this.setMyCar(new BMW("BMWV4-v2.0-1058"));
        }
        else if(car.endsWith("Benz")){
            this.setMyCar(new Benz("BenzV6-v2.5-4315"));
        }
        else{
            this.setMyCar(new Audi("AudiV8-v3.0-3513-supercharged"));
        }
        /*
         * Here, in the future, if I have more cars, I can add them here,
         * If new upgrade of engine, I can change the engine here.
         * I can even hire someone to work in this factory and do all the things for me.
         * At any memont, as a rich man, I only drive, no need to know details.
         */
    }

    public Car getMyCar() {
        return myCar;
    }

    public void setMyCar(Car myCar) {
        this.myCar = myCar;
    }
}

Now, for me, as a rich man, I only need to do:

//This is me, I'm rich and I want to drive my cars.
public class RichMan {
    public static void main(String[] args){
        //Monday, drive my BMW. Oh Yeah~
        Car MondayCar = new BMW("BMWV4-v2.0-1058");
        MondayCar.drive();
        //Tuseday, drive the Benz
        Car TuesdayCar = new Benz("BenzV6-v2.5-4315");
        TuesdayCar.drive();
        //Wed, drive the Audi, Let's roll
        Car WedCar = new Audi("AudiV8-v3.0-3513-supercharged");
        WedCar.drive();

        //Thursday, I don't need to remember any of my car's engine type.
        Car ThursdayCar = new MyCarFactory("Audi").getMyCar();
        ThursdayCar.drive();
    }

}

That’s it. This is the idea. I’m god damn rich and I don’t want to remember all the details of getting my fancy car ready to drive. My factory will take care of it and I only need to drive. Cheers~ - See more at my blog: Why we use simple factory design pattern? Reasons and examples

Steven Zhan
  • 20
  • 1
  • 5