andrejsstepanovs

Decorator in go

Feb 2nd, 2022 (edited)
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.80 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type pizza interface {
  6.     getPrice() int
  7. }
  8.  
  9. type tomatoTopping struct {
  10.     pizza pizza
  11. }
  12.  
  13. func (c *tomatoTopping) getPrice() int {
  14.     pizzaPrice := c.pizza.getPrice()
  15.     return pizzaPrice + 7
  16. }
  17.  
  18. type veggeMania struct {
  19. }
  20.  
  21. func (p *veggeMania) getPrice() int {
  22.     return 15
  23. }
  24.  
  25. type cheeseTopping struct {
  26.     pizza pizza
  27. }
  28.  
  29. func (c *cheeseTopping) getPrice() int {
  30.     pizzaPrice := c.pizza.getPrice()
  31.     return pizzaPrice + 10
  32. }
  33.  
  34. func main() {
  35.  
  36.     pizza := &veggeMania{}
  37.  
  38.     //Add cheese topping
  39.     pizzaWithCheese := &cheeseTopping{
  40.         pizza: pizza,
  41.     }
  42.  
  43.     //Add tomato topping
  44.     pizzaWithCheeseAndTomato := &tomatoTopping{
  45.         pizza: pizzaWithCheese,
  46.     }
  47.  
  48.     fmt.Printf("Price of veggeMania with tomato and cheese topping is %d\n", pizzaWithCheeseAndTomato.getPrice())
  49. }
  50.  
Add Comment
Please, Sign In to add comment