4
I am creating a functional Factory where I define what is its output type and what are the required implementation methods.
With this implementation, when I try to use the object obj
within the function that makes the whole Factory process of that object, he cannot interpret it as being of the Bar type.
The idea is that I might have the same statement
FooFactory.build()
for multiple types, changing only the encapsulated implementation and always taking as return aFoo
.
How to solve?
My code is getting like this:
public abstract class Factory<T> where T : class {
public abstract T Build<U>(U obj) where U : class;
}
public class Quadrado {
public int Largura { get; set; }
public int Altura { get; set;}
}
public class Retangulo {
public int Largura { get; set; }
public int Altura { get; set; }
}
public class QuadradoFactory: Factory<Quadrado> {
public override Quadrado Build<Retangulo>(Retangulo o) =>
new Quadrado() { Largura = o.Largura, Altura = o.Largura };
}
public class Program {
public static void Main() {
var f = new QuadradoFactory();
var r = new Retangulo(){ Largura=2, Altura=4 };
var q = f.Build<Retangulo>(r);
System.Console.WriteLine(
"Largura: " + q.Largura +
"Altura: " + q.Altura
);
}
}
No, in the model you showed you pass a string. doing so he really understood but if I use a compound class it does not work.
– LeandroLuk
Class is class, there is no class that is composed or not composed. What you asked I answered, if the real problem is another is not in the question. I can’t answer because the question doesn’t show it.
– Maniero
OK, I rephrased the question
– LeandroLuk
But I already answered what was asked. Now I don’t know if I have the answer, if I have time to see it. Now you’re asking another question.
– Maniero
I changed the answer, but don’t do this, it complicates for who answered.
– Maniero
I don’t think I expressed myself right the first time. really what I need is the second option since I can have multiple Build methods, based only on the type of information passed.
– LeandroLuk
Then I think it’s better to think of something else, leave the code without robustness, with cognitive load to deal with. If I understand correctly. If it is something else, nor rolls, it will not work. Clearly this is not the real problem, but the solution should not even be this for the real problem.
– Maniero