I'm working through a design patterns book and trying to apply the patterns to Go as a way to learn both.
Currently I have reached the decorator pattern and I ran into a scenario I was curious about.
Here is some sample code:
type Beverage interface {
getDescription() string
cost() float64
}
type HouseBlend struct{}
func (hb *HouseBlend) getDescription() string {
return "House Blend Coffee"
}
func (hb *HouseBlend) cost() float64 {
return .89
}
type Mocha struct {
b Beverage
}
func (m *Mocha) getDescription() string {
return m.b.getDescription() + ", Mocha"
}
func (m *Mocha) cost() float64 {
return m.b.cost() + .20
}
What is the difference between
var hb Beverage = &HouseBlend{}
//This works since hb is an interface type
hb = &Mocha{
b: hb,
}
And
hb := &HouseBlend{}
//This assignment fails since Mocha is not type HouseBlend
hb = &Mocha{
b: hb,
}
This also works
hb := *new(Beverage)
hb = &Espresso{}
hb = &Mocha{
b: hb,
}
Is there a shorthand way of giving my variable hb the interface type or does it need to be explicit in order to be able to "decorate" my struct and reassign the variable to different types?
Any suggestions on improving the decorator pattern here and achieving clean polymorphism are welcome. Thank you!
Aucun commentaire:
Enregistrer un commentaire