网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
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,移动开发
本月文章推荐
.使用JWSDP完成Web Service在java.
.用 WebSphere Studio 创建 JSF 提.
.在 WAS 中使用 Java 安全套接字扩.
.Spring AOP在DWR安全上的应.
.Cell插件在J2EE系统中的应用.
.实例说明Java中的方法重载机制.
.在Hibernate中实现复杂的数据映射.
.Java中合并XML文档的设计与实现.
.新手上路:Tomcat5.5.9的安装配置.
.Java技巧21使用档案文件提高appl.
.颠覆Java开发的定律:无需创建对.
.Java编程:配置Tomcat5数据源心得.
.用Java增加一个XSLT功能.
.Java:IT认证界的新贵.
.漫谈Java平台上的CRM系统.
.Java Socket编程(四)重复和并发.
.Java项目中使用Hibernate处理数据.
.Java页面计数器.
.使用Socket发送电子邮件.
.java中文问题-浅析.

谈模式(Singleton Pattern)的变形

发表日期: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--;
   }
  }
上一篇:editplus能够编译java嘛? 如何设置 人气:952
下一篇:查询页面显示进度条,load成功后隐去进度条 人气:1647
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐