Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple factory vs Factory method

Simple factory: enter image description here

Factory method:

enter image description here

hey everyone. I am looking for the difference between simple factory and factory method.. I know the structural difference(images above), but I cant understand difference in use cases. for example, this is the explanation for factory method:

In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.

In my example the client want a Nokia object. So the steps are given below.

1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.

2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.

3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.

I can't understand the first step:

But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.

so client writes something like this:

IMobileFactory factory = LoadFactory("NokiaFactory");

so why is that useful and better? what's the benefit? why shouldn't I just write this:

NokiaFactory factory = new NokiaFactory();

or what about that:

IMobileFactory factory = new NokiaFactory();
like image 549
O. Shekriladze Avatar asked Nov 19 '18 10:11

O. Shekriladze


3 Answers

So your question is about comparing this design #1:

IMobileFactory factory = LoadFactory("NokiaFactory");

to this design #2:

NokiaFactory factory = new NokiaFactory(); // or:
IMobileFactory factory = new NokiaFactory();

As you see the biggest difference is that, while the client in the design #1 does not know about any concrete type like NokiaFactory or IPhoneFactory, the client in the design #2 does.

The downside of knowing about concrete things like NokiaFactory or IPhoneFactory should be well-known. If you want to make changes to these types, for example, you want to add a new method to NokiaFactory and this method is not a part of the IMobileFactory interface, then the client code will be affected unnecessarily. The client does not care about the new method, but its code must be recompiled/redeployed.

UPDATE

To explain more.

For example, a new method named Foo is added to the NokiaFactory class:

class NokiaFactory {
    // old code
    public void Foo() { ... }
}

Foo is not a method of the IMobileFactory interface but it is added to NokiaFactory because there is another client who requires the method, and that client is happy to depend on NokiaFactory class. In other words, that client would welcome changes from NokiaFactory class, but the first client would not.

like image 85
Nghia Bui Avatar answered Oct 10 '22 22:10

Nghia Bui


From the design perspective:

  • Use SimpleFactory when the types of Objects are not fixed. For example: Phones my manufacturer - Nokia, iPhone. May be you want to add a new manufacturer tomorrow.

  • Use Factory method when the types of Objects are fixed. For example: Phones by type - Landline and Mobile.

Finally, it comes down to how you want to design your system. What you want to be extendable.

like image 30
Dakshinamurthy Karra Avatar answered Oct 10 '22 23:10

Dakshinamurthy Karra


You need the interface to avoid creating different flow per type, for 10 types you will have 1 lines of code instead of 10

IMobileFactory factory = loadFactory(myFactoryName);

instead of per type:

NokiaFactory factory = new NokiaFactory();
...
IphoneFactory factory = new IphoneFactory();
...

The difference between using loadFactory method to new NokiaFactory() is that you don't need to explicitly create a new object, you delegate object creation to loadFactory which returns relevant object

like image 1
user7294900 Avatar answered Oct 10 '22 23:10

user7294900