Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- swiftdata
- Firebase Analytics
- swift db
- GCD
- JSON
- ios
- Combine
- swift 5.9
- Subscribe
- xcode
- Swift Tuist
- 카카오뱅크 ios
- ribs
- Firebase
- swift 6
- SwiftUI
- swift
- arc
- realm
- Tuist
- ios swiftdata
- RxSwift
- Tuist Swift
- Subject
- ios database
- observable
- 네트워크 통신
- KeyPath
- swift database
- SeSAC
Archives
- Today
- Total
천원의 개발
Swift Adapter 본문
안녕하세요. 천원입니다.
다른 분들의 Github 코드를 살펴보다가 재상용성이 좋아보이는 코드가 Adapter 패턴으로 작성되었다는걸 알고 공부해 보고자 정리합니다.
Adapter 패턴
현재 특정 기능을 수행하는 인터페이스가 존재하는데 이와 비슷하지만 조금씩 다른 기능을 추가해야할 때 Adapter를 사용해서 사용하는 쪽에서 코드의 변경을 최소화하는 패턴, 코드의 중복을 방지하고 현재 서비스 중인 프로젝트에 대하여 변경을 최소화 할수있습니다.
구현
현재 인증 서비스가 구현되어 있었는데, Google 과 Naver 인증 서비스가 추가되는 시나리오를 구현해 보겠습니다.
protocol AuthServiceType {
func authenticate(username: String, password: String, completion: @escaping (Bool) -> Void)
}
class AuthService: AuthServiceType {
func authenticate(username: String, password: String, completion: @escaping (Bool) -> Void) {
(username == "admin" && password == "admin") ? completion(true) : completion(false)
}
}
class ViewController: UIViewController {
let authService: AuthServiceType = AuthService()
override func viewDidLoad() {
super.viewDidLoad()
authService.authenticate(username: "admin", password: "admin") { result in
print(result)
}
}
}
먼저 위에 코드는 AuthServcieType을 채택한 AuthService 클래스에서 인증을 수행하는 함수와 호출부를 구현한 코드입니다.
이제 Google과 Naver에서는이름과 비밀번호에 추가로 키값을 입력해야하는 Class를 작성해보겠습니다.
enum Key {
static let googleKey = 111111
static let naverKey = 222222
}
class AuthGoogleService {
func authenticate(username: String, password: String, authNumber: Int, completion: @escaping (Bool) -> Void) {
(username == "admin" && password == "admin" && authNumber == Key.googleKey) ? completion(true) : completion(false)
}
}
class AuthNaverService {
func authenticate(username: String, password: String, authNumber: Int, completion: @escaping (Bool) -> Void) {
(username == "admin" && password == "admin" && authNumber == Key.naverKey) ? completion(true) : completion(false)
}
}
class ViewController: UIViewController {
let authService: AuthServiceType = AuthService()
let googleAuth = AuthGoogleService()
let naverAuth = AuthNaverService()
override func viewDidLoad() {
super.viewDidLoad()
authService.authenticate(username: "admin", password: "admin") { result in
print(result)
}
googleAuth.authenticate(username: "admin", password: "admin", authNumber: Key.googleKey) { result in
print(result)
}
naverAuth.authenticate(username: "admin", password: "admin", authNumber: Key.naverKey) { result in
print(result)
}
}
}
동일한 인증 서비스지만 파라미터로 authNumber를 추가적으로 받아서 호출부에서 다른 모습을 보여줍니다. Adapter 패턴을 통해서 수정해보겠습니다.
class AuthGoogleServiceAdapter: AuthServiceType {
let authGoogleService = AuthGoogleService()
func authenticate(username: String, password: String, completion: @escaping (Bool) -> Void) {
authGoogleService.authenticate(username: username, password: password, authNumber: Key.googleKey, completion: completion)
}
}
class AuthNaverServiceAdapter: AuthServiceType {
let authNaverService = AuthNaverService()
func authenticate(username: String, password: String, completion: @escaping (Bool) -> Void) {
authNaverService.authenticate(username: username, password: password, authNumber: Key.naverKey, completion: completion)
}
}
class ViewController: UIViewController {
let authService: AuthServiceType = AuthService()
let googleAuth: AuthServiceType = AuthGoogleServiceAdapter()
let naverAuth: AuthServiceType = AuthNaverServiceAdapter()
override func viewDidLoad() {
super.viewDidLoad()
authService.authenticate(username: "admin", password: "admin") { result in
print(result)
}
googleAuth.authenticate(username: "admin", password: "admin") { result in
print(result)
}
naverAuth.authenticate(username: "admin", password: "admin") { result in
print(result)
}
}
}
호출부에서 깔끔하게 호출이 가능해진 모습입니다.
전체코드: https://github.com/Yoon-hub/ExAdapter
https://ios-development.tistory.com/1235
https://www.kodeco.com/books/design-patterns-by-tutorials/v3.0/chapters/12-adapter-pattern
'iOS&Swift🍎 > Swift' 카테고리의 다른 글
Swift RIBs RootRIB 설정하기 (0) | 2023.09.09 |
---|---|
Swift RIBs 튜토리얼 (with StoryBorad) (0) | 2023.08.26 |
Swift RIBs (0) | 2023.07.27 |
Swift 전처리문 (#if Debug #endif) (0) | 2023.05.16 |
Swift Concurrency(동시성 프로그래밍) (0) | 2023.04.06 |