背景
题目:
假设有一个理发店只有一个理发师,一张理发时坐的椅子,若干张普通椅子顾客供等候时坐。没有顾客时,理发师睡觉。顾客一到,叫醒理发师
。如果理发师没有睡觉,而在为别人理发,他就会坐下来等候。如果所有的椅子都坐满了人,最后来的顾客就会离开。
这是一道笔试题,主要考察面试者的业务建模能力,在短时间内抽象主要业务模型,提炼出模型的属性和方法。然后利用消费者-生存者的模型思维串联业务流程,还有协调者模型的设计,使其能完成桥梁枢纽的功能,这些都是考察的对象。
方案
流程的步骤:
生产者:
- 顾客进入理发店,唤醒理发师;
- 理发椅是否使用,未使用直接入座;
- 理发椅在使用,则进入队列排队;
消费者:
- 理发师给理发椅上的顾客理发;
- 队列不为空,中拉取顾客,让顾客做到理发椅上。
这是大致的实现思路,下面是我根据描述画了一张业务流程图:
代码
理发师
它有状态属性,和理发的行为,每次理发需要消耗两秒。
package org.example;import lombok.Getter;
import lombok.SneakyThrows;@Getter
public class Barber {private BarberStatus status;@SneakyThrowspublic void hairCut(Customer customer) {System.out.println(String.format("理发师为 %s 理发", customer.getNum()));Thread.sleep(2 * 1000);}public Barber() {this.status = BarberStatus.SLEEP;}public void weekUp() {this.status = BarberStatus.WORKING;}
}
理发师包含睡觉、工作两种状态:
public enum BarberStatus {SLEEP, WORKING
}
理发椅
它作为协调者是资源共享对象,所以这里有加锁。理发椅在顾客坐下时绑定了顾客,提供完成理发后的资源释放行为。
package org.example;import lombok.Getter;@Getter
public class BarberChair {private Customer customer;private BarberChairStatus status;public BarberChair() {this.status = BarberChairStatus.FREE;}public synchronized void seated(Customer customer) {this.status = BarberChairStatus.SEATED;this.customer = customer;System.out.printf("用户 %s 坐下%n", customer.getNum());}public synchronized void free() {this.status = BarberChairStatus.FREE;System.out.printf("顾客 %s 理发结束%n", customer.getNum());}}
理发椅包含被坐还是空闲两种状态:
public enum BarberChairStatus {SEATED, FREE;
}
理发店
它作为一个聚合对象,是整个领域模型的入口,内聚了所有内部对象,所有业务操作都在这里向外暴露。
理发店的属性:理发师、等待的顾客、供顾客坐的椅子数、理发椅。
理发店拥有两个行为:接单、派单。
这里可以将理发店理解位理发店系统,系统完成顾客接单,放入队列中。然后异步地派单给理发师,从队列中获取并通知顾客去理发椅理发。
package org.example;import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;import java.util.concurrent.ArrayBlockingQueue;@Setter
@Getter
public class BarberStore {private Barber barber;private ArrayBlockingQueue<Customer> waitingCustomers;private int customerChairSize;private BarberChair barberChair;@SneakyThrowspublic void receiveOrder(Customer customer) {barber.weekUp();if (barberChair.getStatus() == BarberChairStatus.FREE) {barberChair.seated(customer);return;}if (waitingCustomers.size() == customerChairSize) {System.out.printf("用户 %s 离开%n", customer.getNum());return;}System.out.printf("用户 %s 等待%n", customer.getNum());waitingCustomers.add(customer);}public void dispatchOrder() {// 椅子的状态是SEATED才表明有人需要理发if (barberChair.getStatus() == BarberChairStatus.SEATED) {hairCutting(barberChair.getCustomer());return;}if (!waitingCustomers.isEmpty()) {Customer customer = waitingCustomers.poll();barberChair.seated(customer);hairCutting(customer);}}public BarberStore(int customerChairSize) {this.barber = new Barber();this.customerChairSize = customerChairSize;this.barberChair = new BarberChair();this.waitingCustomers = new ArrayBlockingQueue<>(customerChairSize);}@SneakyThrowsprivate void hairCutting(Customer customer) {barber.hairCut(customer);barberChair.free();}
}
测试客户端
模拟两个线程,一个生产者接单,一共接10个单,接单一次休息1s。一个消费者线程派单,一直不停地派单,理发椅有人则去理发,理发完成后让顾客坐到理发椅上,一直重复直到队列为空。
package org.example;import lombok.SneakyThrows;/*** Hello world!*/
public class App {public static void main(String[] args) {BarberStore barberStore = new BarberStore(3);// 椅子是顾客和理发师连接的纽带new Thread(() -> {for (int i = 0; i < 10; i++) {barberStore.receiveOrder(new Customer(i));mockPauseSecond(1);}}).start();new Thread(() -> {while (true) {barberStore.dispatchOrder();}}).start();}@SneakyThrowsprivate static void mockPauseSecond(int i) {Thread.sleep(i * 1000);}
}