Skip to main content
Home / Blog / Easy Data Access In Pages Of GWT/Swing Application

Easy Data Access In Pages Of GWT/Swing Application

Vishal Shukla June 30, 2009 1 min
Design PatternGWT

Overview

When developing desktop-based or similar applications, it becomes a headache to synchronize data between different pages. For example, when "Delete" is clicked in a context menu of some node of a tree, other Grid and panel elements need to be notified. Here is a simple way for accessing data of other panels using a singleton session object.

Implementation

We have a singleton session object (only one object at runtime) at each client's runtime. When we have data that needs to be used in other pages, we can put those objects in the session map and access it from other classes.

Here is how we can create a class that gives a singleton instance of the session map:

import java.util.HashMap;

public class SessionFactory {
  private static HashMap session;

  private SessionFactory(){
  }

  public static HashMap getSessionInstance(){
    if(session==null){
      session = new HashMap();
    }
    return session;
  }
}

How It Works

  1. The SessionFactory class provides a static method that returns a single HashMap instance
  2. The first time getSessionInstance() is called, it creates the HashMap
  3. Subsequent calls return the same instance
  4. Different pages and panels in the application can put their data into this shared session map
  5. Any page that needs access to data from another page can retrieve it from the session map

This design pattern ensures that data is centrally managed and easily accessible across different pages in a GWT or Swing application without creating tight coupling between components.

Tags

Design Pattern, GWT, Java, Swing

VS

Vishal Shukla

A member of the Brevitaz team sharing insights on software engineering, big data, and cloud technologies.

Back to all articles