面向对象的分析与设计
需求 : 有三种类型的客户:VIP客户,普通客户,快速客户,异步随机生成各种类型的客户,各种类型客户在其对应窗口按顺序依次办理业务。
号码管理器对象,让这个对象不断地产生号码,就等于随机生成了客户。
由于有三类客户,每类客户的号码编排都是完全独立的,所以,想到本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。
这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中只能有一个,所以,它要被设计成单例。
各类型客户在其对应的窗口依次办理业务,准确地说,应该是窗口依次叫号。
各个窗口怎么知道应该叫哪一个号呢?他一定是问相应的号码管理器,即服务窗口每次找号码管理器获取当前要被服务的号码。
各类关系如下
NumberMachine类
定义得到号码管理器对象的功能,因为号码生成器只有一个,使用单例设计模式。
NumberManager类
实现取号功能,将号码加入队列中;
如果队列中号码个数大于零,则取出队列中的第一个。因为是多线程,加synchronized。
ServiceWindow类
定义开始方法,定义一个进程池,根据不同的客户执行不同的服务代码
普通客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有,先休息一秒钟,再获取对象
快速客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有对象,获取普通客户对象,为普通客户服务,服务时间固定为一秒
VIP客户服务代码,获取对象,如果有对象,为其服务,结束后,再次调用方法,如果没有对象,获取普通客户对象,为普通客户服务
将程序中用到的常量在单独的类中定义,单独定义一个枚举类,存放客户类型,并复写toString()方法。
MainClass类:
按照题目要求,生成4个普通窗口,1个快速窗口,1个VIP窗口,调用窗口的start()方法,开始获取相应客户,获取相应客户后为之服务。
建立线程池,按照客户比例设置相应的定时器间隔来生成客户。
---------------ServiceWindow类---------------
packageBankDispatchSystem;
importjava.util.Random;
importjava.util.concurrent.Executors;
publicclass ServiceWindow {
private CustomerType type =CustomerType.COMMON;
private int windowId = 1;
public void setType(CustomerType type) {
this.type = type;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(newRunnable(){
public void run(){
while(true)
{
switch(type)
{
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName="第"+windowId+type+"窗口";/
//此处使用type,而不固定为普通,因为快速窗口及VIP窗口会调用该方法
Integer number =NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
System.out.println(windowName+"正在获取任务");
if(number!= null)
{
System.out.println(windowName+"为第"+number+"个普通客户服务");
long beginTime = System.currentTimeMillis();
int maxRand =Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serveTime = newRandom().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (Exception e) {
e.printStackTrace();
}
int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;
System.out.println(windowName+"为第"+number+"个普通客户完成服务,耗时"+costTime+"秒");
}
else{
System.out.println(windowName+"没有取到服务任务,先休息一秒钟嘛");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void expressService() {
String windowName="第"+windowId+"号"+type+"窗口";
Integer number =NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+"正在获取任务");
if(number!= null)
{
System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
long beginTime = System.currentTimeMillis();
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);
} catch (Exception e) {
e.printStackTrace();
}
int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;
System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime+"秒");
}
else{
System.out.println(windowName+"没有取到服务任务");
commonService();
}
}
private void vipService() {
String windowName="第"+windowId+"号"+type+"窗口";
Integer number = NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName +"正在获取任务");
if(number!= null)
{
System.out.println(windowName+"为第"+number+"个"+type+"客户服务");
long beginTime =System.currentTimeMillis();
int maxRand =Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serveTime = newRandom().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try {
Thread.sleep(serveTime);
} catch (Exception e) {
e.printStackTrace();
}
int costTime=(int)(System.currentTimeMillis()-beginTime)/1000;
System.out.println(windowName+"为第"+number+"个"+type+"客户完成服务,耗时"+costTime+"秒");
}
else{
System.out.println(windowName+"没有取到服务任务");
commonService();
}
}
}
--------------------MainClass_Bank--------------------
packageBankDispatchSystem;
importjava.util.concurrent.Executors;
importjava.util.concurrent.TimeUnit;
publicclass MainClass_Bank {
public static void main(String[] args) {
for(int i=1;i<5;i++)
{
ServiceWindow commonWindow = newServiceWindow();
commonWindow.setWindowId(i);
commonWindow.start();
}
ServiceWindow expressWindow = newServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
ServiceWindow vipWindow = newServiceWindow();
vipWindow.setType(CustomerType.VIP);
vipWindow.start();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){
public void run()
{
Integer number =NumberMachine.getInstance().getCommonManager().generateNewManager();
System.out.println(number+"号普通客户等待服务");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
//使用定时器产生普通客户,快速客户及VIP客户 Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){
public void run()
{
Integer number = NumberMachine.getInstance().getExpressManager().generateNewManager();
System.out.println(number+ "号快速客户等待服务");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME*2,
TimeUnit.SECONDS);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(newRunnable(){
public void run()
{
Integer number =NumberMachine.getInstance().getVipManager().generateNewManager();
System.out.println(number+ "号VIP客户等待服务");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME*6,
TimeUnit.SECONDS);
}
}