mercredi 29 juillet 2015

Benefits from factory method pattern

I am reading Head First Design Patterns now, and i have a question. In the beginning of book i saw this principle:

Favor 'object composition' over 'class inheritance

And then i saw Factory Method with

abstract class Creator{
   abstract Product create(String type);
}

and subclassing

class ConcreteCreator extends Creator{
    Product create(String type){
     //creation
    }
}

But we can compose our class with Simple Factory, like

class OurClass{
     SimpleFactory factory;
     void ourMethod(){
        Product product = factory.create(String type);
     }
}

where SimpleFactory may be interface. So why we need Factory Method pattern?

Aucun commentaire:

Enregistrer un commentaire