Objects containing a list of the same type of object

Is there something wrong with defining something like this:

class ObjectA { property a; property b; List <ObjectA> c; ... } 
+4
source share
5 answers

No, and since the answer requires at least 30 characters, I’ll add that this is a common template.

Since you included the oop tag, I will add that this template gives a lot of control over the outside world. If c is a list of children, for example, you give everyone who has access to an ObjectA instance the ability to add, remove, or replace its children.

A tougher approach would be to use some kind of read-only type (perhaps to implement IList<ObjectA> ) to expose children.

EDIT

Please note that the following allows others to modify your list:

  class ObjectA { property a; property b; List <ObjectA> c; ... public List<ObjectA> Children { get { return c; } } } 

The absence of a setter only prevents outsiders from replacing the list object.

+7
source

Nope. This is perfectly acceptable. Tree structures do this.

+6
source

This is absolutely true. For example, you would need to do something similar to build a tree data structure (the parent node contains a list of child nodes).

+1
source

I have to ask if you have a question about putting the List <> there, or if it is about placing the <ObjectA> list inside ObjectA. and the answer to both questions: "Yes!"

the thing to keep in mind is that by default access is private. if you want other classes to use this list, you need to add a few things to your class ...

 class ObjectA { property a; property b; List <ObjectA> c; // allow access, but not assignment // you can still modify the list from outside, you just cant // assign a new list from outside the class public List<ObjectA> somePropertyName{ get { return this.c;}} // same as above, only allow derived child classes to set the list public List<ObjectA> somePropertyName{ get { return this.c;} protected set { this.c = value;} } // allow all access public List<ObjectA> somePropertyName{ get { return this.c;} set { this.c = value;} } } 
+1
source

No. It's really. Many structures use this chart as a template.

If you, for example, have a base collection class

 namespace MiniGraphLibrary { public class GraphCollection { public Node Root { set; get; } public Node FindChild(Node root) { throw new NotImplementedException(); } public Node InsertNode(Node root, Node nodeToBeInserted) { throw new NotImplementedException(); } } } 

Then you can do the node action as follows:

 namespace MiniGraphLibrary { public class Node { private string _info; private List<Node> _children = new List<Node>(); public Node(Node parent, string info) { this._info = info; this.Parent = parent; } public Node Parent { get; set; } public void AddChild(Node node) { if (!this.DoesNodeContainChild(node)) { node.Parent = this; _children.Add(node); } } public bool DoesNodeContainChild(Node child) { return _children.Contains(child); } } } 

Please note that this is what I wrote in 2 minutes, and this breakdown is not very good in production, but the main thing is that you have a parent node and many children. When you add a child of a node to a given node, you make sure that it has a parent node. Here I first check if the child is allready in the list of children before joining the two. You can make some changes to the code and make sure that if the child is deleted, the parent lists to which it is already connected. I didn’t do it.

I did this to illustrate how this can be used. And it is used in many places. Fx clustered indexes in MSSQL use some kind of tree similar to a view. But I am NOT an expert on this, so correct me if I am wrong.

I have not implemented two classes in the GraphCollection class. The disadvantage of my small example is that if you intend to implement the Find method, you must go through the entire graph. You can create a binary tree in which there are only two children:

 namespace MiniTreeLibrary { public class SimpleNode { private string _info; private SimpleNode _left; private SimpleNode _right; private SimpleNode _parent; public SimpleNode(Node parent, string info) { this._info = info; this.Parent = parent; } public Node Parent { get; private set; } } } 

I skipped the insert on the right and left. Now with this binary tree you can do a pretty quick search if you want! But this is another discrepancy.

There are many rules when they come trees and graphs, and my graph is even a real graph. But I gave these examples so you can see that it is used a lot! If you want to move more to linear and other data structures, see the Series of articles. In parts 3, 4, and 5, they talk more about trees and graphs.

+1
source

Source: https://habr.com/ru/post/1386587/


All Articles