ΥڡϤƤʥ֥åޡɲΥڡޤϤƤʥ֥åޡ Υڡlivedoor åפɲΥڡޤlivedoor å

ܼ

Singleton

  • Creational Patternsΰ
  • ȥפΰ绥ʰΩļˤΰ̣
  • ֥Ȥ1Ĥɬפʤ˻Ѥ롣
    • 2İʾΥ󥹥󥹤¸ߤˤꡢץθư꥽̵̸ȯʤɤ꤬˻Ѥ롣
  • SingletonѥϥХѿΤ褦ʥХʥݥȤ󶡤뤬Թȼʤ
  • 󥹥󥹤ɬפˤʤäȤˤϡΥ饹Ȳ񤹤1ĤΥ󥹥󥹤֤äƤ롣
  • SingletonٱǺ褦˼Ǥ롣
    • ϥ꥽󷿤Υ֥ȤˤϽפǤ롣
  • Ʊ饹ʣʥȤ1˥ɤǤ롣Υ饹SingletonǤ⡢ξ2İʾΥСĤȤˤʤꡢSingletonΥ󥹥󥹤ʣǤ뤳Ȥˤʤ롣
    • ΤᡢʣΥ饹SingletonѤβˡ1ĤʬȤǥ饹ꤹ뤳ȤǤ롣
  • SingletonΥ󥹥ȥ饯privateʤΤǡĥ뤳ȤϤǤʤĥС󥹥ȥ饯public뤤protectedѹ뤳Ȥˤʤ뤬¾Υ饹󥹥󥹲Ǥ뤿῿SingletonǤϤʤʤ롣
    • 󥹥ȥ饯ѹ¾ˤ꤬롣Singletonμϥƥåѿ˴ŤƤΤǡñ˥֥饹Ȥ٤Ƥ饹Ʊ󥹥ѿͭ롣
  • SingletonϹ˻Ѥ٤
  • Java 1.1ϥ١쥯ΥХˤꡢSingletonؤΥХ뻲Ȥʤ硢SingletonŬڤ˲˴Ƥ
    • Java 1.2ʹߤǤϤΥХϲ褷Ƥ롣
  • 󥰥ȥؤΥgetter(ǤgetInstance())̤ƹԤäƤ¤ꡢƱ󥹥󥹤Ф뤳Ȥݾڤ롣Javaξ硢Υ󥹥󥹰ʳ̤Υ󥹥󥹤ʤȤݾڤǤʤ㤨Сξ硢󥹥󥹤ͣˤ礬ꤨȤȤǤ롣

  • åɥס
  • å
  • ܥå
  • ץե
  • 쥸ȥ륪֥
  • ѥ֥
  • ץ󥿤䥰եåɤʤɤΥǥХɥ饤ФȤƯ֥

Singletonѥμ

ٱ䥤󥹥󥹺

1

  • GetInstance()synchronizedĤ뤳Ȥ2ĤΥåɤƱˤΥ᥽åɤ뤳ȤϤǤʤ
    • ʤ顢ƱˤʤΤϽˤ⤫餺ʸGetInstance()θƤӽФˤsynchronizedƯΤϸΨ
      • ˡȤƤԥ󥹥󥹺ѹ뤫ťå֥å󥰤Ȥ

[]VB.NET

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
''' <summary>
''' ٱ䥤󥹥󥹺SingletonѥΥ饹.
''' </summary>
''' <remarks></remarks>
Public Class Singleton
    ' ȤΥ󥹥.
    Private Shared instance_ As Singleton
    ''' <summary>
    ''' 󥹥ȥ饯.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
    End Sub
    ''' <summary>
    ''' ͣΥ󥹥󥹤֤.
    ''' </summary>
    ''' <returns>ͣΥ󥹥</returns>
    ''' <remarks></remarks>
    <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function GetInstance() As Singleton
        If instance_ Is Nothing Then
            instance_ = New Singleton()
        End If
        Return instance_
    End Function
End Class
 
Public Class Client
    <STAThread()> _
    Public Shared Sub Main(ByVal args() As String)
        Dim instance1 As Singleton = Singleton.GetInstance
        Dim instance2 As Singleton = Singleton.GetInstance
        If instance1 Is instance2 Then
            Console.WriteLine("The same instance")
        End If
        Console.ReadLine()
    End Sub
End Class

ťå֥å

ԥ󥹥󥹺

[]VB.NET

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
''' <summary>
''' ԥ󥹥󥹺SingletonѥΥ饹.
''' </summary>
''' <remarks></remarks>
Public Class Singleton
    ' ȤΥ󥹥.
    Private Shared instance_ As Singleton = New Singleton()
    ''' <summary>
    ''' 󥹥ȥ饯.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
    End Sub
    ''' <summary>
    ''' ͣΥ󥹥󥹤֤.
    ''' </summary>
    ''' <returns>ͣΥ󥹥</returns>
    ''' <remarks></remarks>
    Public Shared Function GetInstance() As Singleton
        Return instance_
    End Function
End Class

ꥢ饤к

1readResolve()򥪡С饤ɤ

  • Serializable롣
  • 饹ѿΥꥢСID롣
  • ¾Υ󥹥ѿСtransient롣
  • readResolve()򥪡С饤ɤ롣
    • ˤꡢǥꥢ饤󥹥󥹤˴롣

[]Java

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 
 
 
-
|
|
|
-
!
|
-
|
!
!
import java.io.ObjectStreamException;
import java.io.Serializable;
 
public class Singleton implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final Singleton INSTANCE = new Singleton();
    
    private Singleton() {
    }
 
    private Object readResolve() throws ObjectStreamException {
        return INSTANCE;
    }
}

2 enumѤ

  • enum Ǥľ󲽲ǽʻݾ㤵ƤޤΤǡimplements Serializable ޤreadResolve ⼫ưDz褷Ƥޤ
  • enumѤ줿¾˥󥹥󥹤¸ߤʤȤJVMݾڤƤ뤳Ȥˤʤ롣
  • ޤꥢ饤ǥꥢ饤˴ؤƤ䤳Ȥ򤹤ɬפʤ
  • enumΰտޤƤŪ鳰Ƥ뤿ᡢ´򴶤ͤ뤫⤷ʤ
  • enum˵Ȥơѥ˥󥹥󥹤ʬäƤʤ褦ʾenumȤʤ
  • enumstatic importȤ߹碌ȷ̾άǤΤǡ󥹥ѿINSTANCESINGLETONΤۤ褤⤷ʤ

[]Java

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
-
|
|
|
-
|
!
-
|
!
!
public enum Singleton {
    INSTANCE;
 
    private String field;
    public String getField() {
        return field;
    }
    public void setField(String field) {
        this.field = field;
    }
}

ʸ

  • Head Firstǥѥ
  • Javaǥѥϥɥ֥å