目录
一、关于单件模式
设计模式:17、单件模式(单例模式)_单例模式类图-CSDN博客
二、设计要求
设计一个champion单件类以及多个线程。每个线程从左向右水平移动一个属于自己的按钮,最先将按钮移动到指定位置的为冠军,即该线程将负责创建出champion单件类的唯一实例(冠军)。
三、设计实现
1.单件类的设计
java
package xyz.jangle.design.app.singleton;
/**
* 冠军,单例
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月20日 下午2:18:29
*
*/
public class Champion {
private static Champion uniqueChampion;
String message;
private Champion(String message) {
uniqueChampion = this;
this.message = message;
}
public static synchronized Champion getChampion(String message) {
if(uniqueChampion == null) {
uniqueChampion = new Champion(message+"是冠军");
}
return uniqueChampion;
}
public static void initChampion() {
uniqueChampion = null;
}
public String getMessage() {
return message;
}
}
四、应用程序
选手类:Player.java
java
package xyz.jangle.design.app.singleton;
/**
* 选手
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月20日 下午2:23:05
*
*/
import javax.swing.JButton;
import javax.swing.JTextField;
public class Player extends Thread{
int maxDistance;
int stopTime,step;
JButton com;
JTextField showMess;
Champion champion;
public Player(int stopTime, int step, int maxDistance, JButton com, JTextField showMess) {
super();
this.stopTime = stopTime;
this.step = step;
this.maxDistance = maxDistance;
this.com = com;
this.showMess = showMess;
}
@Override
public void run() {
while(true) {
int a = com.getBounds().x;
int b = com.getBounds().y;
if(a+com.getBounds().width >= maxDistance) {
champion = Champion.getChampion(com.getText());
showMess.setText(champion.getMessage());
}
a = a+step;
com.setLocation(a, b);
try {
sleep(stopTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主程序Application:
java
package xyz.jangle.design.app.singleton;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2026年10月20日 下午2:34:15
*
*/
public class Application extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton start;
Player playerOne,playerTwo,playerThree;
JButton one,two,three;
JTextField showLabel;
int width = 60;
int height = 28;
int maxDistance=460;
public Application() {
setLayout(null);
start = new JButton("开始比赛");
start.addActionListener(this);
add(start);
start.setBounds(200, 30, 90, 20);
showLabel = new JTextField("冠军会是谁呢?");
showLabel.setEditable(false);
add(showLabel);
showLabel.setBounds(300, 30, 120, 20);
showLabel.setBackground(Color.orange);
showLabel.setFont(new Font("隶书",Font.BOLD,16));
// 玩家1
one = new JButton("玩家1");
one.setSize(60, 30);
one.setBackground(Color.yellow);
playerOne = new Player(18,2,maxDistance,one,showLabel);
// 玩家2
two = new JButton("玩家2");
two.setSize(65, 30);
two.setBackground(Color.cyan);
playerTwo = new Player(19,2,maxDistance,two,showLabel);
// 玩家3
three = new JButton("玩家3");
three.setSize(62, 30);
three.setBackground(Color.green);
playerThree = new Player(21,2,maxDistance,three,showLabel);
initPosition();
setBounds(100,100,600,300);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void initPosition() {
Champion.initChampion();
showLabel.setText("冠军会是谁呢?");
repaint();
remove(one);
remove(two);
remove(three);
add(one);
add(two);
add(three);
one.setLocation(1, 60);
two.setLocation(1, 60+2+height);
three.setLocation(1, 60+4+height*2);
}
@Override
public void actionPerformed(ActionEvent e) {
boolean boo = playerOne.isAlive() ||playerTwo.isAlive()||playerThree.isAlive();
if(boo==false) {
initPosition();
int m = (int) (Math.random()*2) +19;
playerOne = new Player(m,2,maxDistance,one,showLabel);
m = (int)(Math.random()*3)+18;
playerTwo = new Player(m,2,maxDistance,two,showLabel);
m = (int)(Math.random()*4)+17;
playerThree = new Player(m,2,maxDistance,three,showLabel);
}
playerOne.start();
playerTwo.start();
playerThree.start();
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(maxDistance, 0, maxDistance, maxDistance);
}
public static void main(String[] args) {
new Application();
}
}
五、执行结果
