1

I have a number of different organisations, each of which is an instance of the Organisation class. This contains getters and setters for instance variables, the setters contain validation where appropriate. It also has a few other odds and ends - overwritten toString(), equals() and hashCode() for example.

I have OrganisationView extends JFrame which accepts an Organisation and displays it in the GUI, and includes listeners for the various functions.

I understand from this question how this should all fit together and work with OrganisationController. What I'm not clear on is how many, if any, instances of OrganisationController I need. Is it one per organisation, and storing the organisation it refers to as an instance variable? Because it seems easier just to declare OrganisationController as static and call its methods directly from OrganisationView giving OrganisationView a method something like:

private boolean deleteButtonPressed(){
    return OrganisationController.deleteOrganisation(this.organisationDisplayed)
}

(and perhaps some other business logic, but that's by the by)

OrganisationView, by the way, is called each time that particular display is needed, and is passed the organisation to show.

My question is: If it is better to have a separate instance of OrganisationController for each Organisation then why? It seems like an unnecessary amount of objects differing only in one instance variable. Or are static controllers acceptable?

Community
  • 1
  • 1
MrB
  • 818
  • 8
  • 28

2 Answers2

0

I would not make it static. Use a singular controller and separate your views into directories. Then you can organized each part accordingly. You don't want to statically call the controller from the view. You want each person who logs in to have their own instance. Its simply a matter of separating out your views, models etc into separate folders and directories. I'm actually working on a project right now where I do this. I prepend each section with a keyword so as to keep it separate.

Dale
  • 1,613
  • 4
  • 22
  • 42
0

You can use the Singleton pattern to make sure that you only create one Controller && also access your controller in a static way.

I suggest you go for the enum implementation of Singleton which would be something like this

public enum OrganisationController{
   INSTANCE;

    // you can have fields
    private final example;
    // and also methods
    public boolean deleteOrganisation(Organization org){
    // do whatever here
    }
}

And you can invoke methods in a static-looking way

OrganisationController.INSTANCE.deleteOrganization(org);
Xipo
  • 1,765
  • 1
  • 16
  • 23