ArchitectureMidMCQ
What is SOLID and how does it apply to Swift?
Test your knowledge:
Explanation & Code
Answer: SOLID is five principles for writing maintainable, extensible code.
S — Single Responsibility: A type should do one thing.
// ❌ ViewController fetching, parsing, and displaying
// ✅ Separate into ViewController + ViewModel + Repository
O — Open/Closed: Open for extension, closed for modification.
protocol PaymentMethod { func pay(amount: Double) }
struct ApplePay: PaymentMethod { func pay(amount: Double) { } }
struct CreditCard: PaymentMethod { func pay(amount: Double) { } }
// Add new payment types without changing existing code
L — Liskov Substitution: Subtypes must be substitutable for base types.
// Any ArticleRepository implementation should work wherever the protocol is used
I — Interface Segregation: Prefer small, focused protocols.
protocol Readable { func read() -> Data }
protocol Writable { func write(_ data: Data) }
// Rather than one large DataStore protocol
D — Dependency Inversion: Depend on abstractions, not concretions.
class ViewModel {
let repo: ArticleRepository // protocol, not RemoteArticleRepository
}
Rate your understanding: