17、设计模式应用举例:单件模式:多线程争冠军

目录

一、关于单件模式

二、设计要求

三、设计实现

1.单件类的设计

四、应用程序


一、关于单件模式

设计模式: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();
	}

}

五、执行结果

相关推荐
高晶13 小时前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
高晶13 小时前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
延晗14 小时前
【从零开始 DeepSeek Harness 插件开发教程】二、一切皆插件:简单理解一下 Cordis 内核与 DSH 心智模型
设计模式·deepseek
高晶15 小时前
Android 通讯录应用开发实战:从零搭建 SQLite 数据持久化完整项目
android·设计模式·前端框架
日和雫20 小时前
封装、继承、多态 & 组合,为什么组合大于继承?
设计模式
代钦塔拉1 天前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式
haluhalu.1 天前
深入理解AIAgent(1.3)--- 设计模式
人工智能·python·设计模式·langchain
高晶2 天前
CANopen 在 BMS 系统中的 C 语言实战指南
设计模式·程序员
博风2 天前
13、设计模式应用举例:工厂方法模式:创建药品对象
设计模式·工厂方法模式