mercredi 4 mars 2015

Design pattern - creating an object from different types of data source

I have been trying to find an appropriate design pattern, if a formal one exists, for the following scenario:


Object A requires an object B. Now, object B can be created using data from different sources, say S_1 or S_2. I don't want A to have to care about creating B, it should just be given it and proceed. So then, is there a nice clean way of creating B? I have thought about factory-type patterns but I do not have polymorphism at play. I was thinking something like:



B obj_b = B.createInstance(type_S_1);
obj_A.doSomething(obj_B);


where I give the type of data soruce to the static method createInstance().


I'm still learning a lot about design patterns, so that's why I ask. I may be over complicating things, so please feel free to say so!


Aucun commentaire:

Enregistrer un commentaire