samedi 21 janvier 2017

Abstract Factory Design Pattern with generics : where is the additional decoupling?

I don't understand the additional decoupling the abstract factory design pattern would add in this situation :

Let's say I have :

  • a mapper which is generic :

    public interface Mapper<A,B> { [mapper methods]}

  • a concrete class that implements this mapper interface:

    JsonMapper implements Mapper<Json,ModelObject>{ [mapper methods implemented..] }

Now I want to use a mapper in a third object, a repository for instance :

OnlineRepo {
  Mapper<Json, ModelObject> mMapper;
  OnlineRepo(Mapper<Json, ModelObject> jsonToModelMapper ){
    mMapper = jsonToModelObject;
  }
// other repo methods using mMapper, get, upsert etc etc...
}

And finally my main code may assembly the whole (instanciating concrete classes/implementations).

main{
JsonMapper mapper = new JsonMapper();  // reference to concrete JsonMapper
OnlineRepo repo = new OnlineRepo(mapper); // inject concrete JsonMapper
}

How would the factory design pattern help me decouple these objects ?

  1. The OnlineRepository does not directly reference the concrete JsonMapper it references only the abstract Mapper<Json, ModelObject>. Isn't it ?

  2. How would an abstract factory design pattern would help me decouple this code even more.

  3. It seems like I will always need to reference an implementation in my main program (if not an implementation of JsonMapper, an implementation of JsonMapperFactory ?).

To better illustrate I do not see the advantage of this :

psoeudo UML with abstract factory

over this :

psoeudo uml without abstract factory

I hope my question is clear, if not, do not hesitate to state it to help me redact it better.

Thanks in advance for your help. Best, Antonin

Aucun commentaire:

Enregistrer un commentaire