Singleton-pattern.md

Swift๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ iOS ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ Class๋ช….shared.๋ฉ”์„œ๋“œ ๋ฐฉ์‹์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ธธ๋ž˜ ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ณด์•˜๋‹ค.

Singleton Pattern

Singleton Pattern์€ ๋””์ž์ธ ํŒจํ„ด์˜ ์ผ๋ถ€๋กœ,

์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ์–ด๋””์„œ๋“  ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

๋™์‹œ์„ฑ(Concurreny)๋ฌธ์ œ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ(Thread-Safe ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์—ฌ) multi-thread์—์„œ ์‚ฌ์šฉํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋„๋ก ์„ค๊ณ„ํ•ด์•ผํ•œ๋‹ค.

์‚ฌ์šฉ์ด์œ 

  • ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋“ฑ๋กํ•˜์—ฌ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์ฒญ์ด ๋งŽ์€ ๊ณณ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

in Code

/// in Referenced Class
class AuthManager {
	static let shared = AuthManager()
	public func registerNewUser(email: String, password: String) {
		// Create New User, Insert account to database
	}
}

/// Client
AuthManager.shared.registerNewUser(email: email, password: password) {
	// Something
}

์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šคํ™”์‹œํ‚จ shared ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์•ผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Last updated