I
I
industriousSloth2015-05-13 12:54:12
C++ / C#
industriousSloth, 2015-05-13 12:54:12

Is it possible to create an interface for a list of the following classes?

Good afternoon! At me the list of tokens is generated from one module. And I wanted to use some kind of pattern that would select a class from the incoming list and create an instance of it. But I can't use the Builder or the Factory because I can't find the unified interface.
Here is the list of classes:

public class ProductA 
{
   public string FieldFirst { get;set; }
   public string FieldSecond { get; set; }
   public string FieldThird { get; set; }
   public string FieldFourth { get; set; }
}

public class ProductB 
{
   public string FieldFirst { get; set; }
   public string FieldSecond { get; set; }
   public string FieldThird { get; set; }
   public List<AnotherClass1> FieldFourth { get; set; }
}

public class ProductC 
{
   public string FieldFirst { get; set; }
   public string FieldSecond { get; set; }
   public List<AnotherClass2> FieldThird { get; set; }
}

public class ProductD 
{
   public string FieldFirst { get; set; }
   public List<AnotherClass3> FieldSecond  { get; set; }
}

Actually, is it possible to find some kind of interface for them, and use some kind of pattern, for example, an Abstract Factory or a Builder. Or is it a bad decision in this case. Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-05-13
@industriousSloth

Here you need to leave one option out of two: either you still have a common interface through which you will work with an object of any class, and then you need to solve the problem of creating an instance, for which you can use either the mentioned Factory or reflection. If you are firmly convinced that the interface is unknown to you, then you are solving a meta-problem - you need to do something with previously unknown fields / methods of previously unknown classes (for example, when serializing objects in the general case), then you cannot do without reflection/code generation. You only clearly find out for yourself what you know about your objects and what you don't.
With the advice of Oxoronperhaps I do not agree, because. if the interface is empty, then it serves solely for the purpose of adding additional. information about the class (well, that is, here we have classes that implement it, but here they don’t), and this task is solved much more logically using attributes - they were created for this.

D
Dmitry Makarov, 2015-05-13
@DmitryITWorksMakarov

Maybe a linker would work? plus a property with meta information about the current object....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question