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 243367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:54:55+00:00 2026-05-11T20:54:55+00:00

I have a class A maintaining a list of objects class B. But each

  • 0

I have a class A maintaining a list of objects class B.
But each object of class B can be referenced in any object of class A.
Class B also maintains a list of objects of class A where it is being referenced.
The program can and will create (several) objects of both class A and B ‘at will’ and also delete them.

If I use C# I can add and delete objects from both classes with following code

public class A
{
    private List<B>ListOfObjects_B;
    public bool Add(B Object)
    {
       bool bAdd = false;
       if ((Object != null) && (ListOfObjects_B.IndexOf(B) <0))
       {
          ListOfObjects_B.Add(Object);
          Object.Add(this);
          bAdded = true;
       }
       return bAdded;
    }

    public bool Delete(B Object)
    {
       bool bDeleted = ListOfObjects_B.Remove(Object);
       if (bDeleted == true) Object.Delete(this);
       return bDeleted;
    }
}

public class B
{
    private List<A>ListOfObjects_A;
    public bool Add(A Object)
    {
        bool bAdd = false;
        if ((Object != null) && (ListOfObjects_A.IndexOf(A) <0))
        {
            ListOfObjects_A.Add(Object);
            Object.Add(this);
            bAdded = true;
        }
        return bAdded;
   }

   public bool Delete(A Object)
   {
       bool bDeleted = ListOfObjects_A.Remove(Object);
       if (bDeleted == true) Object.Delete(this);
       return bDeleted;
   }
}

This will work as because of removing/adding the object to the ListOfObjects the SECOND time (by recursion) the function will be called it will fail to delete/add thereby avoiding an infinite loop.

But I don’t like this code even though A and B do not know ‘much’ about the other class and just call a Delete/Add function.

I suppose this kind of problem is general and a design pattern exists for handling it in such a way that recursion can be avoided and updating both lists will be ‘just better’.
What design pattern should I use? I would appreciate if some code would be added as well.

  • 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-11T20:54:55+00:00Added an answer on May 11, 2026 at 8:54 pm

    You can simplify thing by moving the “object association concern” into a dedicated class. Here’s what I have in mind.

    Define a class called AssociationTable. This class will maintain a list of pairs where each pair holds a reference to an A object and a reference to a B object.

    Each A object (and each B object) will hold a reference to the AssociationTable object.
    A.Add(B) will be implemented as table.add(this, b);
    B.Add(A) will be implemented as table.add(a, this);

    Deletion will be implemented as table.delete(this, b) or table.delete(a, this)

    class Pair { 
      A a; B b; 
      Pair(A a, B b) { this.a = a; this.b = b; } 
      // Also override Equals(), HashCode()
    }
    
    class AssociationTalbe {
      Set<Pair> pairs = ...;
    
      void add(A a, B b) { pairs.add(new Pair(a, b)); }
      void remove(A a, B b) { pairs.remove(new Pair(a, b)); }
    }
    
    class A {
      AssociationTable table;
    
      public A(AssociationTable t) { table = t; }
    
      void add(B b) { table.add(this, b); }
      void remove(B b) { table.remove(this, b); }
    }
    

    Edit:
    The problem with this design is garbage collection. the table will hold references to objects thereby supressing their collection. In Java you could use a WeakReference object to overcome this issue. I am pretty sure there’s something similar in the .Net world

    Also, the table could be a singleton. I don’t like singletons too much. In here, a singleton will make the A-B association unique across your program. This may be something that is undesirable but it depends on your concrete needs.

    Finally, (just to put things in context) this design works the same way as Many-to-Many relationships in relational data bases.

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

Sidebar

Related Questions

I have a manager class maintaining a list of objects. Each Object has a
I have class method that returns a list of employees that I can iterate
Context: I have a class that implements a Session, maintaining a TCP connection to
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have class A, which exposes an event. an object of class B subscribed
I have a project where lots of the objects hold state by maintaining simple
I have a linq to sql class with several table bound properties, but I
I have some JS you'll see below. I want the inner class object to
I have some classes like Client, Employee, Property I am maintaining the list of
I have this class: public class CompositeSecurityAuthorizer implements SecurityAuthorizer { @inject @CompositeSecurityAuthorizerAnnot List<SecurityAuthorizer> authorizers;

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.