0

I am working with a big Java class MainAgent which has several inner classes, and altogether they make a 1500+ code lines, which makes maintainance of this class so hard. I need to replace these inner classes with saperate normal outside classes and instantiate them inside MainAgent.

Here is a dummy structure similar to my scenario.

class MainAgent {
    private List<String> agentNameList;
    private List<String> anotherList;
    private List<Behavior> behaviourList;

    // getters and setters

    public MainAgent(){
        // instantiate attributes
         behavourList.add( new AgentBehaviour());
    }
    
    
    class AgentBehaviour extends Behaviour {

        AgentBehaviour() {
            agentNameList.add(this.name);
            
            while(conditoin){
                cyclicBehaviour();
            }
        }

        private void cyclicBehaviour(){
            // access outer class attributes from this method
        }
    }
}

MainAgent is using several inner classes like this AgentBehaviour inner class. I need to move them into separate files, for better maintainability. Since inner classes are using attributes of the outer class. I need to access them somehow, after the migration. Can someone suggest me any design pattern or something similar to this issue.

PS: Better if something other than just passing MainAgent object reference to each behaviour object im creating inside the MainAgent constructor.

Example:

behaviourList.add(new AgentBehaviour(this));
user207421
  • 305,947
  • 44
  • 307
  • 483
LSampath
  • 138
  • 1
  • 3
  • 11
  • Why do you need the classes to be inner classes in the first place? Couldn't you just user 'normal' outer classes? – geanakuch May 13 '21 at 04:45
  • 2
    "Better if something other than just passing MainAgent object reference to each behaviour object im creating inside the MainAgent constructor." Actually this is what your inner classes are doing under the hood. – Sweeper May 13 '21 at 04:46
  • @geanakuch I am trying to reformat and extend some legacy code. Previous developer has used inner classes for ease of development. – LSampath May 13 '21 at 05:03
  • I strongly suggest you leave it alone. Your idea of maintainable differs from the original implementor's, and may differ from the next guy's. Don't create work. Solve real problems. – user207421 May 13 '21 at 05:04
  • @Sweeper thanks. Never thought of that. Will further refer into that fact. But still I need a better way to structure my code. – LSampath May 13 '21 at 05:05
  • @user207421 it was a concept model then. But we are working on production level right now. I would like it to be maintainable. – LSampath May 13 '21 at 05:07

0 Answers0