网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.java.util下的Timer,TimerTask和.
.JavaInterface是常量存放的最佳地.
.Java中引用变量的使用.
.程序分享:从UDDI注册中心查询服.
.[JAVA100例]033、读写文件(字节).
.Java技巧:实例讲解Servlet的图象.
.非阻塞套接字(Nonblocking Socke.
.勿以恶小而为之:Java编程的恶魔.
.在ColdFusion和J2EE之间共享数据.
.为什么程序员不应调用“sun”包.
.Sun Java技术认证风靡全球.
.在 .NET 上安装 XBike以及操作方.
.description 属性.
.学习java&xml心得(2).
.为 Web 服务构建 Struts 应用程序.
.Together for Eclipse的使用.
.Java Socket编程(五) 简单的WE.
.英文打字练习.
.新一代网络模式Web 2.0火爆.
.经典:从追MM谈Java的23种设计模.

谈模式(SingletonPattern)的变形

发表日期:2008-1-5



  下面是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--;
   }
  }
上一篇:Java技巧:用Java实现回调例程 人气:574
下一篇:Borland新一代Java开发环境JBuilderX 人气:437
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐