Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:39:03+00:00 2026-05-27T03:39:03+00:00

I have this situation where I have a parent child relationship between two sets

  • 0

I have this situation where I have a parent child relationship between two sets of data. I have a parent document collection and a child document collection. The requirement is that the parents and their corresponding children need to be exported into ‘a’ pdf document. A simple-implementation of the above situation can be as follows(java-ish pseudo code below):

for(Document parentDocument:Documents){
   ExportToPdf(parentDocument);
    for(Document childDocument:parentDocument.children()){
      AppendToParentPdf(childDocument);  
  }
}

Something as above will probably solve the problem, but all of a sudden the requirements changes and now each of these parents and their corresponding children need to be in separate pdfs, so the above given snippet is modified by changing the AppendToParentPdf() to ExportToPdf() follows:

for(Document parentDocument:Documents){
   ExportToPdf(parentDocument);
    for(Document childDocument:parentDocument.children()){
      ExportToPdf(childDocument);  
  }
}

Going along this way, it will not take long before this seemingly trivial snippet would suffer from some serious code smells.

My questions to SO are:

  1. Are there better representations of parent-child relationships such as the above where instead of brute-forcing my way through all the documents and their children in an O(n^2) fashion, I can use a different data-structure or technique to traverse the entire structure in a more optimal fashion.

  2. In the scenario that I described above, where the business rules are rather fluid about the way the pdfs should be exported, is there a smarter way to code the nature of the export function? Also the export format is transient. PDFs can give way to *.docs/csvs/xmls et al.

It will be great to get some perspective on this.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:39:04+00:00Added an answer on May 27, 2026 at 3:39 am

    You could encapsulate what you want to do with a document in a handler. This will also allow you to define new handlers in the future that you can pass to existing code.

    interface DocumentHandler {
        void process(Document d);
    }
    
    class ExportToPdf implements DocumentHandler { ... }
    class AppendToParentPdf implements DocumentHandler { ... }
    
    // Now you're just passing the interface whose implementation does something with the document
    void handleDocument(DocumentHandler parentHandler, DocumentHandler childHandler) {
        for(Document parent : documents) {
            parentHandler.process(parent);
    
            for(Document child : parent.children()) {
                childHandler.process(child);
            }
        }
    }
    
    DocumentHandler appendToParent = new AppendToParentPdf();
    DocumentHandler exportToPdf = new ExportToPdf();
    
    // pass the child/parent handlers as needed
    handleDocument(exportToPdf, appendToParent);
    handleDocument(exportToPdf, exportToPdf);
    

    As for efficiency, I’d say don’t try to optimise unless you run into performance issues. In any case, the problem won’t be with the nested loop but with the logic itself that processes the documents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this situation I have two models, Comment and Score. The relationship is defined
I have two tables with parent - child relationship: PARENT table with id as
I have two classes: Parent and Child , mapped one-to-many with relationship owned by
So, here's the situation: I have a parent buildfile that defines a compilation task,
In a situation where you have a parent class which has one child class,
I have a parent/child relationship defined which, unfortunately, cannot be maintained via a foreign
I have a Parent entity with a Child entity in a ManyToOne relationship: @Entity
I have an object-oriented parent-child tree in PHP that I want to clone. The
I have this situation: { float foo[10]; for (int i = 0; i <
I have this situation where I want to display a list of Administration objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.