디자인 패턴: 싱글톤
개요
정의
Singleton: a single person or thing of the kind under consideration.
The Singleton design pattern is a creational design pattern. It refers to a class that has only one instance in existence at any given time. If we try to instantiate an instance of the Singleton class for a second time, the new variable also points to the first instance that was created earlier.
Let's take a look at the implementation of this pattern and then talk about some problems it can help solve.
구현
To achieve the Singleton pattern, the class has two main characteristics:
- The constructor is private.
- There is a static method that returns the Singleton instance.
These characteristics allow us to first create an instance for the class if it has yet to be instantiated, and then return that instance whenever the class is called for.
Using C#, the pattern looks like this:
using System;
public class Program
{
public static void Main()
{
Singleton object1 = Singleton.Instance();
Singleton object2 = Singleton.Instance();
if (object1 == object2)
Console.WriteLine("They're the same instance!");
}
}
/// <summary>
/// The Singleton class.
/// </summary>
class Singleton
{
private static Singleton myInstance;
// 1. The constructor is private.
private Singleton() { }
// 2. Static method that returns the Singleton instance.
public static Singleton Instance()
{
if (myInstance == null)
myInstance = new Singleton();
return myInstance;
}
}
In this case, the program would write the string "They're the same instance!". You can play with this code here on .NET Fiddle
실제 사례
Sweet, now we're able to write a class that will only have one instance in our program at a time! What can we do with that? What kind of recurring software development problems can this pattern help solve?
A Singleton pattern is especially useful when accessing some sort of resource that the entire application is making use of that may have issues when being accessed by more than one instance at a time (it helps solve concurrency issues). There are a lot of common development situations where this might be the case, like hardware interface access, and logging data to a file.
In the case of hardware interface access, we may be connecting to hardware that will have concurrency issues when being accessed by multiple instances. For example, the Singleton pattern can help a printer print documents in a certain order, avoiding deadlock situations. In the case of logging data to a file, we may have multiple clients logging to a single file at the same time. The Singleton pattern can help prevent problems with concurrent access to this log file.
면책 조항: 디자인 패턴을 학습하기 위한 많은 리소스가 있으며 다양한 방식으로 구현할 수 있습니다. 이 게시물을 마치면 더 많은 리소스를 탐색하는 것이 좋습니다.
Reference
이 문제에 관하여(디자인 패턴: 싱글톤), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/ryhenness/design-patterns-singleton-2g8d
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
The Singleton design pattern is a creational design pattern. It refers to a class that has only one instance in existence at any given time. If we try to instantiate an instance of the Singleton class for a second time, the new variable also points to the first instance that was created earlier.
Let's take a look at the implementation of this pattern and then talk about some problems it can help solve.
To achieve the Singleton pattern, the class has two main characteristics:
- The constructor is private.
- There is a static method that returns the Singleton instance.
These characteristics allow us to first create an instance for the class if it has yet to be instantiated, and then return that instance whenever the class is called for.
Using C#, the pattern looks like this:
using System;
public class Program
{
public static void Main()
{
Singleton object1 = Singleton.Instance();
Singleton object2 = Singleton.Instance();
if (object1 == object2)
Console.WriteLine("They're the same instance!");
}
}
/// <summary>
/// The Singleton class.
/// </summary>
class Singleton
{
private static Singleton myInstance;
// 1. The constructor is private.
private Singleton() { }
// 2. Static method that returns the Singleton instance.
public static Singleton Instance()
{
if (myInstance == null)
myInstance = new Singleton();
return myInstance;
}
}
In this case, the program would write the string "They're the same instance!". You can play with this code here on .NET Fiddle
실제 사례
Sweet, now we're able to write a class that will only have one instance in our program at a time! What can we do with that? What kind of recurring software development problems can this pattern help solve?
A Singleton pattern is especially useful when accessing some sort of resource that the entire application is making use of that may have issues when being accessed by more than one instance at a time (it helps solve concurrency issues). There are a lot of common development situations where this might be the case, like hardware interface access, and logging data to a file.
In the case of hardware interface access, we may be connecting to hardware that will have concurrency issues when being accessed by multiple instances. For example, the Singleton pattern can help a printer print documents in a certain order, avoiding deadlock situations. In the case of logging data to a file, we may have multiple clients logging to a single file at the same time. The Singleton pattern can help prevent problems with concurrent access to this log file.
면책 조항: 디자인 패턴을 학습하기 위한 많은 리소스가 있으며 다양한 방식으로 구현할 수 있습니다. 이 게시물을 마치면 더 많은 리소스를 탐색하는 것이 좋습니다.
Reference
이 문제에 관하여(디자인 패턴: 싱글톤), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/ryhenness/design-patterns-singleton-2g8d
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(디자인 패턴: 싱글톤), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ryhenness/design-patterns-singleton-2g8d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)