简体   繁体   中英

Create one general interface for GUI, have it implemented by many classes

I'm just writing an Application which consists out of 3 layers (GUI, AppLogic, DatabaseQueries). I want my GUI to communicate with the AppLogic just via interfaces (as it is common practice i guess). I'd like to have just one interface in the AppLogic that defines all the methods that the GUI can call. In my AppLogic I want many classes to implement this interface. So in my Controller class I´d like to create one instance of whatever is implementing the interface that the GUI can use to call the AppLogic Methods.

public interface AppControllerInterface {

    public void m1();
    public void m2();
    public void m3();
    public void m4();
    public void m4();

}

public class M1 implements AppControllerInterface{

    public void m1(){};

}

public class M2 implements AppControllerInterface{

    public void m2(){};

}  

...and so on

So, I'd like to specify as many classes as i want and create only one instance with which the GUI controller can call it´s methods

AppControllerInterface appCI = new WHATHERE???();

I'm aware of the face that I cannot do this the way I am thinking but maybe somebody knows how to solve this somehow. And apart from this, is there any reason for creating multiple interfaces for communicating with other layers?

Thanks a lot in advance for your help!

You should look into Model-View-Controller (MVC). This pattern should assist you with separating your business logic, database, and graphical elements.

To quote Martin Fowler:

"At the heart of MVC is what I call Separated Presentation. The idea behind Separated Presentation is to make a clear division between domain objects that model our perception of the real world, and presentation objects that are the GUI elements we see on the screen. Domain objects should be completely self contained and work without reference to the presentation, they should also be able to support multiple presentations, possibly simultaneously."

The following demonstrates a very primitive example of building these layers:

DefaultController.java - runs the show. Creates the view(s) and model(s). Instantiates the applications (via main method).

public class DefaultController
{
    final DefaultView view = new DefaultView();
    final DefaultModel model = new DefaultModel();

    public JFrame init()
    {
        this.view.addButtonActionListener(new ButtonAction(this.view, this.model));
        return this.view;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new DefaultController().init().setVisible(true);
            }
        });
    }
}

ButtonAction.java - not shown, but would implement ActionListener and handle the button click. It takes the model and the view as parameters because it may update the model or the view based on the event. The ButtonAction can be considered an extension of the controller.

DefaultModel.java - contains data that is presented (eg form values, connection settings, etc.).

public class DefaultModel
{
    boolean buttonClicked = false;

    public void setButtonClicked(boolean clicked)
    {
        this.buttonClicked = clicked;
    }

    public boolean getButtonClicked()
    {
        return this.buttonClicked;
    }
}

DefaultView.java - the actual display. This could be graphical or text-based. Assume that the example below contains a JButton labeled button .

public class DefaultView extends javax.swing.JFrame
{
    // ... auto generated code ...

    public void addButtonActionListener(ActionListener listener)
    {
        this.button.addActionListener(listener);
    }

    // Other view controls...
    public void setEnabledButton(boolean enabled)
    {
        this.button.setEnabled(enabled);
    }
}

Now, the DefaultController registers listeners for the view. It handles creating the view and model and ultimately displaying the view. The Controller only interacts with the view through the DefaultView public interface. The same is true with the DefaultModel . The DefaultView and DefaultModel do not interact with one another directly.

There are many different ways of implementing this (you'll find every person implements it slightly different), and a good tutorial can be found here .

MVC also has a few different variations (eg MVC, MVP, MVVM, etc.) that can be found on Wikipedia .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM