This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1: Define the Compression Strategy Protocol | |
// This protocol ensures all compression strategies have a common interface. | |
protocol CompressionStrategy { | |
func compress(files: [String]) -> String | |
} | |
// Step 2: Concrete Compression Strategies | |
// ZIP compression strategy | |
class ZipCompression: CompressionStrategy { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Step 1: Define the Validation Strategy Protocol | |
// Each validation strategy will implement this protocol. | |
protocol ValidationStrategy { | |
func validate(_ input: String) -> Bool | |
} | |
// Step 2: Concrete Validation Strategies | |
// Email validation strategy | |
class EmailValidation: ValidationStrategy { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a protocol that provides a common interface for sorting strategies | |
protocol SortingStrategy { | |
func sort(_ numbers: inout [Int]) // Method to sort an array of integers | |
} | |
// Concrete implementation of the SortingStrategy using Bubble Sort | |
class BubbleSort: SortingStrategy { | |
func sort(_ numbers: inout [Int]) { | |
numbers.sort(by: <) // Sorting in ascending order (using a simple built-in sort for demonstration) | |
print("Sorting array using bubble sort strategy") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define a protocol that all payment methods must conform to | |
// It ensures a uniform interface for calculating the processing fee | |
protocol PaymentMethodProtocol { | |
func calculateProcessingFee(for amount: Double) -> Double | |
} | |
// Concrete implementation of the PaymentMethodProtocol for Credit Card payments | |
class CreditCardPayment: PaymentMethodProtocol { | |
func calculateProcessingFee(for amount: Double) -> Double { | |
return amount * 0.02 // 2% fee for credit card payments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//************************************** family of algorithms ****************************** | |
A family of algorithms refers to a set of related algorithms that solve the same type of problem but use different approaches. | |
For example: | |
Sorting: Bubble Sort, Quick Sort, Merge Sort. | |
Payment processing: Credit Card, Debit Card, Net Banking. | |
Compression: ZIP, RAR, GZIP. | |
These algorithms share a common purpose or goal but differ in their implementation details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScrollView(.horizontal) { | |
LazyHGrid(rows: threeColumnGrid) { | |
// Display the item | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScrollView { | |
LazyVGrid(columns: threeColumnGrid) { | |
// Display the item | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Atomic<Value> { | |
private let queue = DispatchQueue(label: "com.vadimbulavin.atomic") | |
private var value: Value | |
init(wrappedValue: Value) { | |
self.value = wrappedValue | |
} | |
var wrappedValue: Value { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
@propertyWrapper | |
struct UserDefaultWrapper<Value: Codable> { | |
let key: String | |
let defaultValue: Value | |
var container: UserDefaults = .standard | |
private let publisher = PassthroughSubject<Value, Never>() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
struct Capitalized { | |
var wrappedValue: String { | |
didSet { wrappedValue = wrappedValue.capitalized } | |
} | |
init(wrappedValue: String) { | |
self.wrappedValue = wrappedValue.capitalized | |
} | |
} |
NewerOlder