Skip to content

구조체에서 싱글톤 패턴은 싱글톤 패턴이라 보기 힘듭니다. #390

@Mindohyeon

Description

@Mindohyeon

public struct AddAccountAPI {
static let shared = AddAccountAPI()
var addAccountProvider = MoyaProvider<AddAccountService>()
private init() { }

구조체는 인스턴스를 공유하지 않습니다.

클래스의 경우 메모리를 가르키기 때문에(참조) 인스턴스를 공유하지만, 구조체는 value type 이기 때문에 값을 복사합니다.

만약, AddAccountAPI.shared 로 접근한다면, 값 변경이 이루어질 수 없을 뿐만 아니라 접근 할 때마다 다른 메모리를 보고있기 때문에 싱글톤이라고 보기 힘듭니다.

만약 싱글톤의 의미대로 인스턴스를 공유하며 주소를 공유하는 형태로 사용하려면 Class 를 사용하는게 좋아보입니다.
ref. https://hyerios.tistory.com/254

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions