March 4, 2021
Hot Topics:

Creating Dynamic Swing Views with Reflection by Extending MVC

  • By Vlad Kofman
  • Send Email »
  • More Articles »

I’m passing a reference to the View itself [ConcreteView1.this] as an optional parameter, but if I wanted too I could’ve used it to retrieve user data from the View and done something with it. MainController will instantiate any Model, if it has not done it yet, by using Reflection, add it to the HashMap, and call its action method. (See MainController execute method source.) I add each new Model objects to the HashMap right after instantiation, for the obvious reason not to instantiate a new object every time there is a request from the View. Therefore, if a new request comes, I first check if object already exists and only then complete action. See listing 2.

Model object is created once and is reused afterwards.

Please note that the model does not have any dependence on views or Controller. Generally speaking model layer encapsulates application domain’s concepts, both behavior and state. It responds to requests for information about its state and responds to instructions to change its state. In a well–designed project each concrete Model class will be based on some business logic need and may involve extensive operations on the data or a RDBMS access.

Models should also have clear and simple public interfaces so that if a view is completely changed, for instance from my Swing JTabbPane to a web JSPs, same methods wound be called and requests made.

Model Layer

I created a public ModelInterface interface class, and inherited it by all concrete Model classes. The only public method is doAction and it simply prints out which Model it belongs to. When Controller delegated the request from the View it calls this method.

ModelInterface model = (ModelInterface) models.get(model_key);
if (model == null) {
      Class newClass = Class.forName((String) model_key);
      //try to make an object
      ModelInterface anyModel = (ModelInterface)
      //try to add our new view tab here.
      models.put(model_key, anyModel);
      model = anyModel;

(See Controller source for more into)

The model beans don’t really do anything fancy, but you could effortlessly add more methods to the ModelInterface and their implementing in the concrete classes and then call them based in the key passed on through the controller.

As you can see, this project now has all the elements that can be used to expand View layer with more view. If I want to add another View, all I have to do is create a new class that extends AbstractView, add a new line in the property file for instance “NewTab”, call super.init() in View’s constructor and I’m done! The controller will create it, add it to the main application and show it. It’s that easy! See listing 3.

The incorporation of Model–View–Controller design architecture allowed for decoupling of presentation layer and logic it represents, and extension of MVC allowed for dynamic View generation. Naturally, once the controller is done and coding guidelines have been established, such as properties file format and inheritance structure, work on any project following this design can be concentrated on business logic and presentation by multiple developer groups, simultaneously. Reflection played a big part in automatic view generation, but if class encapsulation were preserved, developers working on the Views or Models would not need to know how their classes are instantiated, added to the main components, referenced and displayed.

Listing 1

Listing 2

Listing 3

Download source code jar here

About the Author

Vlad Kofman is a System Architect currently working on projects under government defense contracts. He has also been involved with enterprise level projects for major wall-street firms and US government. His main interests are object oriented programming methodologies and design patterns.


Sun Reflection API by Dale Green

WebSphere Studio Application Developer Version 5 Programming Guide
By Ueli Wahli, Ian Brown, Fabio Ferraz, Maik Schumacher, and Henrik Sjostrand
IBM Redbook SG24-6957-00, May 9, 2003

Core Java 2, Volume II: Advanced Features (5th Edition)
by Cay Horstmann, Gary Cornell
Publisher: Prentice Hall; 5th edition (December 10, 2001)

Page 2 of 2

This article was originally published on September 29, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date