The point is that in some cases you want to be able to pass a more general interface while getting some Type-checking and convenience of generics.
In this case we want to hold on to PopProtocolSender and perhaps use it as a variable somewhere. When we use it we need to acccess it's specific IPopProtocol Protocol Property. But we also want to be able to treat it as a general IProtocolSender that has a IProtocol Property, so it can be sent to other classes that takes that interface.
These kinds of situations mainly arise when you have a more complicated architecture. If there's a better way to have your cake and eat it too I'd be glad to know it :)
public interface IPopProtocol : IProtocol
{
void SomeUniqueMetod();
}
public interface IProtocol
{
}
public interface IProtocolManager
{
void SendMessage(IProtocol protocol, string message);
}
public interface IProtocolSender
{
IProtocol Protocol
{
get;
}
}
public class PopProtocol : IPopProtocol
{
public void SomeUniqueMetod()
{
}
}
public class Program
{
public void DoStuff()
{
var sender = new PopProtocolSender ();
var manager = new ProtocolManager();
manager.SendMessage(sender.Protocol, "Testing");
sender.Protocol.SomeUniqueMethod();
}
}
public class ProtocolManager : IProtocolManager
{
public void SendMessage(IProtocol protocol, string message)
{
}
}
public class ProtocolSender : IProtocolSender
{
protected readonly IProtocol _protocol;
public ProtocolSender(IProtocol protocol)
{
_protocol = protocol;
}
public IProtocol Protool => _protocol;
}
public class PopProtocolSender : ProtocolSender<IPopProtocol>
{
public PopProtocolSender() : base(new PopProtocol)
{
}
}
public class ProtocolSender<T> : ProtocolSender where T : IProtocol
{
public ProtocolSender(T protocol) : base(protocol)
{
}
public new T Protocol => (T)_protocol;
}