Skip to content

Instantly share code, notes, and snippets.

@cedricbahirwe
Last active August 24, 2022 09:38
Show Gist options
  • Save cedricbahirwe/ffe3a2e5e20a1bfc5cdf0a3b9edd3fb6 to your computer and use it in GitHub Desktop.
Save cedricbahirwe/ffe3a2e5e20a1bfc5cdf0a3b9edd3fb6 to your computer and use it in GitHub Desktop.
A bunch of example illustrating SOLID Principles
import Foundation
class Book {
let name: String
let authorName: String
let year: Int
let price: Double
let isbn: String
init(name: String, authorName: String, year: Int, price: Double, isbn: String) {
self.name = name
self.authorName = authorName
self.year = year
self.price = price
self.isbn = isbn
}
}
class Invoice {
private(set) var book: Book
private(set) var quantity: Int
private(set) var discountRate: Double
private(set) var taxRate: Double
var total: Double { calculateTotal() }
init(book: Book, quantity: Int, discountRate: Double, taxRate: Double) {
self.book = book
self.quantity = quantity
self.discountRate = discountRate
self.taxRate = taxRate
}
func calculateTotal() -> Double {
let price: Double = ((book.price - book.price * discountRate) * Double(self.quantity));
let priceWithTaxes: Double = price * (1 + taxRate);
return priceWithTaxes;
}
}
class InvoicePrinter {
private let invoice: Invoice
init(invoice: Invoice) {
self.invoice = invoice
}
func printInvoice() {
print(invoice.quantity, "x ", invoice.book.name, " " , invoice.book.price, " $");
print("Discount Rate: ", invoice.discountRate);
print("Tax Rate: ", invoice.taxRate);
print("Total: ", invoice.total, " $");
}
}
protocol InvoicePersistence {
func save(invoice: Invoice)
}
class DatabasePersistence: InvoicePersistence {
func save(invoice: Invoice) {
// Save to DB
}
}
class FilePersistence: InvoicePersistence {
func save(invoice: Invoice) {
// Save to file
}
}
class PersistenceManager {
let invoicePersistence: InvoicePersistence
init(invoicePersistence: InvoicePersistence) {
self.invoicePersistence = invoicePersistence;
}
}
class Rectangle {
fileprivate var width, height: Int;
init(_ width: Int, _ height: Int) {
self.width = width;
self.height = height;
}
func getHeight() -> Int { height }
func getWidth() -> Int { width }
func getArea() -> Int { width * height }
func setWidth(_ width: Int) {
self.width = width;
}
func setHeight(_ height: Int) {
self.height = height;
}
}
class Square: Rectangle {
init(_ size: Int) {
super.init(size, size)
}
override func setWidth(_ width: Int) {
super.setWidth(width)
super.setHeight(width)
}
override func setHeight(_ height: Int) {
super.setHeight(height)
super.setWidth(height)
}
}
class Test {
static func getAreaTest(_ r: Rectangle) {
let width = r.getWidth();
r.setHeight(10);
print("Expected area of ", (width * 10), ", got ", r.getArea());
}
static func main() {
let rc = Rectangle(2, 3);
getAreaTest(rc);
let sq = Square(6);
sq.setWidth(5);
getAreaTest(sq);
}
}
Test.main()
class Car {
}
protocol ParkingLot {
func parkCar(); // Decrease empty spot count by 1
func unparkCar(); // Increase empty spots by 1
func getCapacity(); // Returns car capacity
}
protocol PaidParkingLot: ParkingLot {
func calculateFee(_ car: Car) -> Double; // Returns the price based on number of hours
func doPayment(_ car: Car);
}
protocol FreeParkingLot: ParkingLot {}
protocol HourlyFeeParkingLot: PaidParkingLot {}
protocol ConstantFeeParkingLot: PaidParkingLot {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment