0

I'm currently writing a small application following the MVC model I have a Controller class which initiates the Model and View classes. The Controller class consists of several inner private classes that are subclasses of AbstractAction used for responding to GUI events, setting Icons and mnemonics.

Now my problem is that this class is already getting big, I have about 300 lines of code for 7 Action classes, so I'm thinking about splitting the Controller and put these Action classes into their own files, adding them to a new controller.actions package.

However, the Actions need to be aware of the model and views, in other words, all of the things that the Controller already does, which was my reason for putting them as inner classes to the Controller in the first place. If I make them separate classes, I will have to have a reference to the Controller somehow (passing to the constructor or making a new abstract base class for the Actions).

Is it a good idea to expose the Actions in separate classes? If so, how should I let them know what the Controller now knows? Or should I keep them as inner, private classes? Or any other solution?

pg-robban
  • 1,395
  • 4
  • 21
  • 42
  • 2
    I wonder if using a Mediator Design Pattern would help by giving the sub control classes access to the model and view. – Hovercraft Full Of Eels Jan 18 '15 at 16:47
  • 2
    As noted [here](http://stackoverflow.com/a/25556585/230513), "not _every_ interaction needs to pass through your application's controller." – trashgod Jan 18 '15 at 20:31
  • 2
    I code separate action classes when I build a Swing GUI. Each action class handles one action on the GUI. I pass the model class instance and if necessary the JFrame instance to all of the action classes. I code my action classes as anonymous inner classes, inner classes, and separate classes depending on the size of the class and the number of external fields the action class needs. – Gilbert Le Blanc Jan 18 '15 at 21:42
  • 2
    IF the `Action`s are suitably abstract and could be reusable in other parts of the application, you could expose the model/view via appropriate interfaces. The context of the action is important, if it's a UI action, it might need to be related to the view and not the controller and the same for model actions. This allows the view/model to communicate changes directly to the controller without exposing the controller to the actions – MadProgrammer Jan 18 '15 at 23:38

0 Answers0