[引言]java
咱们在学习软件开发面向对象编程思想的时候,要深刻理解面向对象的设计思想,就会接触到一些设计模式。其中单例模式就是一个使用和面试频度至关高的设计模式。今天小博老师就为你们讲解单例模式的运用案例。面试
[步骤阅读一]单例模式的做用编程
咱们首先来制做一个简单的Java窗体程序,程序启动后实例化登陆窗体,在登陆窗体中点击“注册”按钮后,会弹出注册窗体。登陆窗体核心代码以下:设计模式
package com.bwf.technology.javase.jswing;学习
import java.awt.event.MouseEvent;this
import java.awt.event.MouseListener;设计
import javax.swing.ImageIcon;code
import javax.swing.JButton;对象
import javax.swing.JFrame;blog
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();
这样一来,咱们就实现了同一时间,只会有一个注册窗体存在啦。