[引言]
我们在学习软件开发面向对象编程思想的时候,要深入理解面向对象的设计思想,就会接触到一些设计模式。其中单例模式就是一个使用和面试频度相当高的设计模式。今天小博老师就为大家讲解单例模式的运用案例。
[步骤阅读一]单例模式的作用
我们首先来制作一个简单的Java窗体程序,程序启动后实例化登录窗体,在登录窗体中点击“注册”按钮后,会弹出注册窗体。登录窗体核心代码如下:
package com.bwf.technology.javase.jswing;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class BWFLogin extends JFrame{
public BWFLogin(){
super("www.51code.com");
setBounds(200, 100, 320, 245);
setLayout(null);
logo = new JLabel(new ImageIcon("files/bwf_logo.png"));
logo.setBounds(10, 10, 281, 75);
this.add(logo);
lb1 = new JLabel("账户名称:");
lb1.setBounds(5, 100, 80, 25);
this.add(lb1);
txtUsername = new JTextField();
txtUsername.setBounds(80, 100, 200, 25);
this.add(txtUsername);
lb2 = new JLabel("账户密码:");
lb2.setBounds(5, 130, 80, 25);
this.add(lb2);
txtPassword = new JPasswordField();
txtPassword.setBounds(80, 130, 200, 25);
this.add(txtPassword);
btLogin = new JButton("登 录");
btLogin.setBounds(100, 160, 80, 25);
this.add(btLogin);
btRegist = new JButton("注 册");
btRegist.setBounds(200, 160, 80, 25);
this.add(btRegist);
btRegist.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
new BWFRegist();
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JLabel logo;
public JLabel lb1;
public JLabel lb2;
public JTextField txtUsername;
public JPasswordField txtPassword;
public JButton btLogin;
public JButton btRegist;
}
注册窗体的核心代码如下:
package com.bwf.technology.javase.jswing;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class BWFRegist extends JFrame{
public BWFRegist(){
super("欢迎加入博为峰培训");
setBounds(300, 150, 300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
如果我们只是这样编程程序的话,当用户多次点击注册按钮,就会出现多个注册窗体:
我们希望只能有一个注册窗体同一时间存在,当用户点击登录窗体中的“注册”按钮后,如果此时没有注册窗体,那就实例化一个注册窗体,而如果此时已经存在了注册窗体,就返回已经存在的注册窗体实例。
这个时候,我们就需要将注册窗体类设计成单例模式类了,单例模式的作用就是控制一个类同一时间只能存在一个实例。
[步骤阅读二]单例模式实现
现在我们修改注册窗体为单例设计模式,核心代码如下:
package com.bwf.technology.javase.jswing;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class BWFRegist extends JFrame{
private static BWFRegist instance;
private static String key = "welcome to bwf";
public static BWFRegist getInstance(){
if( instance == null){
synchronized (key) {
if( instance == null ){
instance = new BWFRegist();
}
}
}
return instance;
}
private BWFRegist(){
super("欢迎加入博为峰培训");
setBounds(300, 150, 300, 300);
this.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
if( instance != null ){
synchronized (key) {
if( instance != null ){
instance.dispose();
instance = null;
}
}
}
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
});
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
然后对于登录窗体中“注册”按钮的点击事件稍作修改,把获得注册窗体实例的代码修改为:
// new BWFRegist();
BWFRegist.getInstance();
这样一来,我们就实现了同一时间,只会有一个注册窗体存在啦。
来源:oschina
链接:https://my.oschina.net/u/2971691/blog/803071