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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:17:51+00:00 2026-05-17T20:17:51+00:00

I am currently struggling with a circular dependency problem when designing my classes. Ever

  • 0

I am currently struggling with a circular dependency problem when designing my classes.

Ever since I read about the Anemic Domain Model (something I was doing all the time), I have really been trying to get away from creating domain objects that were just “buckets of getters and setters” and return to my OO roots.

However, the problem below is one that I come across a lot and I’m not sure how I should solve it.

Say we have a Team class, that has many Players. It doesn’t matter what sport this is 🙂 A team can add and remove players, in much the same way a player can leave a team and join another.

So we have the team, which has a list of players:

public class Team {

    private List<Player> players;

    // snip.

    public void removePlayer(Player player) {
        players.remove(player);
        // Do other admin work when a player leaves
    }
}

Then we have the Player, which has a reference to the Team:

public class Player {
    private Team team;

    public void leaveTeam() {
        team = null;
        // Do some more player stuff...
    }
}

One can assume that both methods (remove and leave) have domain-specific logic that needs to be run whenever a team removes a player and a player leaves a team. Therefore, my first thought is that when a Team kicks a player, removePlayer(…) should also call the player.leaveTeam() method…

But then what if the Player is driving the departure – should the leaveTeam() method call team.removePlayer(this)? Not without creating an infinite loop!

In the past, I’d have just made these objects “dumb” POJOs and had a service layer do the work. But even now I’m still left with that problem: to avoid circular dependencies, the service layer still has link it all together – i.e.

public class SomeService {

    public void leave(Player player, Team team) {

        team.removePlayer(player);
        player.leaveTeam();

    }

}

Am I over complicating this? Perhaps I’m missing some obvious design flaw. Any feedback would be greatly appreciated.


Thanks all for the responses. I’m accepting Grodriguez‘s solution as it is the most obvious (can’t believe it didn’t occur to me) and easy to implement. However, DecaniBass does make a good point. In the situation I was describing, it is possible for a player to leave a team (and be aware of whether he is in a team or not) as well as the team driving the removal. But I agree with your point and I don’t like the idea that there’s two “entry points” into this process. Thanks again.

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

    You can break the circular dependency by adding guards to check if the team still has the player / the player is still in the team. For example:

    In class Team:

    public void removePlayer(Player player) {
        if (players.contains(player))
        {
            players.remove(player);
            player.leaveTeam();
            // Do other admin work when a player leaves
        }
    }
    

    In class Player:

    public void leaveTeam() {
        if (team != null)
        {
            team.removePlayer(this);
            team = null;
            // Do some more player stuff..
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My math classes are far behind, and I'm currently struggling to find a decent
Currently struggling with a problem that I've encountered variations on in the past. At
I'm currently struggling with the problem of trimming a breadcrumb navigation so it doesn't
Currently struggling with finding a way to validate 2 tables (efficiently lots of rows
I am currently struggling with whether or not I've achieved a good level of
I'm currently struggling on getting my CheckBox es to properly center within my GridViewColumn
I'm currently working on a UIWebView and am struggling to understand how Apple were
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently my workflow with Emacs when I am coding in C or C++ involves
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.

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.