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

@ -8,19 +8,29 @@
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type book struct {
*product
published interface{}
product
Published interface{}
}
func (b *book) String() string {
p := format(b.published)
return fmt.Sprintf("%s - (%v)", b.product, p)
p := format(b.Published)
return fmt.Sprintf("%s - (%v)", &b.product, p)
}
func (b *book) MarshalJSON() ([]byte, error) {
type jbook book
jb := (*jbook)(b)
jb.Published = format(b.Published)
return json.Marshal(jb)
}
func format(v interface{}) string {

View File

@ -8,5 +8,5 @@
package main
type game struct {
*product
product
}

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)
}
}

View File

@ -8,35 +8,50 @@
package main
import (
"encoding/json"
"fmt"
"sort"
)
func main() {
store := list{
&book{&product{"moby dick", 10}, 118281600},
&book{&product{"odyssey", 15}, "733622400"},
&book{&product{"hobbit", 25}, nil},
&puzzle{&product{"rubik's cube", 5}},
&game{&product{"minecraft", 20}},
&game{&product{"tetris", 5}},
&toy{&product{"yoda", 150}},
&book{product{"moby dick", 10}, 118281600},
&book{product{"odyssey", 15}, "733622400"},
&book{product{"hobbit", 25}, nil},
&puzzle{product{"rubik's cube", 5}},
&game{product{"minecraft", 20}},
&game{product{"tetris", 5}},
&toy{product{"yoda", 150}},
}
out, _ := json.Marshal(store)
fmt.Println(string(out))
var (
nilItem item
nilBook *book
)
fmt.Println("nilBook ?", nilBook == nil)
fmt.Println("nilItem?", nilItem == nil)
nilItem = nilBook
fmt.Println("nilItem?", nilItem == nil)
books := store[:3]
others := store[3:]
books.discount(.5)
fmt.Printf("%s\n", store)
others := store[3:]
sort.Sort(byPrice(others))
fmt.Printf("\n%s\n", store)
store = list{books, others}
fmt.Printf("\n%s\n", store)
// store.discount(.5)
// // sort.Sort(sort.Reverse(byPrice(store)))
// sort.Sort(byName(store))
// fmt.Println(store)
store.discount(.5)
// sort.Sort(sort.Reverse(byPrice(store)))
sort.Sort(byName(store))
fmt.Println(store)
}

View File

@ -10,18 +10,18 @@ package main
import "fmt"
type product struct {
title string
price money
Title string
Price money
}
func (p *product) discount(ratio float64) {
p.price *= money(1 - ratio)
p.Price *= money(1 - ratio)
}
func (p *product) sum() money {
return p.price
return p.Price
}
func (p *product) String() string {
return fmt.Sprintf("%-15s: %s", p.title, p.price)
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
}

View File

@ -8,5 +8,5 @@
package main
type puzzle struct {
*product
product
}

View File

@ -8,5 +8,5 @@
package main
type toy struct {
*product
product
}