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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

談模式(Singleton Pattern)的變形

2019-11-18 13:09:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

Title: THE SINGLETON PATTERN


   *
   *

Description: 建議使用這個(gè)方法
   * 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) {
   }
  }
  
  
  
  只要稍加修改,我們可控制只創(chuàng)建N個(gè)實(shí)例,N由我們來(lái)定.
  下面給出代碼示范
  
   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; //最多只能創(chuàng)建5個(gè)實(shí)例
   static int instanceCount = 0; //開(kāi)始無(wú)實(shí)例
  
   private N_Instance() {}
  
   static public N_Instance getInstance() {
    if (instanceCount < MAXINSTANCE) {
     instanceCount++;
     return new N_Instance(); //返回一個(gè)實(shí)例
    }
    else
     return null; //返回空
   }
  
   public void finalize() {
    instanceCount--;
   }
  }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连云港市| 仙居县| 重庆市| 潞城市| 潮州市| 崇义县| 浮梁县| 天气| 同江市| 天峻县| 于都县| 新闻| 磐安县| 德惠市| 光山县| 静宁县| 阳城县| 扎赉特旗| 新闻| 濮阳县| 舞阳县| 汤原县| 和林格尔县| 肥东县| 伽师县| 乌鲁木齐市| 扶余县| 峨眉山市| 衡阳市| 伊通| 胶州市| 星子县| 夏津县| 巨野县| 蒲江县| 安化县| 青海省| 杨浦区| 唐河县| 无棣县| 建德市|