Alternative for an inner class, which is instantiated inside the outer class

229 views Asked by At

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));
0

There are 0 answers