麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

談模式(Singleton Pattern)的變形

2019-11-18 13:09:47
字體:
來源:轉載
供稿:網友

  下面是Singleton Pattern的原意
  package Pattern.Creational.Singleton.Demo1;
  
  /**
   *

Title: THE SINGLETON PATTERN


   *
   *

Description: 建議使用這個方法
   * Another apPRoach, suggested by Design Patterns, is to create
   * Singletons using a static method to issue and keep track of instances. To
   * prevent instantiating the class more than once, we make the constrUCtor
   * private so an instance can only be created from within the static method
   * of the class.
   *
   * Other Consequences of the Singleton Pattern
   * 1. It can be difficult to subclass a Singleton, since this can only work
   * if the base Singleton class has not yet been instantiated.
   * 2. You can easily change a Singleton to allow a small number of instances
   * where this is allowable and meaningful.
   *
   *


   *
   *

Copyright: Copyright (c) 2005


   *
   *

Company:


   *
   * @author Lin.Xiang.Xiang
   * @version 1.0
   */
  public class IsSpooler
  {
  //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
  //the constructor is privatized-
  //but need not have any content
   private IsSpooler() {}
  
  //static Instance method returns one instance or null
   static public IsSpooler Instance() {
    if (!instance_flag) {
     instance_flag = true;
     return new IsSpooler(); //only callable from within
    }
    else
     return null; //return no further instances
   }
  
  //-------------------------------------------
   public void finalize() {
    instance_flag = false;
   }
  
   public static void main(String[] args) {
   }
  }
  
  
  
  只要稍加修改,我們可控制只創建N個實例,N由我們來定.
  下面給出代碼示范
  
   package Pattern.Creational.Singleton.Demo2;
  
  /**
   *

Title: Singleton Pattern 的變形


   *
   *

Description:


   *
   *

Copyright: Copyright (c) 2005


   *
   *

Company:


   *
   * @author Lin.Xiang.Xiang
   * @version 1.0
   */
  public class N_Instance {
   final static int MAXINSTANCE = 5; //最多只能創建5個實例
   static int instanceCount = 0; //開始無實例
  
   private N_Instance() {}
  
   static public N_Instance getInstance() {
    if (instanceCount < MAXINSTANCE) {
     instanceCount++;
     return new N_Instance(); //返回一個實例
    }
    else
     return null; //返回空
   }
  
   public void finalize() {
    instanceCount--;
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩黄在线观看 | 国产激爽大片在线播放 | 色播亚洲 | 依人九九宗合九九九 | 日本中文字幕网址 | 91精品国产91| 中文字幕综合在线观看 | 欧美成人精品欧美一级乱黄 | 中文字幕激情视频 | 欧洲精品久久 | 黑人一级片视频 | 国产精品久久久久无码av | 黄视频免费在线观看 | 99国产精成人午夜视频一区二区 | 国产精品久久久久久久不卡 | 久久久久久久久久久高潮一区二区 | 国产91一区二区三区 | 久久精品国产一区二区电影 | 久草最新在线 | 911网站大全在线观看 | 午夜视频久久 | 久久久久亚洲精品国产 | 二级大黄大片高清在线视频 | 欧美成人免费在线视频 | 蜜桃91麻豆 | 神马久久精品综合 | 国产精品99一区二区 | 色成人在线 | 九九热在线视频观看这里只有精品 | 蜜桃传免费看片www 一本色道精品久久一区二区三区 | xp123精品视频 | 鲁久久| 91,视频免费看 | 欧美日韩亚洲另类 | 午夜精品视频在线 | 久久久久久久一区 | 久久精品一区二区三区四区五区 | 毛片在线视频观看 | 国产在线欧美日韩 | 久久99久久98精品免观看软件 | 免费观看高清视频网站 |