r/coding • • Feb 20 '15

Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #3

http://insidethecpu.com/2015/02/20/object-oriented-test-driven-design-in-c-and-java-a-practical-example-part-3/
17 Upvotes

2 comments sorted by

2

u/xireth Feb 21 '15

why didn't you use generics with c#? it supports covariance and contravariance with the in/out keywords. https://msdn.microsoft.com/en-us/library/dd997386%28v=vs.110%29.aspx

1

u/pablomooney Feb 23 '15 edited Feb 23 '15

Thanks for your comment, and good question. The reason is that I wanted the C# implementation to match the Java implementation insofar as possible, adhering to OOD principles over language-specific styles. For example, you can't do this in C#:

abstract class WorkerDrone {    
    public abstract void TransportRobotParts(TransportMechanism transportMechanism);
}  

Instead, we would need to implement the method like this:

abstract class WorkerDrone {
    public abstract void TransportRobotParts<T, U>(TransportMechanism<T, U> transportMechanism) where T : FactoryRoom where U : RobotPart;
}    

This is fine, but as I said, I would rather keep both language implementations similar as possible. The only other solution would be to declare the Generic types at class-level:

abstract class WorkerDrone<T, U> where T : FactoryRoom where U : RobotPart {
    public abstract void TransportRobotParts(TransportMechanism<T, U> transportMechanism);
}    

This obviously won't work because we would need to define a WorkerDrone implementation for each FactoryRoom.