Skip to content

Instantly share code, notes, and snippets.

@nomkhonwaan
Last active January 10, 2020 02:02
Show Gist options
  • Save nomkhonwaan/d643f1f818bbcadb814d071bb6720ae9 to your computer and use it in GitHub Desktop.
Save nomkhonwaan/d643f1f818bbcadb814d071bb6720ae9 to your computer and use it in GitHub Desktop.
go-solid-single-responsibility-5dfc2e5da33fd7a8f2eebc59
// Money represents currency in Thai Baht
type Money float64
// Employee is a person employed by a company
type Employee struct {
// An amount to-be paid per working hour
salaryPerHour Money
// A total number of working hours of the employee
workingHours int
}
// CalculatePay does calculating a total amount to-be paid to the employee
func (e Employee) CalculatePay() Money {
return e.salaryPerHour * Money(e.workingHours)
}
// Save stores an employee data to the data storage
func (e Employee) Save() {
fmt.Println("an employee data has been saved")
}
// ReportHours returns a number of working hours
func (e Employee) ReportHours() string {
return fmt.Sprintf("an employee has been working %d hours so far...", e.workingHours)
}
// Employee is an abstraction of all employee types.
type Employee interface {
// Return an amount to-be paid per working hour
SalaryPerHour() Money
// Return a total number of working hours of the employee
TotalWorkingHours() int
// Return a number of working hours
ReportHours() string
}
// An EmployeeDAO
type EmployeeDAO struct{}
// Save stores an employee data to the data storage
func (repo EmployeeDAO) Save(e Employee) error {
fmt.Println("an employee data has been saved")
return nil
}
// An EmployeePayment
type EmployeePayment struct{}
// CalculatePay does calculating a total amount to-be paid to the employee
func (p EmployeePayment) CalculatePay(e Employee) Money {
return e.SalaryPerHour() * Money(e.TotalWorkingHours())
}
// DailyEmployee will be paid every day at the end of the day
type DailyEmployee struct {
// An amount to-be paid per working hour
salaryPerHour Money
// A daily working hours
dailyWorkingHours int
}
func (e DailyEmployee) SalaryPerHour() Money {
return e.salaryPerHour
}
func (e DailyEmployee) TotalWorkingHours() int {
return e.dailyWorkingHours
}
func (e DailyEmployee) ReportHours() string {
return fmt.Sprintf("an employee has been working %d hours so far...", e.dailyWorkingHours)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment