国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

談模式(SingletonPattern)的變形

2019-11-18 11:18:49
字體:
來源:轉載
供稿:網友

  下面是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--;
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 纳雍县| 扎囊县| 留坝县| 剑河县| 阿克苏市| 响水县| 商水县| 突泉县| 张掖市| 天津市| 望奎县| 乌兰浩特市| 通道| 宿迁市| 绍兴县| 贵州省| 兴海县| 平武县| 永城市| 凌源市| 屏东市| 太康县| 乐业县| 青海省| 五莲县| 荔波县| 泊头市| 建德市| 磴口县| 托克托县| 盐山县| 云龙县| 陆丰市| 焦作市| 禹州市| 伽师县| 双城市| 泉州市| 工布江达县| 独山县| 平山县|