DesignPattern : Singleton
6899 단어 JavaDesignPatternSingleton
1. Eager Initialization Approach
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = new Singleton();
private Singleton()
{
}
public static Singleton getInstance()
{
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest
{
@Test
public void test()
{
Singleton singleton1 = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
assertEquals(singleton1, singleton2);
}
}
2. Lazy Initialization Approach
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (null == singleton)
{
singleton = new Singleton();
}
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest
{
@Test
public void test()
{
Singleton singleton1 = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
assertEquals(singleton1, singleton2);
}
}
Comments:
1) Problems may occur when under the circumstance of multi-thread.
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (null == singleton)
{
try
{
System.out.println("Current Thread: " + Thread.currentThread());
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
singleton = new Singleton();
}
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
public class AppTest
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread().start();
}
}
class MyThread extends Thread
{
private Singleton singleton = null;
@Override
public void run()
{
singleton = Singleton.getInstance();
System.out.println(singleton);
}
}
Current Thread: Thread[Thread-0,5,main]
Current Thread: Thread[Thread-1,5,main]
edu.xmu.designPattern.DesingPattern_Singleton.Singleton@1b67f74
edu.xmu.designPattern.DesingPattern_Singleton.Singleton@69b332
Comments:
1) We can find out that there has been two different instance of Singleton created.
This violates Singleton.
2) We can use getInstance() to mark that we are using Singleton.
We can use newInstance() to mark that we are using Reflection to create a new instance.
3. Enum Approach
package edu.xmu.oop.singleton;
public enum Singleton {
SINGLETON("Davy", 24);
private String name;
private int age;
Singleton(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name: " + name + ", age: " + age;
}
}
package edu.xmu.oop.singleton;
import static org.junit.Assert.assertEquals;
import org.apache.log4j.Logger;
import org.junit.Test;
public class SingletonTest {
private static final Logger logger = Logger.getLogger(SingletonTest.class);
@Test
public void singletonTest() {
Singleton instance = Singleton.valueOf("SINGLETON");
logger.info(instance.toString());
assertEquals("name: Davy, age: 24", instance.toString());
instance = Singleton.SINGLETON;
logger.info(instance.toString());
assertEquals("name: Davy, age: 24", instance.toString());
instance.setName("Cal");
instance.setAge(22);
logger.info(instance.toString());
assertEquals("name: Cal, age: 22", instance.toString());
instance = Singleton.valueOf("SINGLETON");
logger.info(instance.toString());
assertEquals("name: Cal, age: 22", instance.toString());
}
}
Advantages:
1> Thread safe: Enum is thread safe and implementation of Singleton through Enum ensures that your singleton will have only one instance even in a multithread env.
2> Deserialization safe: When we serialize a class and deserialize it then it creates another instance of the singleton class. Basically as many times as you deserialize the singleton instance,
it will create multiple instance. In this case, the best way is to make the singleton as enum, in that way, the underlying Java implementation takes caare of all the details,
if this is not possible when we will need to override the readobject() method to return the same singleton instance.
Disadvantates:
1> Enum instance is defined at compile time, we cannot make any changes to the properties of this instance if the properties are final.
These properties are assigned with default value before we can even have access to that.
Conclusion:
A single-element enum type is the best way to implement a singleton.
Reference Links:
1) Effective Java 2nd Edition -Joshua Blosh
2) http://java.dzone.com/articles/singleton-design-pattern-%E2%80%93
3) http://stackoverflow.com/questions/18882612/is-it-possible-to-have-a-java-singleton-using-an-enum-that-uses-constructor-argu
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.