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();
	}

}

五、执行结果

相关推荐
HarmonSir1 天前
Codex 架构解析:一条对话如何长成一个运行时系统
后端·设计模式·架构
饮水桶_T2 天前
Java调用大语言模型 笔记
笔记·设计模式·电脑·ai编程
ssshooter3 天前
Replit 项目本地跑:一份实测过的改造清单
前端·后端·设计模式
饮水桶_T3 天前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AI酒旅实战开发4 天前
旅行 AI Agent 的 7 种设计模式:从工具调用到交易闭环
人工智能·设计模式
geovindu6 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
geovindu7 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh12 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊13 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua33313 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法