Design (How-to) classes containing collections of other classes

How to create classes associated with collections of other classes?

General example:

A workspace contains the number of projects .
The project contains a large number of Resources .
Each resource can contain a large number of Files .

Thus, the identified classes here may be Workspace, Project, Resource, and File. The workspace will have a list of Project.Project will have a list of resources and the resources will be a list of files. Of course, each class has corresponding settings.

Now the main questions:
a) Who creates and adds the class to a specific collection? Another class or class containing a collection?
b) Also, how to track a specific collection and how to store it?
c) Who checks the changes in a particular collection?
d) What are the various design patterns that can be applied in such situations?

Basically, I want to reduce the connection between different classes.

Thank you all

+5
source share
3 answers

There are many types of relationships - consider

  • Car and wheels
  • Car and driver
  • Car and registered owner
  • Order and order and order
  • School and class and class copy

UML-, , Aggegration , , .

, .

d). .

- , . , , , IDriver Person, IDriver , Automobile. , .

a). . , Factory. , factory. :

   IPerson aPerson = myAutomobile.createDriver( /* params */);  

  IPerson aPerson = aPersonFactory.create( /* params */);
  myAutomobile.addDriver(aPerson);

, , , - . , , OrderLines, ClassInstances?

). ? Collection. (-, - ..) , . - .. . / - (frontLeft, frontRight, backLeft, backRight, ). "" , . , JPA.

). ? , . , car.addDriver() . - , , . OO, , . : " " , , , addDriver(), , , .

+5

, , .

A WorkSpace Project + Project ^ 2 + Project ^ 3... ( , , WorkSpace)

,

+ ^ 2 + ^ 3...

+ ^ 2 + ^ 3...

, , # †, WorkSpace :

public class WorkSpace : IList<Project> //the important thing here is that you're declaring that things that are true for a list of Projects is true for a WorkSpace. The WorkSpace class may in fact do other stuff too...
{

}

.

:

foreach (var project in WorkSpace)
{
    //do stuff
}

Projects.Add(new Resource() { file1, file2, file3, file4, /* etc */});

. - . , ( - , , , ).

† , #, .

+2

OO - , , .

, . ? , , - . , , . - , . - , , .

, . , . , . , . , , . ; , , , - , , .

. , , , . . - , . , , , .

. /, , . , .

As for the audit. You can perform some audit (logging, statistics) from your classes, but if your audit needs are complex (you need to know when your data changes), it might be better to use the Observer template.

+1
source

All Articles