dimanche 11 juin 2017

Simple factory design in C# calling factory from another factory?

I need to create an object with a simple factory in C#, that object has a dependency object that is also created using another simple factory.

Is it a good design principle to call a simple factory from another simple factory? I think I might be doing something strange.

I have the following:

interface IObjecAtImplement
void task();

interface IObjecBtImplement
void taskFoo();
void anotherTaskFoo();


public static class ObjectAFactory
{
public static IObjecAtImplement Get(string objecttype) {

IObjectBImplement objectB = ObjectBFactory.Get(objecttype);

switch (objecttype)

case "A";
return ObjectAFirstImplementacion(objectB);

case "B";
return ObjectASecondImplementacion(objectB);
  }
}



public static class ObjectBFactory
{
public static IObjecBtImplement Get(string objecttype) {

switch (objecttype)

case "A";
return new ObjectBFirstImplementacion();
case "B";
return new ObjectBSecondImplementacion();
}

}

Is this a good idea at all? is there any other way of solving it? (The code brackets are not well written, it's just an example)

Aucun commentaire:

Enregistrer un commentaire