Skip to content

Instantly share code, notes, and snippets.

View daniyalyousuf07's full-sized avatar
🎯
Focusing

Daniyal daniyalyousuf07

🎯
Focusing
View GitHub Profile
@daniyalyousuf07
daniyalyousuf07 / CatViewModel.swift
Last active March 12, 2023 18:57
Ways to make parallel API calls in Swift. Please note this is code snippet of parallel apis execution and does not focus on code structuring.
//
// CatViewModel.swift
//
// Created by Daniyal Yousuf on 07/01/2023.
//
import Foundation
import Combine
struct CatsModel {
@daniyalyousuf07
daniyalyousuf07 / NetworkService.swift
Created March 11, 2023 12:10
Testable network service using combine
import Foundation
import Combine
enum NetworkServiceErrors: Error {
case invalidURL
case decodingError(String)
case genericError(String)
case invalidResponseCode(Int)
var errorDescription: String {
@daniyalyousuf07
daniyalyousuf07 / Logger.swift
Last active July 20, 2023 04:47
Ways to make Singleton a thread safe and perform async tasks.
import Foundation
let count = 20
class Logger {
var logDic = [String: Any]()
static var shared = Logger()
private init() {}
func writeToLog(key: String, value: String) {
@daniyalyousuf07
daniyalyousuf07 / SignupPresenter+SignupPresenterTests.swift
Last active March 7, 2023 19:24
A demo of Presenter layer of MVP. The class uses combine framework with dependencies injected via constructor making the class testable.
import Foundation
import Combine
final class SignupPresenter: SignupPresenterProtocol {
private var cancellable: Set<AnyCancellable> = Set<AnyCancellable>()
private var formModelValidator: SignupModelValidatorProtocol
private var signupWebService: SignupWebServiceProtocol
private weak var signupViewDelegate: SignupViewDelegateProtocol?
@daniyalyousuf07
daniyalyousuf07 / DecoratorPattern.swift
Last active March 7, 2023 19:24
A demo of Decorator pattern to extend functionality without affecting existing one
import Foundation
struct SignupFormModel {
let firstName: String
let email: String
}
protocol ValidatorProtocol {
func validate(withName name: String) -> Bool
func validate(withEmail email: String) -> Bool
@daniyalyousuf07
daniyalyousuf07 / NetworkManager.swift
Created February 24, 2023 11:34
A demo of Dependency Inversion Principle (DIP) & Example of network mocking
import Foundation
protocol APIHandlerProtocol {
func fetchData(url: URL,
completion: @escaping(Result<Data,
DemoError>) -> Void)
}
protocol ResponseHandlerProtocol {
func parseData<T:Codable>(type: T.Type,
@daniyalyousuf07
daniyalyousuf07 / Birds.swift
Created February 24, 2023 11:14
A demo of Liskov Substitution Principle (LSP)
import Foundation
class Birds {
var breed: String
init(breed: String) {
self.breed = breed
}
@daniyalyousuf07
daniyalyousuf07 / CakePayment.swift
Last active February 24, 2023 10:44
A demo of Open-Closed principle.
import Foundation
protocol CakePaymentDelegate {
func payForCake(amount: Double)
}
class VelvetCakePayment: CakePaymentDelegate {
func payForCake(amount: Double) { }
}
@daniyalyousuf07
daniyalyousuf07 / TelevisionFactory.swift
Last active February 24, 2023 09:34
Factory design pattern
import Foundation
///In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
///
protocol TelevisionCompanyDelegate {
func manufactureTV()
}
final class Sony: TelevisionCompanyDelegate {
@daniyalyousuf07
daniyalyousuf07 / HomeTheatre.swift
Created February 24, 2023 09:26
Facade Pattern
import Foundation
///Facade Class that hides implementation and provide interface to control implementation of all dependencies
/*
The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. A facade can:
improve the readability and usability of a software library by masking interaction with more complex components behind a single (and often simplified) API
provide a context-specific interface to more generic functionality (complete with context-specific input validation)
serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favor of more loosely-coupled code
Developers often use the facade design pattern when a system is very complex or difficult to understand because the system has many interdependent classes or because its source code is unavailable. This