Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The factory class - Design patterns for dummies

From Design patterns for dummies:

There you go — you’ve got a factory class

enter image description here

What I have understood from factory pattern is that it helps us to avoid constant code modification whenever a new object needs to be created. But the function createConnection won't have to be modified it to create and add another object? So how is this helpful?

What's the point that I am missing?

like image 847
Aquarius_Girl Avatar asked Jun 13 '13 10:06

Aquarius_Girl


2 Answers

The factory pattern can be used to limit the number of code changes you have to do. Using your code as an example, imagine you have lots of functions which use database connections

void ExampleMethod() {
    var con = new MySqlConnection();

    // do something with con
}

And now you want to use Oracle instead of MySQL so you have to change every single line that says new MySqlConnection into new OracleConnection.

A factory can solve this problem

void ExampleMethodUsingFactories() {
    var con = factoryInstance.createConnection();
}

where factoryInstance is instantiated only once with factoryInstance = new FirstFactory("MySql"). Changing the entire program to use Oracle becomes trivial now. You only have to replace "MySQL" by "Oracle". You don't have to change the methods that actually use the database connection.

Of course adding a new database engine still means you have to change the factory code, but that's only in one class.

like image 80
Dirk Avatar answered Sep 24 '22 20:09

Dirk


That is a terrible design. It is not SOLID. Most often comparing types at runtime is the sign of a poor design. Polymorphism is the answer. What makes things worse, this factory can produce only one kind of conection, so it should compare the strings once instead of doing it in every call to CreateConnection. What makes it even worse - strings. Why not enum or static ints? (All the errors of this example are of course caused by it's demonstrational nature. But every example of this kind should have a sidenote about the flaws - because it might stick - when you're reading a handbook you naturally asume that what it says is correct)

The proper factory in this case is:

public interface ConnectionFactory {
    Connection createConnection();
}

public class OracleConnectionFactory implements ConnectionFactory {
    public Connection createConnection(){
        return new OracleConnection();
    }
}

public class SqlServerConnectionFactory implements ConnectionFactory {
    public Connection createConnection(){
        return new SqlServerConnection();
    }
}

Insted of:

FirstFactory factory = new FirstFactory("Oracle");

You would have:

ConnectionFactory factory = new OracleConnectionFactory();

When you want a new kind of Connection, you would add a new factory, and changed this one line, you wouldn't have to modify the existing classes.

like image 33
lisp Avatar answered Sep 23 '22 20:09

lisp