using System;
namespace Articulos.Cap04
{
public interface ITransformador
{
int Transformar (int x);
}
public class Utilitario
{
public static void TransformarTodo (int[] valores, ITransformador t)
{
for (int i = 0; i < valores.Length; i++)
{
valores[i] = t.Transformar (valores[i]);
}
}
}
public class Cuadrado : ITransformador
{
public int Transformar(int x)
{
return x * x;
}
}
public class Cubico : ITransformador
{
public int Transformar (int x)
{
return x * x * x;
}
}
public class Aplicacion
{
public static void Main()
{
int[] valores = {1, 2, 3, 4, 5};
Utilitario.TransformarTodo(valores, new Cuadrado());
Console.WriteLine("Cuadrados para los elementos de `Valores`:");
foreach (int i in valores)
{
Console.Write("{0} ", i.ToString());
}
Console.WriteLine();
int[] valores2 = {1, 2, 3, 4, 5};
Utilitario.TransformarTodo(valores2, new Cubico());
Console.WriteLine("Cubo para los elementos de `Valores`:");
foreach (int i in valores2)
{
Console.Write("{0} ", i.ToString());
}
Console.WriteLine();
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNAp7CiAgICBwdWJsaWMgaW50ZXJmYWNlIElUcmFuc2Zvcm1hZG9yCiAgICB7CiAgICAgICAgaW50IFRyYW5zZm9ybWFyIChpbnQgeCk7CiAgICB9CiAgICAKICAgIHB1YmxpYyBjbGFzcyBVdGlsaXRhcmlvCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIFRyYW5zZm9ybWFyVG9kbyAoaW50W10gdmFsb3JlcywgSVRyYW5zZm9ybWFkb3IgdCkKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdmFsb3Jlcy5MZW5ndGg7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdmFsb3Jlc1tpXSA9IHQuVHJhbnNmb3JtYXIgKHZhbG9yZXNbaV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgY2xhc3MgQ3VhZHJhZG8gOiBJVHJhbnNmb3JtYWRvcgogICAgewogICAgICAgIHB1YmxpYyBpbnQgVHJhbnNmb3JtYXIoaW50IHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4geCAqIHg7CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgY2xhc3MgQ3ViaWNvIDogSVRyYW5zZm9ybWFkb3IKICAgIHsKICAgICAgICBwdWJsaWMgaW50IFRyYW5zZm9ybWFyIChpbnQgeCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiB4ICogeCAqIHg7CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgY2xhc3MgQXBsaWNhY2lvbgogICAgewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgICAgICB7CiAgICAgICAgICAgIGludFtdIHZhbG9yZXMgPSB7MSwgMiwgMywgNCwgNX07CiAgICAgICAgICAgIFV0aWxpdGFyaW8uVHJhbnNmb3JtYXJUb2RvKHZhbG9yZXMsIG5ldyBDdWFkcmFkbygpKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDdWFkcmFkb3MgcGFyYSBsb3MgZWxlbWVudG9zIGRlIGBWYWxvcmVzYDoiKTsKICAgICAgICAgICAgZm9yZWFjaCAoaW50IGkgaW4gdmFsb3JlcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZSgiezB9ICIsIGkuVG9TdHJpbmcoKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpbnRbXSB2YWxvcmVzMiA9IHsxLCAyLCAzLCA0LCA1fTsKICAgICAgICAgICAgVXRpbGl0YXJpby5UcmFuc2Zvcm1hclRvZG8odmFsb3JlczIsIG5ldyBDdWJpY28oKSk7CiAgICAgICAgICAgIAogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiQ3VibyBwYXJhIGxvcyBlbGVtZW50b3MgZGUgYFZhbG9yZXNgOiIpOwogICAgICAgICAgICBmb3JlYWNoIChpbnQgaSBpbiB2YWxvcmVzMikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZSgiezB9ICIsIGkuVG9TdHJpbmcoKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CiAgICAgICAgfQogICAgfQp9