Novel polymorphism – any reasons for this code?

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, PropertyTwoPropertyFour 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật