<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>빛을 담고 세상 넓히기 &#187; singleton</title>
	<atom:link href="http://fantazic.com/archives/tag/singleton/feed" rel="self" type="application/rss+xml" />
	<link>http://fantazic.com</link>
	<description>마음의 빛으로 넓은 세상을 비추고 싶다.</description>
	<lastBuildDate>Sun, 27 Nov 2011 23:52:11 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Singleton Pattern 구현</title>
		<link>http://fantazic.com/archives/92</link>
		<comments>http://fantazic.com/archives/92#comments</comments>
		<pubDate>Fri, 29 Jun 2007 08:07:35 +0000</pubDate>
		<dc:creator>따지크</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[패턴]]></category>

		<guid isPermaLink="false">http://fantazic.com/archives/92</guid>
		<description><![CDATA[
Tweet

최근 singleton 패턴 구현에 대한 질문을 받은 적이 있다. singleton 패턴은 인스턴스를 하나만 생성해서 그 객체를 공유해서 사용하는 패턴이다. 일반적으로 DB Pool과 같이 전체 시스템에서 하나의 자원을 공유해서 사용할 때 이 패턴을 활용할 수 있다.
이 패턴을 구현하는 방법은 synchronized getInstance(), double-checked getInstance(), static final instance 등 다양한데, 내 경우는 Singleton 클래스를 구현하기 위해 static initializer를 [...]]]></description>
			<content:encoded><![CDATA[<div style="display:block;margin-left: 10px; margin-bottom: 10px;">
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://fantazic.com/archives/92" data-text="Java Singleton Pattern 구현" data-count="horizontal" >Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>
<p>최근 singleton 패턴 구현에 대한 질문을 받은 적이 있다. singleton 패턴은 인스턴스를 하나만 생성해서 그 객체를 공유해서 사용하는 패턴이다. 일반적으로 DB Pool과 같이 전체 시스템에서 하나의 자원을 공유해서 사용할 때 이 패턴을 활용할 수 있다.</p>
<p>이 패턴을 구현하는 방법은 synchronized getInstance(), double-checked getInstance(), static final instance 등 다양한데, 내 경우는 Singleton 클래스를 구현하기 위해 static initializer를 활용한 방법을 제안했다.</p>
<pre>
public class Singleton {
  private static Singleton _instance;

  static {
    _instance = new Singleton();
  }

  private Singleton() {}

  public static Singleton getInstance() {
    return _instance;
  }

}
</pre>
<p>이렇게 구현한 경우 클래스가 로딩되면서 객체가 생성되기 때문에 객체가 이중으로 생성될 수 없고, 가장 확실한 방법인 synchronized getInstance()에 비해서 매번 메소드 호출시에 동기화 작업을 안해줘도 되는 장점이 있다. (synchronized 키워드가 있는 경우 메소드 실행이 약 100배 정도 늦어진다고 한다.)</p>
<p>하지만 오늘 <a href="http://wiki.java.net/bin/view/Javapedia/Singleton">관련 정보</a>를 찾아보니, 글 작성자는 명백히 이 방법보다는 synchronized getInstance()가 좋다고 한다.</p>
<p>getInstance()에서 객체를 생성할 때 장점은 lazy initialization(객체가 사용될 때까지 객체 생성을 연기)이 가능하다는 점과 내부 구현을 Pooling 등으로 변경할 때 쉽게 변경이 가능하다는 것이다. (이 부분은 static initializer도 가능하다.)</p>
<p>다만 Singleton 패턴을 사용해도 ClassLoader 마다 개별적으로 인스턴스가 생성되기 때문에 이 부분은 주의를 기울여야 한다. 특히 Servlet Container는 여러개의 ClassLoader를 사용할 수 있으니 이 점을 인지하고 있어야 한다.</p>
]]></content:encoded>
			<wfw:commentRss>http://fantazic.com/archives/92/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

