- 浏览: 748052 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
Another notable difference is that static member classes cannot implement an interface
,
unless that interface is simply a marker. So if the class has to
realize a contract expressed by an interface, it really has to be a
singleton.
以上摘自:http://en.wikipedia.org/wiki/Singleton_pattern
他说静态类实例和singleton的区别是静态成员类不能实现接口,感觉不对啊,可以实现吧,他说除非是个marker,那意思是去实现的话就不能是个静态成员类了?就得是个单例了?单例就允许实现接口,这算什么定义呢...
貌似如此也无不可
interface Foo { void foo(); } class Outer { static class Inner implements Foo { public void foo() { } } }
维基一般不会错吧,那到底哪里有问题呢?
关于这个,仔细看了很久,焦点貌似应该集中在下面这段:
Note the distinction between a simple static instance of a class
and a singleton: although a singleton can be implemented as a static
instance, it can also be lazily constructed, requiring no memory or
resources until needed. Another notable difference is that static member
classes cannot implement an interface, unless that interface is simply a
marker. So if the class has to realize a contract expressed by an
interface, it really has to be a singleton.
static member classes:
如果仅从字面理解,我觉得应是静态成员类没错,虽然前面比的是static instance of a class,两者不同,但这里主要比较的是singleton
接下来的表述就非常迷惑了,或者干脆可以断定为不完整,比如
static member classes cannot implement an interface
如果非要实现怎么办呢?后面也没说接口必须按照什么规范
interface Foo { void hello(); } class Outer { static class Inner implements Foo { public void hello() { System.out.println("hello"); } } }
接口的确是实现了,但它就必须得是singleton吗?明显不是啊,或者static member classes理解有误,不过感觉也不太像
那假如把static member classes理解成静态类(classes which have only static members),貌似像说得通
interface Foo { void hello(); } class Outer implements Foo { public static void go() { } @Override public void hello() { } public static void main(String[] args) { Outer.go(); } }
像这样如果实现了接口,就出现了非静态的方法了,除非是个marker,否则无以为静态类。因此在两者的比较环境中非此即彼,能去实现非marker接口的就算是singleton了,不知这种逻辑算勉强是对这个的解释否
from: http://topic.csdn.net/u/20110221/21/01c7c6a7-295d-4c3e-bfb4-aa3ece3b7275.html
something also has proved it.
There are a few ways to implement Singletons. Although you can get Singleton-like
behavior with static fields and methods [for example,
java.lang.Math.sin(double)
], you gain more flexibility by creating an
instance. With Singletons implemented as single instances instead of static class
members, you can initialize the Singleton lazily, creating it only when it is first
used. Likewise, with a Singleton implemented as single instance, you leave open the
possibility of altering the class to create more instances in the future. With some
implementations of the Singleton, you allow writers of subclasses to override methods
polymorphically, something not possible with static methods.
from: http://java.sun.com/developer/technicalArticles/Programming/singletons/
发表评论
-
NullPointerException in ternary operator
2013-01-19 00:01 1126java代码: Integer i = null; ... -
why concrete class must implement abstract method while abstract class need not
2011-05-11 13:27 1517yes, just as the title...why co ... -
Enum singleton - Elvis Stealer
2011-04-26 14:41 1636import java.io.ByteArrayInputSt ... -
why can inner class be instantiated in this way?
2011-04-06 10:24 1021public class Outer { pub ... -
into native source code
2011-03-23 23:09 966once there was a problem occurr ... -
about Thread.join method
2011-03-23 08:09 1276import java.util.Timer; import ... -
why is static inner class singleton thread safe?
2011-03-15 09:03 1457http://en.wikipedia.org/wiki/In ... -
why string concatenation yields no interned one
2011-03-14 16:48 1114public class Test { public st ... -
why can bit mask suppress sign extention
2011-03-10 09:16 1181yes, bit mask can suppress sign ... -
About gc two objects which are inter referenced to each other
2011-03-08 11:00 1053my workmate told me such a sena ... -
circumvents exception checking
2011-03-03 11:02 969import java.io.IOException; ... -
why List hasn't clone method but the ArrayList has
2011-02-16 15:27 1173List list = new ArrayList(); ... -
deep copy
2011-02-16 15:20 1041static public Object deepCopy(O ... -
Java is Pass-by-Value
2011-02-14 13:28 1096This often heard saying is not ... -
不是说字符串不可变吗
2011-02-14 11:28 898from csdn: http://topic.csdn.ne ... -
没有实现抽象方法的具体类居然编译通过了
2011-02-04 22:29 976来自csdn: http://topic.csdn.net/ ... -
关于iterator的fail-fast
2011-01-20 11:15 997是使用集合的iterator后,再改变就可能抛出这个异常 ... -
生成几个String的问题
2011-01-11 16:26 1013public class Test extends java. ... -
无符号右移>>>
2011-01-11 15:46 2344public class Test { public st ... -
Struts2之log信息不出的问题
2010-12-30 11:18 3024刚开始学习Struts2的时候,用的是maven方式,但是却不 ...
相关推荐
singleton pattern 的定义 主要应用方法 优缺点 通过代码 具体分析解释
单例模式(Singleton Pattern)是一种常用的软件设计模式,在系统中确保某个类只有一个实例,并提供一个全局访问点。这种模式通常用于控制资源的消耗,比如数据库连接、线程池等,以及确保某个系统配置的一致性。 #...
单例模式(Singleton Pattern)是软件设计模式中的一种,它保证一个类只有一个实例,并提供一个全局访问点。这种模式在很多场景下非常有用,比如控制资源的唯一性、全局配置对象或者缓存服务等。本篇文章将深入探讨...
### 单例模式 Singleton Pattern #### 概述 单例模式是一种常见的设计模式,属于创建型模式之一。这种模式的核心在于确保某个类只有一个实例存在,并且提供一个全局访问点来获取该实例。单例模式在Java开发中尤其...
在Go语言中,实现单例模式(Singleton Pattern)通常涉及确保一个类只有一个实例,并提供一个全局访问点来获取该实例。由于Go语言没有传统的类和对象概念,但具有结构体(struct)和函数,我们可以通过使用包级变量...
Java据说有23种设计模式,Android的设计模式肯定是由Java来引申出来的。这里不讨论有多少人全会,有多少种设计模式会使用到,我们来讲下其中用得最多的也就是人人都知道的...这里是一个简单的SingletonPatternDemo。
Singleton Pattern单例模式是一种创建型设计模式,提供了一种创建对象的最佳方式。该模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建在应用程序运行期间,单例模式只会在全局作用域下...
在`singleton-pattern-master`这个压缩包中,可能包含了一个示例项目或者一个库,它演示了如何在Laravel中实现和使用单例模式。可能的目录结构包括源代码文件、配置文件、示例测试等,帮助开发者更好地理解和应用...
**单例模式(Singleton Pattern)**是软件设计模式中的一种基础模式,它的核心思想是确保一个类只有一个实例,并提供一个全局访问点。这种模式在很多场景下非常有用,比如配置管理、线程池、数据库连接池等,这些都...
单件模式(Singleton pattern)是设计模式中的一种结构型模式,它的主要目的是确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于系统中需要频繁创建和销毁的对象,如日志服务、线程池或者数据库连接等...
单例模式(Singleton Pattern)是一种创建型设计模式,确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于需要全局共享资源的场景,比如配置管理、日志记录等。 单例模式的组成 私有构造函数:防止外部...
单例模式是软件设计模式中的一种,属于创建型模式,它的主要目的是确保一个类只有一个实例,并提供一个全局访问点。这种模式在很多场景下都非常有用,例如管理共享资源、配置对象或者缓存服务等。 单例模式的核心...
### C++中实现Singleton模式的关键知识点 #### 一、Singleton模式简介 Singleton模式是一种常用的软件设计模式,旨在确保一个类只有一个实例,并提供一个全局访问点。这种模式在系统中经常被用于控制对共享资源...
这段代码中,`Singleton`类的构造函数是私有的,防止了外部直接创建实例。`getInstance()`方法是一个静态成员函数,它在第一次调用时创建Singleton的实例(如果尚未创建),并在后续调用中返回相同的实例。`delete`...
C#单例模式(Singleton Pattern)是一种常用的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。下面我们将详细介绍C#单例模式的定义、实现和优化。 单例模式的定义: 单例模式的主要目的是确保一个...
单例模式是软件设计中的一种常见模式,它的主要目的是确保一个类只有一个实例,并提供全局访问点。这样的设计在很多场景下都非常有用,比如配置管理、线程池、缓存服务等,因为这些服务通常只需要一个实例来协调整个...
在Qml中,我们可以通过Qt的Singleton组件来实现这一模式。 首先,让我们理解单例模式的基本概念。在软件工程中,单例模式保证一个类只有一个实例,并提供一个全局访问点。这个设计模式在许多场景下都很实用,比如...
在Java编程中,单例模式是一种常用的创建型设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。本主题探讨的是线程安全的单例模式实现,特别是饥饿模式(Starvation)和延迟加载(Lazy Initialization)...
Singleton Pattern 的意图是:保证一个类仅有一个实例,并提供一个访问它的全局访问点。生活中的例子是,例如一个公司的 CEO,一个国家的总统,这些职位在系统中只能存在一个实例。 Singleton Pattern 的适用性有...
单例模式(Singleton Pattern)是一种常用的软件设计模式,它的核心思想是确保一个类在整个应用程序中只有一个实例存在,并提供一个全局访问点来获取这个实例。这种模式在很多场景下非常有用,比如管理系统资源、...