⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 singlespooler.java

📁 这个是java设计模式的文件 可能对了解设计思想有一定帮助
💻 JAVA
字号:
class Spooler 
{
   //this is a prototype for a printer-spooler class
   //such that only one instance can ever exist
   static boolean instance_flag=false; //true if 1 instance

   public Spooler() throws SingletonException
   {
      if (instance_flag) 
         throw new SingletonException("Only one printer allowed");    
      else
         instance_flag=true;     //set flag for 1 instance
      System.out.println("printer opened");
   }
   //-------------------------------------------
   public void finalize()
   {
      instance_flag = false;
   }
}                                                        
//===============================================
class SingletonException extends RuntimeException
{      
   //new exception type for singleton classes
   public SingletonException()
   {
      super();
   }
//-----------------------------------------------
   public SingletonException(String s)
   {
      super(s);
   }
}
//===============================================
public class singleSpooler
{
   static public void main(String argv[])
   {
      Spooler pr1, pr2;
      //open one printer--this should always work
      System.out.println("Opening one spooler");
      try{
      pr1 = new Spooler();
      }
      catch (SingletonException e)
      {System.out.println(e.getMessage());}
      //try to open another printer --should fail
      System.out.println("Opening two spoolers");
      try{
      pr2 = new Spooler();
      }
      catch (SingletonException e)
      {System.out.println(e.getMessage());}
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -