Created
May 3, 2016 16:02
-
-
Save thanhluu/7f6f7d3342e733bda14f07043f08625c to your computer and use it in GitHub Desktop.
Protocols
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
protocol FullyNameable { | |
var fullName: String { get } | |
} | |
struct User: FullyNameable { | |
var fullName: String | |
} | |
let user = User(fullName: "John Smith") | |
struct Friend: FullyNameable { | |
let firstName: String | |
let middleName: String | |
let lastName: String | |
var fullName: String { | |
return "\(firstName) \(middleName) \(lastName)" | |
} | |
} | |
let friend = Friend(firstName: "Taylor", middleName: "Alison", lastName: "Swift") | |
friend.fullName | |
// Bai Tap | |
protocol UserType { | |
var name: String { get } | |
var age: Int { get set } | |
} | |
struct Person: UserType { | |
let name: String | |
var age: Int | |
} | |
let somePerson = Person( name: "Thanh Luu", age: 27 ) | |
/***********************************/ | |
import Foundation | |
protocol Payable { | |
func pay() -> (basePay: Double, benefits: Double, deductions: Double, vacationTime: Int) | |
} | |
enum EmployeeType { | |
case Manager | |
case NotManager | |
} | |
class Employee { | |
let name: String | |
let address: String | |
let startDate: NSDate | |
let type: EmployeeType | |
var department: String? | |
var reportsTo: String? | |
init(fullName: String, employeeAddress: String, employeeStartDate: NSDate, employeeType: EmployeeType) { | |
self.name = fullName | |
self.address = employeeAddress | |
self.startDate = employeeStartDate | |
self.type = employeeType | |
} | |
} | |
func payEmployee (employee: Payable) { | |
let paycheck = employee.pay() | |
} | |
class HourlyEmployee: Employee, Payable { | |
var hourlyWage: Double = 15.00 | |
var hoursWorked: Double = 10 | |
let availableVacation = 0 | |
func pay() -> (basePay: Double, benefits: Double, deductions: Double, vacationTime: Int) { | |
return( hourlyWage * hoursWorked, 0, 0, availableVacation ) | |
} | |
} | |
let hourlyEmployee = HourlyEmployee(fullName: "Pasan", employeeAddress: "none", employeeStartDate: NSDate(), employeeType: .NotManager) | |
payEmployee(hourlyEmployee) | |
let employee = Employee(fullName: "Gabe", employeeAddress: "some address", employeeStartDate: NSDate(), employeeType: .Manager) | |
//payEmployee(Employee) | |
// Bai Tap | |
// Declare protocol here | |
protocol ColorSwitchable { | |
func switchColor(color: Color) | |
} | |
enum LightState { | |
case On, Off | |
} | |
enum Color { | |
case RGB(Double, Double, Double, Double) | |
case HSB(Double, Double, Double, Double) | |
} | |
class WifiLamp: ColorSwitchable { | |
let state: LightState | |
var color: Color | |
init() { | |
self.state = .On | |
self.color = .RGB(0,0,0,0) | |
} | |
func switchColor (color: Color) { | |
self.color = color | |
} | |
} | |
/***********************************/ | |
protocol Blendable { | |
func blend() | |
} | |
class Fruit: Blendable { | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
func blend() { | |
print("I'm mush") | |
} | |
} | |
class Dairy { | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
} | |
class Cheese: Dairy {} | |
class Milk: Dairy, Blendable { | |
func blend() { | |
print("I haven't changed. I'm still milk") | |
} | |
} | |
func makeSmoothie(ingredients: [Blendable]) { | |
for ingredient in ingredients { | |
ingredient.blend() | |
} | |
} | |
let strawberry = Fruit(name: "Strawberry") | |
let cheddar = Cheese(name: "Cheddar") | |
let chocolateMilk = Milk(name: "Chocolate") | |
let ingredients: [Blendable] = [strawberry, chocolateMilk] | |
makeSmoothie(ingredients) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment