We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
생성
class UserInfo { **static let shared = UserInfo()** var id: String? var pass: String? var name: string? **private init() { }** }
static
private init() { }
접근: static프로퍼티를 통해 접근
let userInfo = UserInfo.shared userInfo.id = "bbu"
예시
let screen = UIScreen.main let userDefaults = UserDefaults.standard let application = UIApplication.shared let fileManager = FileManager.default let notification = NotificationCenter.default let sharedURLSession = URLSession.shared
??
thread-safe하지 않은 환경에서 singleton을 생성하면, 동시에 여러 개의 싱글톤 인스턴스가 생성될 수 있음 → 이는 더 이상 싱글톤이 아님.
swift: static을 사용해 타입 프로퍼티로 인스턴스 생성 → 사용시점에 초기화(lazy)
싱글톤 인스턴스가 최초 생성되기 전까지는 메모리에 올라가지 않고(lazy), 한 번 생성되면 그 뒤로는 생성되지 않는 objc의 dispatch_once도 자동으로 적용된다.(dispatch_once: 앱 실행되고 단 한 번만 실행되도록 제어)
싱글톤을 생성할 때 thread-safe하게 동작
The text was updated successfully, but these errors were encountered:
No branches or pull requests
# 싱글톤패턴 Singleton Pattern
사용
생성
static
프로퍼티에 인스턴스 생성private init() { }
→ init 함수 호출을 통해 또 다른 인스턴스 생성을 방지
→ 객체를 생성하지 않아도, 접근이 가능하도록
접근: static프로퍼티를 통해 접근
예시
장단점
장점
??
DBCP(DataBase Connection Pool) 처럼 공통된 객체를 여러개 생성해서 사용해야 하는 상황에서 많이 쓰임(쓰레드풀, 캐시, 대화상자, 사용자설정, 등)단점
→ 수정, 테스트 어려워짐
특징 thread-safe? (feat. type property)
thread-safe하지 않은 환경에서 singleton을 생성하면, 동시에 여러 개의 싱글톤 인스턴스가 생성될 수 있음 → 이는 더 이상 싱글톤이 아님.
swift: static을 사용해 타입 프로퍼티로 인스턴스 생성 → 사용시점에 초기화(lazy)
싱글톤 인스턴스가 최초 생성되기 전까지는 메모리에 올라가지 않고(lazy), 한 번 생성되면 그 뒤로는 생성되지 않는 objc의 dispatch_once도 자동으로 적용된다.(dispatch_once: 앱 실행되고 단 한 번만 실행되도록 제어)
싱글톤을 생성할 때 thread-safe하게 동작
🔖참고
The text was updated successfully, but these errors were encountered: