谈模式的覆盖
下面是Singleton Pattern的原意
package mo1;
/**
* 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 ang
* @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 mo2;
/**
* Title: Singleton Pattern 的变形
*
* Description:
*
* Copyright: Copyright (c) 2005
*
* Company:
*
* @author ang
而乐乐的食道管上方最狭窄的地方只有3毫米。经过专家组仔细研究 * @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--;
}
} 查看本文来源
成都治白癜风去哪里石家庄白癜风医院电话
西宁男科哪家医院好
- 下一页:谈对话框的动画弹出和动画消隐计划
- 上一页:谈流行木马灰鸽子的清除方法牛
- 06月21日武侠外界因素也会影响到大白熊犬的成长位置
- 06月21日武侠外出期间怎样带着杜宾犬上火车呢求方法位置
- 06月21日武侠给狗狗喂药的正确方法位置
- 06月21日武侠多吃零食弊端多给拉布拉多犬吃零食的五项注位置
- 06月21日武侠多久清理一次猫砂比较合适位置
- 06月21日武侠夏季银狐犬饲养问题位置
- 06月21日武侠夏季西施犬饲养问题位置
- 06月20日武侠猫很怕洗澡下雨和水的原因位置
- 06月20日武侠猫家长会犯的十大错误位置
- 06月20日武侠猫咪饲养必备小常识位置
- 06月20日武侠猫怎么照顾搬家的猫咪需要怎么照顾位置
- 06月20日武侠猫咪讨厌的三件事位置