Skip to main content

Go Example

package main

type cheeseTopping struct {
pizza pizza
}

func (c *cheeseTopping) getPrice() int {
pizzaPrice := c.pizza.getPrice()
return pizzaPrice + 10
}
package main

type pizza interface {
getPrice() int
}
package main

type tomatoTopping struct {
pizza pizza
}

func (c *tomatoTopping) getPrice() int {
pizzaPrice := c.pizza.getPrice()
return pizzaPrice + 7
}
package main

type veggeMania struct {
}

func (p *veggeMania) getPrice() int {
return 15
}
package main

import "fmt"

func main() {

pizza := &veggeMania{}

//Add cheese topping
pizzaWithCheese := &cheeseTopping{
pizza: pizza,
}

//Add tomato topping
pizzaWithCheeseAndTomato := &tomatoTopping{
pizza: pizzaWithCheese,
}

fmt.Printf("Price of veggeMania with tomato and cheese topping is %d\n", pizzaWithCheeseAndTomato.getPrice())
}