Segregating interfaces/concerns to a tree

When working with a single object there are a number of ways to segregate interfaces to it.

  • Breaking it into smaller components that can be treated independently.
  • Breaking it into simpler interfaces.

Both of these seem challenging to do with a tree. I think it would be nice to be able to do the following.

class Node : INodeData, INodePresentation { ... }

class Program
{
    static void Main()
    {
        var node = new Node();
        ManipulateNode(node);
        PresentNode(node);
    }

    void ManipulateNode(INodeData nodeData)
    {
        foreach (INodeData child in nodeData.GetChildren())
             nodeData.AddVertices(...);
        nodeData.Rotate(...);
    }

    void PresentNode(INodePresentation nodePresentation)
    {
        nodePresentation.Color = Color.Blue;
        nodePresentation.Size = 15;
        nodePresentation.Paint();
        foreach (INodePresentation child in nodePresentation.GetChildren())
            PresentNode(child);
    }
}

In this way different parts of the program could use the node according to a concise interface. But each interface I add will require more methods in the Node class which could be unmaintainable. It seems like the presentation and data should be defined in separate classes, but I can’t think of a good way to do this while maintaining the ability to traverse the tree. Any suggestions?

1

Probably the most common way is to create a Node<T> with just the structure stuff and pass that into other functions that can use a T to do the presentation stuff.

If you’re having difficulty inverting control when doing a recursive traversal, try passing a function into a traversal method, something like (forgive syntax as I don’t know C#):

void DepthFirstTraversal(Node<T> node, Function func)
{
    func(node);
    foreach (Node<T> child in node.GetChildren())
        DepthFirstTraversal(child, func);
}

Another useful method to add functionality to a class, but somewhat less familiar depending on what languages you know, is using a mixin. It may or may not be useful in your case.

You can implement mixins in C# 3.0 using extension methods, and I believe there are third-party libraries to help. Mixins basically let you add functionality to a class without requiring it to implement an interface. The implementation is mixed in with the existing implementation of the class.

1

It seems like the presentation and data should be defined in separate classes, but I can’t think of a good way to do this while maintaining the ability to traverse the tree. Any suggestions?

The thing about interfaces is that you have to know them all up front. If you want to add new interfaces, you’ll need to use the Decorator pattern. But I think maybe you should re-evaluate your approach. Do you really need an interface for every new node manipulation? You can just add a function/static method that takes the tree as an argument and does whatever manipulation you need.

3

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