best approach to code hierarchical classes ?

someone someone at somewhere.com
Tue Jun 8 00:54:41 UTC 2021


Consider the following code in what I used nested-classes for the 
first time within D:

```d
import std.string;
import std.stdio;

class classComputers {

    classComputers lhs;
    classComputers rhs;

    int opApply(int delegate(classComputer) dg) { /// boilerplate 
code to handle the class's default collection

       int lintResult = 0; /// must find a better name

		foreach (lobjComputer; computers) { /// looping over the 
computers starting in current node

			lintResult = dg(lobjComputer); /// passing single object to 
the loop body

			if (lintResult != 0) { break; }

		}

       if (lintResult != 0 && lhs ! is null) { lintResult = 
lhs.opApply(dg); } /// recursing child nodes
       if (lintResult != 0 && rhs ! is null) { lintResult = 
rhs.opApply(dg); } /// recursing child nodes

       return lintResult;

    }

    public classComputer[] computers; alias computers this; /// 
ie: default property

    private string pstrNetwork;

    final @property string network() { return this.pstrNetwork; }
    final @property void network(in string lstrNetwork) { 
this.pstrNetwork = lstrNetwork.strip(); }

    this(
       string lstrNetwork
       ) {

       this.network = lstrNetwork;

    }

    class classComputer {

       private string pstrName;

       final @property string name() { return this.pstrName; }
       final @property void name(in string lstrName) { 
this.pstrName = lstrName.strip(); }

       this(
          string lstrName
          ) {

          this.name = lstrName;

       }

    }

}

void main (

    ) {

    classComputers lobjComputers = new classComputers(r"lab"c); 
/// ie: the lab network

    lobjComputers ~= lobjComputers.new classComputer(r"dell"c);
    lobjComputers ~= lobjComputers.new classComputer(r"ibm"c);
    lobjComputers ~= lobjComputers.new classComputer(r"apple"c);
    lobjComputers[1].name = r"lenovo"c;

    foreach(lobjComputer; lobjComputers) { writeln(r"["c ~ 
lobjComputer.outer.network ~ r" network has "c ~ 
lobjComputer.name ~ r"]"c); }

}
```

As expected, the above code produces:

[lab network has dell]
[lab network has lenovo]
[lab network has apple]

Now think of wider hierarchical structures, let's say 6~9 to 
begin with or greater; eg: computers.computer.devices.firmware... 
and so on.

Coding such scenario with nested classes will produce a huge code 
base within the same module. My first approach, obviously, was to 
put each class in its own module and in main() I instantiate the 
primary one and the child objects/classes are instantiated either 
directly in main or say, by each class' own add()/remove() 
methods. This works as expected. However, I can't manage to 
traverse the tree from main() like something.parent.somethingelse 
(like I can do with outer for the nested classes example I gave 
you) nor I am able to access parent members within the classes 
own bodies.

Are there alternatives to nested classes for such scenarios ?


More information about the Digitalmars-d-learn mailing list