1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| using System;
public interface IA
{
}
class IAFactory
{
protected class A1 : IA
{
}
protected class A2 : IA
{
}
protected class A3 : IA
{
}
public IA New(String s)
{
IA result = null;
if (s.Equals("A1"))
{
result = new A1();
}
else if (s.Equals("A2"))
{
result = new A2();
}
else if (s.Equals("A3"))
{
result = new A3();
}
return result;
}
}
class Test
{
static void Main()
{
// Factory.A1 a1 = new Factory.A1(); // Won't compile because A1 type is protected
IAFactory f = new IAFactory();
IA a1 = f.New("A1");
IA a2 = f.New("A2");
IA a3 = f.New("A3");
Console.WriteLine(a1);
Console.WriteLine(a2);
Console.WriteLine(a3);
}
} |
Partager