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{
@Override
protected void pressFloor(Integer[] floors) {
if (floors!=null && floors.length>0){
this.abstractEvevator.status.handler(this,floors);
}
}
@Override
protected Floor getFloor(Integer floor) {
return this.abstractEvevator.instance.getFloor(floor);
}
@Override
public synchronized void run(int floor, Status status) {
abstractEvevator.run(floor,status);
}
@Override
public 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);
}
@Override
void 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();
}
}
}
@Override
void 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++;
}
}
}
@Override
void 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--;
}
}
}
@Override
void 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);
}
@Override
protected 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);
}
@Override
protected 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);
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
for (Integer floor : floors) {
Floor floor1 = abstarctDispatcher.getFloor(floor);
press(floor1);
abstarctDispatcher.changeTargetMax(floor);
floor1.isOpen = true;
}
}
@Override
public void press(Floor floor) {
floor.upKey = true;
}
}
下降状态
package cn.xx.evevator;
public class DownStatus extends Status{
public DownStatus(String name) {
super(name);
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
for (Integer floor : floors) {
Floor floor1 = abstarctDispatcher.getFloor(floor);
press(floor1);
abstarctDispatcher.changeTargetMax(floor);
floor1.isOpen = true;
}
}
@Override
public void press(Floor floor) {
floor.downKey = true;
}
}
停止状态
package cn.xx.evevator;
public class StopStatus extends Status{
public StopStatus(String name) {
super(name);
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
}
@Override
public 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();
}
}
运行结果: