Swift में डिज़ाइन पैटर्न: iOS डेवलपर के लिए कोड उदाहरण

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें
Here is the translated HTML content in Hindi, with all HTML tags, code, and specific terms preserved as instructed. ```html

डिज़ाइन पैटर्न Swift में: सिद्धांत से व्यवहार तक

डिज़ाइन पैटर्न डेवलपमेंट में सामान्य समस्याओं के सिद्ध समाधान हैं। Swift का उपयोग करने वाले iOS डेवलपर के लिए, पैटर्न में महारत हासिल करना केवल रिज़्यूमे पर एक प्लस नहीं, बल्कि एक आवश्यकता है। वे साफ, पुन: प्रयोग करने योग्य और परीक्षण योग्य कोड लिखने में मदद करते हैं। इस लेख में, हम Swift में वास्तविक उदाहरणों के साथ 5 प्रमुख पैटर्न का विश्लेषण करेंगे।



1. सिंगलटन

iOS में सबसे लोकप्रिय पैटर्न में से एक। यह गारंटी देता है कि एक क्लास का केवल एक उदाहरण है और उस तक पहुंचने के लिए एक वैश्विक बिंदु प्रदान करता है।



Swift में कार्यान्वयन

class NetworkManager {    static let shared = NetworkManager()    private init() {} // prevents creating other instances

func requestData(from url: URL) { // request logic print("Request to \\(url.absoluteString)") }}

// UsageNetworkManager.shared.requestData(from: URL(string: "https://api.example.com")!)


कब उपयोग करें: साझा संसाधनों (नेटवर्क, डेटाबेस, UserDefaults) के प्रबंधन के लिए। नुकसान: यूनिट टेस्टिंग को जटिल बनाता है, इसलिए इसका अत्यधिक उपयोग नहीं करना चाहिए।



2. फैक्टरी मेथड

एक ऑब्जेक्ट बनाने के लिए एक इंटरफ़ेस परिभाषित करता है लेकिन उपवर्गों को बनाए जाने वाले ऑब्जेक्ट के प्रकार को बदलने देता है। यह तब उपयुक्त है जब पहले से ज्ञात न हो कि किस विशिष्ट ऑब्जेक्ट की आवश्यकता होगी।



उदाहरण: UI तत्व बनाना

protocol Button {    func render()}

class iOSButton: Button { func render() { print("iOS style button") }}

class macOSButton: Button { func render() { print("macOS style button") }}

enum Platform { case iOS, macOS}

class ButtonFactory { static func createButton(for platform: Platform) -> Button { switch platform { case .iOS: return iOSButton() case .macOS: return macOSButton() } }}

// Usagelet button = ButtonFactory.createButton(for: .iOS)button.render() // iOS style button


3. ऑब्ज़र्वर

ऑब्जेक्ट को इवेंट्स की सदस्यता लेने और परिवर्तनों के बारे में सूचनाएं प्राप्त करने की अनुमति देता है। Swift में, इसे मूल रूप से NotificationCenter और Combine के माध्यम से कार्यान्वित किया जाता है।



NotificationCenter के साथ उदाहरण

extension Notification.Name {    static let userDidLogin = Notification.Name("userDidLogin")}

class AuthManager { func login() { // login logic NotificationCenter.default.post(name: .userDidLogin, object: nil) }}

class ProfileViewController { init() { NotificationCenter.default.addObserver( self, selector: #selector(updateUI), name: .userDidLogin, object: nil ) }

@objc func updateUI() { print("Updating profile after login") }

deinit { NotificationCenter.default.removeObserver(self) }}


आधुनिक विकल्प: रिएक्टिव प्रोग्रामिंग के लिए Combine (iOS 13+) का उपयोग @Published और PassthroughSubject के साथ करें।



4. डेलिगेट

Cocoa Touch के मौलिक पैटर्न में से एक। यह एक ऑब्जेक्ट को कार्य निष्पादन को दूसरे को सौंपने की अनुमति देता है। इसका व्यापक रूप से UIKit में उपयोग किया जाता है (जैसे, UITableViewDelegate)।



कस्टम डेलिगेट बनाना

protocol DownloadDelegate: AnyObject {    func didFinishDownloading(data: Data)    func didFailWithError(error: Error)}

class Downloader { weak var delegate: DownloadDelegate?

func download(from url: URL) { // simulating download let data = Data() delegate?.didFinishDownloading(data: data) }}

class ViewController: UIViewController, DownloadDelegate { let downloader = Downloader()

override fu

ब्लॉग

पुस्तक अनुशंसाएँ