Skip to content
/ VMVC Public

An implementation of the Model-View-Controller pattern for my personal Swing projects.

License

Notifications You must be signed in to change notification settings

Valkryst/VMVC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java CI with Maven CodeQL

This project is a Java implementation of the MVC design pattern. It is designed to be used with Swing UIs and for my personal use. It is not intended to be a general purpose MVC framework, but you are free to use it as such if you wish.

The design goals are as follows:

  1. Separate the View from the Model. This not only makes the code cleaner, easier to read and maintain, but it also allows for easier testing of "business" logic in the Model by having it separate from the UI.
  2. Separate some of the Listener logic from the View to the Controller. This allows for easier testing, without the need for something to click through a UI to trigger a Listener.
  3. Provide methods to perform get, set, or compound operations on the Model VIA the Controller. Again, this allows for cleaner code in the View and less methods in the Model.

The classes can be described as follows:

  • Model - Contains data and "business" logic for manipulating the data. It has no knowledge of the View or Controller.
  • View - A Swing UI. It has no knowledge of the Model and can only get/set data VIA the Controller.
  • Controller - Provides methods for getting/setting data in its Model. It may also implement various Listener interfaces (e.g. ActionListener, ChangeListener, etc.) if it needs to communicate with the Model when the View changes.

Links

Installation

VMVC is hosted on the JitPack package repository which supports Gradle, Maven, and sbt.

Gradle Gradle

Add JitPack to your build.gradle at the end of repositories.

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add VMVC as a dependency.

dependencies {
	implementation 'com.github.Valkryst:VMVC:0d3561d613db3f790f1639a04fb22a7a63ad44f0'
}

Maven Maven

Add JitPack as a repository.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add VMVC as a dependency.

<dependency>
    <groupId>com.github.Valkryst</groupId>
    <artifactId>VMVC</artifactId>
    <version>0d3561d613db3f790f1639a04fb22a7a63ad44f0</version>
</dependency>

Scala SBT Scala SBT

Add JitPack as a resolver.

resolvers += "jitpack" at "https://jitpack.io"

Add VMVC as a dependency.

libraryDependencies += "com.github.Valkryst" % "VMVC" % "0d3561d613db3f790f1639a04fb22a7a63ad44f0"

Example

Using the Controller, Model, and View classes below, we can display the View in a Swing UI as follows:

import javax.swing.*;
import java.awt.*;

public class Example {
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            final var frame = new JFrame("Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setPreferredSize(new Dimension(640, 480));
            
            // createView() will call the overridden methods in ExampleModel.
            frame.add(new ExampleModel().createView());
            
            frame.setVisible(true);
            frame.pack();
            frame.setLocationRelativeTo(null);
        });
    }
}

Controller

// This can implement various Listener interfaces, if the View requires it.
public class ExampleController extends Controller<ExampleModel> {
    public ExampleController(final ExampleModel model) {
        super(model);
    }
    
    // Add methods to perform get, set, or compound actions on the Model.
  
    // Add overridden methods from Listener interfaces, if the View requires it.
}

Model

public class ExampleModel extends Model<ExampleController, ExampleView> {
    // Add instance variables to store data used by the View.
    
    @Override
    protected ExampleController createController() {
      return new ExampleController(this);
    }
  
    @Override
    protected ExampleView createView(final ExampleController controller) {
      return new ExampleView(controller);
    }
    
    // Add methods to perform "business" logic on the data.
}

View

public class ExampleView extends View<ExampleController> {
    public ExampleView(final ExampleController controller) {
        super(controller);
        
        // Create a Swing UI within ExampleView.
    }
}

Credits & Inspiration

About

An implementation of the Model-View-Controller pattern for my personal Swing projects.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages