Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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).

share|improve this question
5  
Polymorphic code is best understood from code that uses this polymorphic code. You didn't post any. –  Euphoric Feb 26 '13 at 12:48
    
What do you consider the super-type and sub-type in this example? It looks like two classes with similar names that both implement the same Interface and one class references another class. –  DFord Feb 26 '13 at 13:38
1  
the classes are both sub-types of the interface. The interface is the super-type. –  stevef4000 Feb 26 '13 at 13:51
add comment

3 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
And just to make it clear, the inheritance was wrong and the OP suspicion, right: less specific classes should have no knowledge of more specific ones. –  Bruno Brant Jun 11 at 14:13
add comment

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.

share|improve this answer
    
That makes sense and I see why you'd have that sort of structure in a tree. However in the example code that's not the case - the subtypes either return themselves, or nothing, not a step back down the tree. I'll update the question. –  stevef4000 Feb 26 '13 at 12:35
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.