update: interfaces composition

This commit is contained in:
Inanc Gumus
2019-08-30 00:49:27 +03:00
parent 6ab99ac78e
commit ec39a882c8
7 changed files with 52 additions and 36 deletions

View File

@@ -12,12 +12,9 @@ import (
"strings"
)
type summer interface {
sum() money
}
type item interface {
summer // same as: `sum() money`
sum() money
discount(float64)
fmt.Stringer // same as: `String() string`
}
@@ -45,13 +42,7 @@ func (l list) sum() (total money) {
}
func (l list) discount(ratio float64) {
type discounter interface {
discount(float64)
}
for _, it := range l {
if it, ok := it.(discounter); ok {
it.discount(ratio)
}
it.discount(ratio)
}
}