Java电梯模拟升级版

Java电梯模拟升级版


文章目录

  • Java电梯模拟升级版
  • 前言
  • 一、UML类图
  • 二、代码
  • 三、测试


前言

在上一版的基础上进行升级,楼层采用享元模式进行升级,并对楼层对象进一步抽象


一、UML类图

在这里插入图片描述

二、代码

电梯调度器抽象类

package cn.xx.evevator;import java.util.*;public abstract class AbstarctDispatcher {protected  AbstractEvevator abstractEvevator;public void setAbstractEvevator(AbstractEvevator abstractEvevator) {this.abstractEvevator = abstractEvevator;}protected abstract void pressFloor(Integer[] floors);protected abstract Floor getFloor(Integer floor);public abstract void run(int floor,Status status);public abstract void changeTargetMax(Integer floor);}

调度器继承类

package cn.xx.evevator;import java.util.Arrays;public class Dispatcher extends AbstarctDispatcher{@Overrideprotected void pressFloor(Integer[] floors) {if (floors!=null && floors.length>0){this.abstractEvevator.status.handler(this,floors);}}@Overrideprotected Floor getFloor(Integer floor) {return this.abstractEvevator.instance.getFloor(floor);}@Overridepublic synchronized void run(int floor, Status status) {abstractEvevator.run(floor,status);}@Overridepublic void changeTargetMax(Integer floor) {if (abstractEvevator.targetFloorMax<floor){abstractEvevator.targetFloorMax = floor;}}}

电梯抽象类

package cn.xx.evevator;public abstract class AbstractEvevator {public static final Integer FLOOR_TOTAL = 11;protected final AbstarctDispatcher abstarctDispatcher;protected final FloorFactory instance;protected  Status status = StatusCommon.stopStatus;protected static Integer targetFloorMax = 1;protected static Integer targetFloorMin = 1;protected Integer currentFloor = 11;protected AbstractEvevator(AbstarctDispatcher abstarctDispatcher) {this.abstarctDispatcher = abstarctDispatcher;this.instance = FloorFactory.getInstance();}abstract void run(int floor,Status status);abstract void up();abstract void down();abstract void openDoor(int floor);}

电梯继承类

package cn.xx.evevator;public class Evevator extends AbstractEvevator{protected Evevator(AbstarctDispatcher abstarctDispatcher ) {super(abstarctDispatcher);}@Overridevoid run(int floor, Status status) {if (floor>this.targetFloorMax){this.targetFloorMax = floor;}if (this.status.NAME.equals(StatusCommon.stopStatus.NAME)){if (this.currentFloor==floor){this.openDoor(floor);this.status = status;}if (this.currentFloor<floor){this.status = StatusCommon.upstatus;this.up();}if (this.currentFloor>floor){this.status = StatusCommon.downStatus;this.down();}}}@Overridevoid up()  {if (targetFloorMax==this.currentFloor){openDoor(targetFloorMax);}else {while(this.currentFloor<targetFloorMax){System.out.println(Thread.currentThread().getName()+"电梯正在上升中,目前在" + this.currentFloor+"层,目标:"+targetFloorMax);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Floor floor1 = instance.getFloor(this.currentFloor);if (floor1.isOpen){openDoor(this.currentFloor);floor1.isOpen=false;}if (floor1.upKey) {floor1.notifyPersonLiftHere();}this.currentFloor++;}}}@Overridevoid down() {if (targetFloorMin==this.currentFloor){openDoor(targetFloorMin);}else {while(this.currentFloor>targetFloorMin){System.out.println(Thread.currentThread().getName()+"电梯正在下降中,目前在" + this.currentFloor+"层,目标:"+this.currentFloor);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Floor floor1 = instance.getFloor(this.currentFloor);if (floor1.isOpen){openDoor(this.currentFloor);floor1.isOpen=false;}if (floor1.downKey) {floor1.notifyPersonLiftHere();}this.currentFloor--;}}}@Overridevoid openDoor(int floor) {System.out.println(floor+"层开门中");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}

楼层工厂

import java.util.HashMap;public class FloorFactory {private final HashMap<Integer, Floor> map = new HashMap<>();private static AbstarctDispatcher abstractDispatcher = Context.getDispatcher();private FloorFactory(){for (int i = 1; i <= AbstractEvevator.FLOOR_TOTAL ; i++) {map.put(i,new Floor(i,abstractDispatcher));}}private final static FloorFactory floorFactory = new FloorFactory();public static FloorFactory getInstance(){return floorFactory;}public Floor getFloor(Integer floor){return map.get(floor);}
}

楼层抽象类

public abstract class AbstractFloor {protected Boolean upKey = false;protected Boolean downKey = false;protected Boolean isOpen = false;protected final int currentFloor;protected Status status;protected AbstarctDispatcher abstarctDispatcher;protected LinkedHashSet<Person> personList = new LinkedHashSet<>();protected AbstractFloor(int currentFloor, AbstarctDispatcher abstractDispatcher) {this.currentFloor = currentFloor;this.abstarctDispatcher = abstractDispatcher;}protected abstract void pressFrom(Person person);protected abstract void notifyPersonLiftHere();}

楼层继承类

public class Floor extends AbstractFloor{protected Floor(int currentFloor, AbstarctDispatcher abstractDispatcher) {super(currentFloor,abstractDispatcher);}@Overrideprotected void pressFrom(Person person) {person.status.press(this);LinkedHashSet<Person> personList = this.personList;if (!personList.contains(person)){personList.add(person);}abstarctDispatcher.run(currentFloor,person.status);}@Overrideprotected void notifyPersonLiftHere() {LinkedHashSet<Person> people = this.personList;if (people!=null && people.size()>0){for (Person person1 : people) {if (person1.status.NAME.equals(person1.abstarctDispatcher.abstractEvevator.status.NAME)) {person1.elevatorIsHere(currentFloor);}}}}}

抽象状态类

package cn.xx.evevator;import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;public abstract class  Status{public final String NAME;protected Status(String name) {NAME = name;}abstract void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors);public abstract void press(Floor floor);}

上升状态

package cn.xx.evevator;public class UpStatus extends Status{public UpStatus(String name) {super(name);}@Overridevoid handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {for (Integer floor : floors) {Floor floor1 = abstarctDispatcher.getFloor(floor);press(floor1);abstarctDispatcher.changeTargetMax(floor);floor1.isOpen = true;}}@Overridepublic void press(Floor floor) {floor.upKey = true;}}

下降状态

package cn.xx.evevator;public class DownStatus extends Status{public DownStatus(String name) {super(name);}@Overridevoid handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {for (Integer floor : floors) {Floor floor1 = abstarctDispatcher.getFloor(floor);press(floor1);abstarctDispatcher.changeTargetMax(floor);floor1.isOpen = true;}}@Overridepublic void press(Floor floor) {floor.downKey = true;}
}

停止状态

package cn.xx.evevator;
public class StopStatus extends Status{public StopStatus(String name) {super(name);}@Overridevoid handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {}@Overridepublic void press(Floor floor) {floor.upKey = false;floor.downKey = false;}}

人员类

package cn.xx.evevator;import org.springframework.util.StringUtils;public class Person {protected final Floor floor;protected final AbstarctDispatcher abstarctDispatcher;protected final Integer[] expectFloor;protected final String name;protected final Status status;private Person(AbstarctDispatcher abstarctDispatcher, Integer[] expectFloor, String name,Floor floor,Status status) {this.abstarctDispatcher = abstarctDispatcher;this.expectFloor = expectFloor;this.name = name;this.floor = floor;this.status = status;if (abstarctDispatcher.abstractEvevator.targetFloorMax < floor.currentFloor){abstarctDispatcher.abstractEvevator.targetFloorMax = floor.currentFloor;}}public void press(Integer[] floor){abstarctDispatcher.pressFloor(floor);}public void elevatorIsHere(int floor){System.out.print(floor+"层到了,"+this.name+"请进电梯!期望去:");for (int i = 0; i < expectFloor.length; i++) {if(i==expectFloor.length-1){System.out.println(expectFloor[i]+"层 ");}else{System.out.print(expectFloor[i]+"层和");}}press(expectFloor);try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}public static class Builder{private AbstarctDispatcher abstarctDispatcher;private Integer[] expectFloor;private String name;private Floor floor;private Status status;public Builder(AbstarctDispatcher abstarctDispatcher,Status status){this.abstarctDispatcher = abstarctDispatcher;this.status = status;}public Builder expectFloor(Integer[] expectFloor){this.expectFloor = expectFloor;return this;}public Builder name(String name){if (StringUtils.isEmpty(name)){this.name = "";}else{this.name = name;}return this;}public Builder floor(Floor floor){this.floor = floor;return this;}public Person build(){return new Person(abstarctDispatcher,expectFloor,name,floor,status);}}}

状态common

package cn.xx.evevator;public class StatusCommon {public static final UpStatus upstatus = new UpStatus("上升");public static final DownStatus downStatus = new DownStatus("下降");public static final StopStatus stopStatus = new StopStatus("停止");}

Context类

public class Context {private static final AbstarctDispatcher abstarctDispatcher;private static final AbstractEvevator abstractEvevator;static {abstarctDispatcher = new Dispatcher();abstractEvevator = new Evevator(abstarctDispatcher);abstarctDispatcher.setAbstractEvevator(abstractEvevator);}public static AbstarctDispatcher getDispatcher() {return abstarctDispatcher;}public static AbstractEvevator getEvevator() {return abstractEvevator;}
}

三、测试

public class Test {public static void main(String[] args) throws InterruptedException {AbstarctDispatcher dispatcher = Context.getDispatcher();// 张三在11层按了上行键 进去电梯后按6楼和8楼FloorFactory instance = FloorFactory.getInstance();Floor floor8 = instance.getFloor(8);Floor floor10 = instance.getFloor(10);Floor floor3 = instance.getFloor(3);new Thread(()->{
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }System.out.println("==============张三在8层按了下行键 进去电梯后按3楼和1楼==============");Person person = new Person.Builder(dispatcher,StatusCommon.downStatus).name("张三").expectFloor(new Integer[]{3, 2}).floor(floor8).build();floor8.pressFrom(person);}).start();new Thread(()->{try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("==============李四在10层按了下行键 进去电梯后按5层==============");// 李四在8层按了上行键 进去电梯后按10楼和11楼Person person1 = new Person.Builder(dispatcher,StatusCommon.downStatus).name("李四").expectFloor(new Integer[]{5}).floor(floor10).build();floor10.pressFrom(person1);}).start();new Thread(()->{System.out.println("==============王五在3层按了上行键 进去电梯后按4楼==============");// 王五在3层按了上行键 进去电梯后按9楼和11楼Person person3 = new Person.Builder(dispatcher,StatusCommon.upstatus).name("王五").expectFloor(new Integer[]{11}).floor(floor3).build();floor3.pressFrom(person3);}).start();}
}

运行结果:

在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/725118.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

K倍区间 刷题笔记

法一 前缀和暴力搜索 &#xff08;数据大会超时&#xff09; #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int N100010; int a[N],s[N]; int n,k; int main(){ cin>>n>>…

RISC-V架构学习资料整理

1、韦东山——D1S哪吒开发板的裸机代码仓库 https://github.com/bigmagic123/d1-nezha-baremeta 2、melis系统移植到D1S https://blog.51cto.com/u_13800193/6268813 3、韦东山的gitee仓库 https://gitee.com/weidongshan 4、D1S编译工具链下载 https://github.com/Tina-Linux/…

LabVIEW管道缺陷智能检测系统

LabVIEW管道缺陷智能检测系统 管道作为一种重要的输送手段&#xff0c;其安全运行状态对生产生活至关重要。然而&#xff0c;随着时间的推移和环境的影响&#xff0c;管道可能会出现老化、锈蚀、裂缝等多种缺陷&#xff0c;这些缺陷若不及时发现和处理&#xff0c;将严重威胁到…

ProxySQL实现mysql8主从同步读写分离

ProxySQL基本介绍 ProxySQL是 MySQL 的高性能、高可用性、协议感知代理。以下为结合主从复制对ProxySQL读写分离、黑白名单、路由规则等做些基本测试。 先简单介绍下ProxySQL及其功能和配置&#xff0c;主要包括&#xff1a; 最基本的读/写分离&#xff0c;且方式有多种&…

Java递归生成本地文件目录树形结构

Java递归生成本地文件目录(树行结构) 1.读取txt文件保存的文件目录结构 2.递归生成本地文件目录树形结构&#xff0c;并修改目录文件前缀进行递增 3.结果截图 4.代码 package com.zfi.server.device;import io.swagger.annotations.Api; import org.springframework.web.bind…

Postman接口测试—配置token为全局变量,配置测试环境

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 为什么要进行接口测试 因为不同端&#xff08;前段&#xff0c;后端&#xff09;的工作进度不一…

仿牛客网项目---关注模块的实现

本篇文章是关于我的项目的关注模块的开发。 关注模块的开发实现了用户之间的关注功能和关注列表的展示。通过使用相应的服务类处理关注和取消关注操作&#xff0c;并利用用户服务类获取用户信息&#xff0c;实现了关注功能的存储和查询。同时&#xff0c;通过触发关注事件&…

【软考】设计模式之访问者模式

目录 1. 说明2. 应用场景3. 结构图4. 构成5. java示例5.1 喂动物5.1.1 抽象访问者5.1.2 具体访问者5.1.3 抽象元素5.1.4 具体元素5.1.5 对象结构5.1.6 客户端类5.1.7 结果示例 5.2 超市销售系统5.2.1 业务场景5.2.2 业务需求类图5.2.3 抽象访问者5.2.4 具体访问者5.2.5 抽象元素…

前端面试拼图-原理源码

摘要&#xff1a;最近&#xff0c;看了下慕课2周刷完n道面试题&#xff0c;记录下... 1. JS内存泄漏如何检测&#xff1f;场景有哪些? 1.1 垃圾回收 GC 垃圾回收是一种自动管理内存的机制&#xff0c;它负责在运行时跟踪内存的分配和使用情况&#xff0c;并在不再需要的对象…

理解CAE

用于自监督表示学习的上下文自动编码器 摘要 我们提出了一种新的掩模图像建模(MIM)方法&#xff0c;上下文自编码器(CAE)&#xff0c;用于自监督表示预训练。我们通过在编码的表示空间中进行预测来预训练编码器。预训练任务包括两个任务:掩模表示预测—预测掩模块的表示&…

专业145+总分410+西工大西北工业大学827信号与系统考研经验电子信息与通信工程,海航,真题,大纲,参考书。

经过一年的努力&#xff0c;分数终于出来。今年专业课827信号与系统145&#xff08;很遗憾差了一点点满分&#xff0c;没有达到Jenny老师的最高要求&#xff09;&#xff0c;数一130&#xff0c;英语和政治也都比较平衡&#xff0c;总分410分&#xff0c;当然和信息通信考研Jen…

C及C++每日练习(2)

1.选择&#xff1a; 1.使用printf函数打印一个double类型的数据&#xff0c;要求&#xff1a;输出为10进制&#xff0c;输出左对齐30个字符&#xff0c;4位精度。以下哪个选项是正确的&#xff1f; A.%-30.4e B.%4.30e C.%-30.4f D.%-4.30 在上一篇文章中&#xff0c;提到了…

变老特效哪个app可以拍?深入探索变老效果的应用

随着科技的进步和智能手机的普及&#xff0c;各种摄影应用如雨后春笋般涌现&#xff0c;为我们提供了前所未有的创意拍摄体验。其中&#xff0c;变老特效因其独特的魅力&#xff0c;吸引了众多用户的关注。这种特效能够让我们在短时间内看到自己老去的模样&#xff0c;既有趣又…

JavaWeb HTTP 请求头、请求体、响应头、响应体、响应状态码

J2EE&#xff08;Java 2 Platform Enterprise Edition&#xff09;是指“Java 2企业版”&#xff0c;B/S模式开发Web应用就是J2EE最核心的功能。 Web是全球广域网&#xff0c;也称为万维网(www)&#xff0c;能够通过浏览器访问的网站。 在日常的生活中&#xff0c;经常会使用…

强大的ps 命令 -o 自定义输出内容选项

强大的ps 命令 -o 自定义输出内容选项 1、ps命令介绍和作用2、问题描述 1、ps命令介绍和作用 ps 是一个 Unix 和类 Unix 操作系统中常用的命令&#xff0c;用于显示当前运行的进程信息。ps 命令的作用包括&#xff1a; 查看进程信息&#xff1a; ps 命令可以列出当前系统中正…

数据结构之顺序表及其实现!

目录 ​编辑 1. 顺序表的概念及结构 2. 接口的实现 2.1 顺序表的初始化 2.2 检查顺序表容量是否已满 2.3 顺序表的尾插 ​编辑 2.4 顺序表的尾删 2.5 顺序表的头插 2.6 顺序表的头删 2.7 顺序表在pos位置插入 2.8 顺序表在pos位置删除 2.9 顺序表的查找 2.10 顺…

考研数学——高数:多元函数微分法及其应用

因为复习阶段全篇很细节的写下来一来比较费时间&#xff0c;二容易导致为了记笔记而记。 接下来的内容只会保留上课中比较有意义的地方&#xff0c;以及有自己助于理解的想法 全微分 助记&#xff1a; 证明是否可微&#xff0c;首先判断两个偏导数是否存在&#xff0c;不存在则…

中文版国产Figma简单好上手

在过去的两年里&#xff0c;国内外协同办公室发展迅速。一方面&#xff0c;它是由突如其来的疫情推动的&#xff0c;另一方面&#xff0c;它是科学技术不断进步的必然结果。在市场的推动下&#xff0c;市场上出现了越来越多的协同办公软件&#xff0c;使工作场所的工作更加高效…

插入排序和归并排序

插入排序&#xff0c;Insertion Sort. 给出伪代码 for i 1,2,...,n-1Insert A[i] into Sorted array A[0:i-1]by swaping down to the correct position. 冒泡排序 冒泡排序就是一种插入排序算法。 i ← 1 while i < length(A)j ← iwhile j > 0 and A[j-1] > A…

CorelDRAW2024专业级图形设计矢量图形设计软件

CorelDRAW2024是一款功能强大的矢量图形设计软件&#xff0c;适用于专业级图形设计作品的设计师和创作者。它提供了智能对象、布局、插图和模板等功能&#xff0c;可以帮助用户快速创建高质量的设计作品。 这款软件的用户界面直观且易于使用&#xff0c;允许用户快速访问和管理…