You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
private으로 선언된 속성 / 매서에 접근 불가 하므로 코드 내부의 세부 구현 내용을 숨기는 것이 가능 (은닉화)
코드의 영역을 분리시켜서 , 효율적 관리 가능
컴파일 시간이 줄어듬
(컴파일러가 , 해당 변수가 어느 범위에서만 쓰이는지를 인지 가능)
싱글톤 (Singleton) 패턴
메모리상에 유일하게 1개만 존재하는 객체 설계
static let shared = Singleton()
private init() {}
싱글톤 패턴이 필요한 이유
앱 구현 시, 유일하게 한개만 존재하는 객체가 필요한 경우
→ 특정한 유일한 데이터 / 관리 객체가 필요한 경우
한번 생성된 이후, 앱이 종료될 때까지 유일한 객체로 메모리에 상주
💡 변수로 접근하는 순간 `lazy` 하게 동작하여, 메모리 (데이터 영역) 에 올라간다.
classSingleton{
// 타입 프로퍼티(전역변수)로 선언
staticletshared=Singleton() // 자신의 객체를 생성해서 전역변수에 할당
varuserInfoId=12345privateinit(){}}
실제 사용 예시
letscreen=UIScreen.main // 화면
letuserDefaults=UserDefaults.standard // 한번생성된 후, 계속 메모리에 남음!!!
letapplication=UIApplication.shared // 앱
letfileManager=FileManager.default // 파일
letnotification=NotificationCenter.default
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
접근 제어(Access Control) 가 필요한 이유?
싱글톤 (Singleton) 패턴
static let shared = Singleton()
private init() {}
싱글톤 패턴이 필요한 이유
앱 구현 시, 유일하게 한개만 존재하는 객체가 필요한 경우
→ 특정한 유일한 데이터 / 관리 객체가 필요한 경우
한번 생성된 이후, 앱이 종료될 때까지 유일한 객체로 메모리에 상주
실제 사용 예시
Beta Was this translation helpful? Give feedback.
All reactions