mercredi 30 août 2017

Generic Strategy pattern

I'm trying to implement strategy pattern with generic types. I try to create object and the end invoke it but I have no idea how should look like signature of it. My problem is in ObjectProcessor class(Do method).

//my code
abstract class Credential {}
class SNMP : Credential { }
class HTTP : Credential { }

//dll
abstract class CredentialDLL { }
class SNMPDLL : CredentialDLL { }
class HTTPDLL : CredentialDLL { }

interface IMapper<T, Y>
{
    T Map(Y input);
    Y Map(T input);
}

class SNMPMapper : IMapper<SNMP, SNMPDLL>
{
    public SNMPDLL Map(SNMP input) { throw new NotImplementedException(); }
    public SNMP Map(SNMPDLL input) { throw new NotImplementedException(); }
}

class HTPPMapper : IMapper<HTTP, HTTPDLL>
{
    public HTTPDLL Map(HTTP input) { throw new NotImplementedException(); }
    public HTTP Map(HTTPDLL input) { throw new NotImplementedException(); }
}

class ObjectProcessor
{
    CredentialDLL Do(Credential input)
    {
        IMapper <?,?> mapper; // ??

        if (input is SNMP)
        {
            mapper = new SNMPMapper();
        }
        else
        {
            mapper = new HTPPMapper();
        }

        return mapper.Map(input);
    }
}

Aucun commentaire:

Enregistrer un commentaire