Thursday, June 4, 2009

Model fighter game UI with Swing (SINGLETON design pattern) - Part 2

In Model fighter game UI with Swing - Part 1, I have created the JFrame for fighters' detail view, before I create the main view which shows the thumbnails of all fighters, I need to figure out how to switch between them.


Once switch button clicked in the MainView frame, MainView should call set itself invisible, then get an instance of DetailView and set it visible. The problem is how to get an instance of DetailView. Calling DetailView's constructor directly is problematic, because at anytime, I don't want to create more than one instance of the DetailView. What I want is a getter method, if the DetailView has not been created, then create it and return it to me; otherwise, just return the existing instance to me.
The SINGLETON design pattern is the perfect solution here. By declaring the constructor DetailView() to be private, I can be assured that the only way of creating new instance is by calling getSingleton() method. In getSingleton(), we only call constructor at the first time, which guarantees only one instance is created.
(notice for thread safety, a little bit more complexed singleton pattern will be used instead.)

MultipleFramesExample.java



/*MultipleFramesExample.java*/
package FrameViews;
import java.awt.*;
import javax.swing.*;
public class MultipleFramesExample {
public MultipleFramesExample() {
MainView.getSingleton().setVisible(true);
DetailView.getSingleton().setVisible(false);
}

public static void main(String[] args) {
new MultipleFramesExample();
}
}


MaiView.java


/*MainView.java*/
package FrameViews;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame implements ActionListener {
private static MainView theHandel;

private MainView() {
super("Main View");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setLocationRelativeTo(null);
super.setLocation(300, 400);
JPanel frame1 = (JPanel)super.getContentPane();
setSize(150,100);
frame1.setVisible(true);
JButton button1 = new JButton("Click for Detail View");
frame1.add(button1);
button1.addActionListener(this);
pack();
}

public static MainView getSingleton() {
if (theHandel == null) {
theHandel = new MainView();
}
return theHandel;
}

public void actionPerformed(ActionEvent event) {
theHandel.setVisible(false);
DetailView.getSingleton().setVisible(true);

}
}


DetailView.java


/*DetailView.java*/
package FrameViews;
import java.awt.event.*;

import javax.swing.*;
public class DetailView extends JFrame implements ActionListener {
private static DetailView theHandel;

private DetailView() {
super("Detail View");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setLocation(300, 500);
JPanel frame1 = (JPanel)super.getContentPane();
setSize(150,100);
frame1.setVisible(true);
JButton button1 = new JButton("Click for Main View");
frame1.add(button1);
button1.addActionListener(this);
pack();
}

public static DetailView getSingleton() {
if (theHandel == null) {
theHandel = new DetailView();
}
return theHandel;
}

public void actionPerformed(ActionEvent event) {
theHandel.setVisible(false);
MainView.getSingleton().setVisible(true);

}
}


No comments:

Post a Comment

Why I stopped publishing blog posts as information provider

Now the AI can generate content. Does that mean the web publishing industry reaches the end? ChatGPT said: ChatGPT Not at all. While AI can ...