1. Diagrama de Classes
2. Benefícios x Desvantagens
Controle da instância: possui total controle da instância, já que a mesma controla o processo de instancialização.
3. Código
C#
using System;
using System.Collections.Generic;
using System.Text;
namespace SingletonTest
{
class Program
{
static void Main(string[] args)
{
ServerList serverList = ServerList.GetInstance();
}
}
///
/// Singleton
///
sealed class ServerList
{
private static readonly ServerList _instance = new ServerList();
private IDictionary_serverList = new Dictionary ();
private ServerList()
{
PopulateServerList();
}
///
/// Popula lista para o exemplo.
///
private void PopulateServerList()
{
this._serverList.Add("DarthVader", "192.168.0.1");
this._serverList.Add("Luke Skywalker", "192.168.0.2");
this._serverList.Add("Obi-wan", "192.168.0.3");
}
///
/// Retorna a instancia de ServerList.
///
public static ServerList GetInstance()
{
return _instance;
}
}
}
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace SingletonTest
Class Program
Private Shared Sub Main(ByVal args As String())
Dim serverList__1 As ServerList = ServerList.GetInstance()
End Sub
End Class
'''
''' Singleton
'''
NotInheritable Class ServerList
Private Shared ReadOnly _instance As New ServerList()
Private _serverList As IDictionary(Of String, String) = New Dictionary(Of String, String)()
Private Sub New()
PopulateServerList()
End Sub
'''
''' Popula lista para o exemplo.
'''
Private Sub PopulateServerList()
Me._serverList.Add("DarthVader", "192.168.0.1")
Me._serverList.Add("Luke Skywalker", "192.168.0.2")
Me._serverList.Add("Obi-wan", "192.168.0.3")
End Sub
'''
''' Retorna a instancia de ServerList.
'''
Public Shared Function GetInstance() As ServerList
Return _instance
End Function
End Class
End Namespace
Nenhum comentário:
Postar um comentário