As part of my work on a legacy C# application I’ve come across a novel (to me) use of an interface & concrete implementations. I can’t think of any reason why you’d do the following, but I’m pretty thick, so maybe someone else can?
public interface IContract
{
ContractImplementation1 Contract { get; }
bool IsCollection { get; }
bool Touched { get; set; }
}
public class ContractImplementation1 : IContract
{
public ContractImplementation1(string propertyOne, string propertyTwo, string propertyThree, string propertyFour)
{
PropertyOne = propertyOne;
PropertyTwo = propertyTwo;
PropertyThree = propertyThree;
PropertyFour = propertyFour;
}
public ContractImplementation1 Contract { get { return this; } }
public bool IsCollection { get { return false; } }
public bool Touched { get; set; }
public string PropertyOne { get; private set; }
public string PropertyTwo { get; private set; }
public string PropertyThree { get; private set; }
public string PropertyFour { get; private set; }
public override string ToString()
{
if (string.IsNullOrEmpty(PropertyFour))
return string.Format("{0} => {1}: {2}", PropertyOne, PropertyTwo, PropertyThree);
else
return string.Format("{0} => {1}: {2} {3}", PropertyOne, PropertyTwo, PropertyThree, PropertyFour);
}
}
public class ContractImplementation2 : IContract
{
public ContractImplementation1 Contract { get { return null; } }
public bool IsCollection { get { return true; } }
public bool Touched { get; set; }
public List<ContractImplementation1> Contracts = new List<ContractImplementation1>();
}
I can’t get my head around the super-type having a property that is a sub-type of itself.
Following Cosmin’s answer: I can’t get my head around why you’d have the sub-type as a property given that the property returns itself on the implementation (rather than a ‘parent’ of the same type i.e. a different instantiation of the super-type).
3
The classes you’ve shown are implementations of nodes in some kind of tree structure. The IsCollection
property determines if this is a leaf node or a branch(?) node. If IsCollection
is true, then there is no “data” at this node, so you have to continue down the tree. If IsCollection
is false, then this is a leaf node, so it’s valid to look at the Contract
property.
The domain model seems to indicate that a contract can be made up of other contracts, and so on.
The most likely reason for it being the way it is, is because originally we just had ContractImplementation1
, and then they realized that contracts could be nested like this. So someone created the IContract
interface for the generic node-in-a-tree (and really should have named it IContractNode
or something) and then created ContractImplementation2
to be the branch. This wasn’t really the right way to do it. The right way would be more like:
interface IContractNode
{
bool IsLeaf { get; }
// Only access this is IsLeaf is true
Contract Contract { get; }
// Only access this if IsLeaf is false
ReadOnlyCollection<IContractNode> Children { get; }
}
class ContractNodeLeaf : IContractNode
{
public ContractNodeLeaf(Contract contract)
{
if(contract == null) throw new ArgumentNullException("contract");
this.Contract = contract;
}
public bool IsLeaf { get { return true; } }
public Contract Contract { get; private set; }
public ReadOnlyCollection<IContractNode> Children
{ get { return new List<IContractNode>().AsReadOnly(); }
}
class ContractNodeBranch : IContractNode
{
public ContractNodeBranch(ReadOnlyCollection<IContractNode> children)
{
if(children== null) throw new ArgumentNullException("children");
this.Children= children;
}
public bool IsLeaf { get { return false; } }
public Contract Contract
{ get { throw new InvalidOperationException("I'm not a leaf!"); }
public ReadOnlyCollection<IContractNode> Children { get; private set;}
}
… where Contract
is the original class, from before the change.
1
I can’t get my head around the super-type having a property that is a sub-type of itself.
The code you posted, with PropertyOne
, PropertyTwo
… PropertyFour
is probably just an example, so I’ll assume the liberty to provide my own example of a case where the super-type having properties that are sub-types of itself makes obvious sense:
Let’s say you’ve got a class Human
with subclass Male
and subclass Female
. If you’d add the properties Mother
and Father
to Human
, it makes sense for them to be of the sub-type Female
and Male
.
Given this example, the generic answer is: if the super-type and the sub-type have enough in common, why not? I can imagine this sort of situation arising quite often in tree-like structures.
1
This looks like a (poorly done) implementation of the Composite pattern. Its primary purpose is to allow you to treat an object the same as a collection of those objects, without having to differentiate between the two everywhere in the calling code. I say poorly done because when properly used you shouldn’t need the IsCollection
property.
This happens to be a highly useful and common pattern with trees. You can call methods on a node without worrying about if it is a leaf or contains a whole subtree.