以前曾与C#合作过,我考虑过
Calendar cal = Calendar.getInstance();根据GoF 单例模式(Wikipedia)和我想知道如何创建两个日历,因为Date是不推荐的。
从文件中
获取使用默认时区和区域设置的日历。
过载
getInstance(TimeZone zone)
getInstance(Locale aLocale)在我看来,这似乎是单例模式的一个推广,它在每个时区和地区创建一个单例。但我想要两个日历在同一时区。
但是,当我做测试的时候
@Test
public void test()
{
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
assertTrue(cal == cal2);
}它失败了,告诉我这个getInstance()方法实际上不是单例模式getInstance()方法,而是其他的方法。
那么,getInstance() 在一般情况下是否根据中的单例模式来表示单例呢?如果是,那么在文档中有哪些关键的词语来找出它是或者不是单例?如果不是,如何在Java中识别单例模式?还是Calendar是唯一的例外?
每次遇到getInstance()调用时,我都不想实现单元测试。
我读过这个答案是“在Java中实现单例模式的有效方法是什么?”,它的工作方式与C#完全一样,这与Calendar实现相矛盾。
发布于 2015-06-15 10:13:28
Calendar不是单例,每次对Calendar.getInstance(...)的调用都返回不同的实例。Javadoc没有说每个调用都将返回相同的实例,因此您没有理由假设它会返回。
Calendar.getInstance(...)更适合工厂的设计模式。
查看getInstance的其他示例,例如Locale.getInstance(),您可以看到Javadoc告诉您是否连续调用可能返回相同的实例:
/**
* Returns a <code>Locale</code> constructed from the given
* <code>language</code>, <code>country</code> and
* <code>variant</code>. If the same <code>Locale</code> instance
* is available in the cache, then that instance is
* returned. Otherwise, a new <code>Locale</code> instance is
* created and cached.
*
* @param language lowercase two-letter ISO-639 code.
* @param country uppercase two-letter ISO-3166 code.
* @param variant vendor and browser specific code. See class description.
* @return the <code>Locale</code> instance requested
* @exception NullPointerException if any argument is null.
*/
static Locale getInstance(String language, String country, String variant)同样,这不是单例,但正如Javadoc所说,实例是缓存的。除非Javadoc这么说,否则每次对getInstance的调用都会返回不同的实例。
发布于 2015-06-15 10:14:32
方法getInstance()通常在java中表示单个实例,但它不是刻在岩石中的规则。要确定它是单例的唯一方法,您必须要么查看文档(如果有的话),要么查看更好的实现。
发布于 2015-06-15 10:10:27
不,如果您查看源代码,您总是会得到一个new Calendar,方法getInstace()只是确定您将得到哪种类型的Calendar:
public static Calendar More ...getInstance(TimeZone zone, Locale aLocale)
{
return createCalendar(zone, aLocale);
}和createCalendar
private static Calendar More ...createCalendar(TimeZone zone, Locale aLocale)
{
// If the specified locale is a Thai locale, returns a BuddhistCalendar instance.
if ("th".equals(aLocale.getLanguage())
&& ("TH".equals(aLocale.getCountry()))) {
return new sun.util.BuddhistCalendar(zone, aLocale); // new Budhist
} else if ("JP".equals(aLocale.getVariant())
&& "JP".equals(aLocale.getCountry())
&& "ja".equals(aLocale.getLanguage())) {
return new JapaneseImperialCalendar(zone, aLocale); // new Japanese
}
// else create the default calendar
return new GregorianCalendar(zone, aLocale); // new Gregorian
}如何在java中确定单键?
Singletton总是返回相同对象的实例,有不同的方法,但通常情况下
getInstance方法,它检查属性是否为null来创建它,并且总是相同对象的实例.类似于:
public class Singleton {
// instance attribute
private static Singleton INSTANCE = null;
// private constructor
private Singleton() {}
// public get that checks instance and return it
public static Singleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
}https://stackoverflow.com/questions/30842413
复制相似问题