java 模拟简单打印机功能_java 单例模式模拟打印机打印任务
1.打印機報錯處理類
package com.pattern06.singleton.sample03;
public class PrintSpoolerException extends Exception {
public PrintSpoolerException() {
super();
}
public PrintSpoolerException(String message, Throwable cause) {
super(message, cause);
}
public PrintSpoolerException(String message) {
super(message);
}
public PrintSpoolerException(Throwable cause) {
super(cause);
}
}
2.打印機類
package com.pattern06.singleton.sample03;
public class PrintSpoolerSingleton {
private static PrintSpoolerSingleton instance = null;
private PrintSpoolerSingleton() {
}
public static PrintSpoolerSingleton getInstance() throws PrintSpoolerException {
if(instance == null) {
System.out.println("創建打印池......");
instance = new PrintSpoolerSingleton();
} else {
throw new PrintSpoolerException("打印出正在工作中......");
}
return instance;
}
public static void stop() {
instance = null;
}
public void manageJobs() {
System.out.println("管理打印池任務!");
}
}
3.測試類
package com.pattern06.singleton.sample03;
public class Client {
public static void main(String[] args) throws PrintSpoolerException {
PrintSpoolerSingleton p1,p2,p3;
//創建打印池
p1 = PrintSpoolerSingleton.getInstance();
//使用打印池
p1.manageJobs();
//停止打印池
PrintSpoolerSingleton.stop();
p2 = PrintSpoolerSingleton.getInstance();
p2.manageJobs();
//沒有把打印池停止就直接獲取打印池會報錯
p3 =PrintSpoolerSingleton.getInstance();
}
}
總結
以上是生活随笔為你收集整理的java 模拟简单打印机功能_java 单例模式模拟打印机打印任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux系列】Linux查看日志常用
- 下一篇: Excel表格制作教程-合并相同项,并将