I have an abstract class with a few derived class
public abstract class MyObject
{
public string name { get; set; }
public bool IsObject(string pattern);
...
}
public class MyObjectA : MyObject
{
public string name { get { return "MyObjectA"; } set; }
public bool IsObject(string pattern) { ... }
...
}
public class MyObjectB: MyObject
{
public string name { get { return "MyObjectB"; } set; }
public bool IsObject(string pattern) { ... }
...
}
Now I want to have a function, which returns my the specific class (MyObjectA / MyObectB) based on a string. The problem is, that I have a lot of if/else-clauses to get that:
public MyObject Create(string pattern)
{
MyObjectA obj = new MyObjectA();
if(obj.IsObject(pattern)
{
return obj;
}
else
{
MyObjectB objb = new MyObjectB();
if(objb.IsObject(pattern);
return objb;
else
...
}
}
That looks just awful. What would be a better way to do this?