개인 이해의 단례 모델
5907 단어 단일 모드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
public sealed class Singleton
{
static Singleton instance;
/// <summary>
/// ,
/// </summary>
private Singleton() { }
public static Singleton Instance
{
get
{
return instance == null ? new Singleton() : instance;
}
}
public void dd(){
Console.WriteLine("fdd");
Console.ReadLine();
}
}
}
주 메서드는 다음과 같이 호출됩니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Singleton.Instance.dd();
}
}
}
물론 이것은 때때로 단례가 유일하다는 것을 보장할 수 없으며, lock 방법으로 다음과 같이 실현할 수 있다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
public sealed class Singleton
{
static Singleton instance;
static readonly object padlock = new object();
public Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (padlock)
{
if (instance == null)
{
instance=new Singleton() ;
}
}
}
return instance;
}
}
public void dd()
{
Console.WriteLine("fdd");
Console.ReadLine();
}
}
}
환영하다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java 디자인 모델의 단례 모델소프트웨어 개발 과정에서 우리는 라인 탱크(threadpool), 캐시(cache), 대화상자, 선호 설정 등 하나만 필요로 하는 대상이 종종 있다.이러한 대상이 여러 개의 실례를 만들면 프로그램 행위 이상, 자원 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.