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 @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.