Java电梯模拟升级版

2024-03-07 01:28
文章标签 java 模拟 电梯 升级版

本文主要是介绍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();}
}

运行结果:

在这里插入图片描述

在这里插入图片描述

这篇关于Java电梯模拟升级版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/782014

相关文章

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

Spring核心思想之浅谈IoC容器与依赖倒置(DI)

《Spring核心思想之浅谈IoC容器与依赖倒置(DI)》文章介绍了Spring的IoC和DI机制,以及MyBatis的动态代理,通过注解和反射,Spring能够自动管理对象的创建和依赖注入,而MyB... 目录一、控制反转 IoC二、依赖倒置 DI1. 详细概念2. Spring 中 DI 的实现原理三、

SpringBoot 整合 Grizzly的过程

《SpringBoot整合Grizzly的过程》Grizzly是一个高性能的、异步的、非阻塞的HTTP服务器框架,它可以与SpringBoot一起提供比传统的Tomcat或Jet... 目录为什么选择 Grizzly?Spring Boot + Grizzly 整合的优势添加依赖自定义 Grizzly 作为