Skip to content
New issue

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

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

Open
Mindohyeon opened this issue Apr 11, 2023 · 0 comments

Comments

@Mindohyeon
Copy link

Mindohyeon commented Apr 11, 2023

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

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

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant