10

I'm having trouble defining this data pattern I've come across working on several applications.

It consists of:

  1. An object type which is composed of many objects itself
  2. A second object type, where each instance 'has many' of the first object
  3. And, each of the sub-objects of the first object are modifiable per each association to the second object type.

A simple example might be:

  1. A programming course consisting of a set of lessons
  2. The lessons are composed of a set assignments.
  3. A course can be assigned to a student.
  4. However, once a course is assigned to a student each lesson and/or assignment can be customized to that student, with removals and additions, to the point where the original course may be unrecognizable.

In my solutions, what this results in is:

On assignment of a course to a student, the course is loaded into memory. Then for each sub-object, a student/sub-object relationship object is generated with the appropriate metadata. Essentially, I'm using the original object as a template to generate the required customizable objects.

This results in a huge amount of data as the sub-objects become more complex and numbered. I'm wondering if there is some optimization or pattern to reduce the amount of logic/complexity required to manipulate this data pattern.

  • 2
    Are you sure you want to "reduce the amount of data"? Are you instead looking for ways to "reduce the amount of non-trivial code and logic" that needs to be written to implement the required behavior? (I notice that the ongoing management of the data requires relational data structure, similar to a database.) – rwong Aug 29 '15 at 2:35
  • @rwong Yes, "reduc[ing] the amount of non-trivial code and logic" is my end goal. To me, that means reducing the data complexity in some way, but that's not necessarily a requirement. It's become such a common data pattern that I wonder if there is some simpler way to manage it. – Nicholas Pickering Aug 29 '15 at 2:42
  • 1
    In principle this is an enhanced version of an m:n relationship. What about a title like »How to manage complex object relationships«? – Thomas Junk Aug 29 '15 at 9:54
  • 1
    A huge amount of data is not the same as a huge level of complexity in the data. The difficulty in managing what you are building will probably grow with the complexity more than with the volume. – Walter Mitty Aug 29 '15 at 13:07
  • 1
    Interesting. I've worked on several applications that have this pattern, but never spotted before that it its a pattern. Would also love to see simpler ways of managing this kind of data. – Jules Aug 29 '15 at 13:46
6

I see a few options depending on what you need: (1) if there are many unique instances that follow a common algorithm, (2) if there are many similar objects or you will generated objects at run-time, and (3) if you want to dynamically modify the object's behavior while running. Note: you can combine all of the patterns I mention here, if need be.

  1. If each "second object type" is unique but follows a similar pattern of behavior, you could use Template Pattern. It sounds like you may be doing this. But to make it explicit, your abstract, base class has general algorithm programmed; certain steps of this algorithm are implemented in the derived classes.

  2. If you will create many objects or if creating objects at run-time is important to you, you could use Factory Pattern.

  3. And if you wish to dynamically change behavior, Stategy Pattern could work. For example, if a student in a regular curriculum is decided to be special needs or to go into an accelerated program. This works by composing the "student" of an object that would represent a base-class of the curriculum. The curriculum would be assigned to a derived curriculum at the student's construction (that sounds weird) and could be reassigned to another derived curriculum later.

(Just FYI, if using (3) Strategy Pattern with C++, you will have to Rvalues for composition.)

To store your objects and second objects, it may be worth considering Iterator Pattern (to cycle through them, add, delete, sort, etc.).

A good reference is Head First Design Patterns, which covers the patterns I mention and their implementation. They work in Java.

0

I'm finding it hard to believe, under the presence of a data-store or persistence, that you would need to have objects with this kind of depth at any given point in a run-time. Is this for CRUD GUI's? If so, then I would suggest changing your approach from the start. IE:

Identify the substructure necessary for the Student to show, and statefully store its origin index back to the db, and statelessly update that, going to or from the view and the backend db.

  • I'm not sure I understand your suggestion. I should generate an empty subobject, then force the user to another form which allows them to modify the subobject? – Nicholas Pickering Sep 7 '15 at 19:56
  • I'm suggesting that objects in of themselves don't accomplish much, if all you're doing is a stateless transaction between their content and a back-end database. If this is the case, remove them and just perform the transaction for data particular to what faces the client. – John P. Feltz Sep 7 '15 at 20:17
  • Edit: This may also mean creating objects to capture that particular data being transacted- if you're biased to ORM due to their convenience in that respect. – John P. Feltz Sep 7 '15 at 20:41
  • Unless I'm misunderstanding the, I don't think anything about the process can be made stateless. It all depends on the context of the user action. When a user creates a primary object, they expect the substructure to be available to modify immediately. – Nicholas Pickering Sep 8 '15 at 13:26
-1

A customized course for each student to the point where the original course is unrecognizable suggests that the original course is simply a “default” reference. What I would do is create a class called CustomizedCourse (or a list of them) and have that (or a list of that) as a property of a student. The CustomizedCourse can have a reference to the original course for “referential” usage but the main work and data would be in the CustomizedCourse itself.

  • downvoter care to comment? – frezq Sep 8 '18 at 15:37
  • I didn't downvote (I'm the OP), but it seems like you're trying to describe the Template or Strategy Pattern which were already described in another answer. – Nicholas Pickering Sep 13 '18 at 1:39

protected by gnat Sep 5 '18 at 0:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.