面试:你懂什么是分布式系统吗?Redis分布式锁都不会?>>>
java与模式读书笔记
//懒汉式单例模式
class Soup1
{
private Soup1()
{
}
// (1) Allow creation via static method:
public static Soup1 makeSoup()
{
return new Soup1();
}
}
//饿汉式单例模式,这种设计模式更适合java
class Soup2
{
private Soup2()
{
}
// (2) Create a static object and return a reference
// upon request.(The "Singleton" pattern):
private static Soup2 ps1 = new Soup2();
public static Soup2 access()
{
return ps1;
}
public void f()
{
}
}
// Only one public class allowed per file:
public class Lunch
{
void testPrivate()
{
// Can't do this! Private constructor:
// ! Soup1 soup = new Soup1();
}
void testStatic()
{
Soup1 soup = Soup1.makeSoup();
}
void testSingleton()
{
Soup2.access().f();
}
} // /:~