Decorator Pattern
Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects, called decorators.
Using decorators you can wrap objects countless number of times since both target objects and decorators follow the same interface. The resulting object will get a stacking behavior of all wrappers.
Golang Implementation
Example: Pizza Toppings
pizza.go
package main
type IPizza interface {
getPrice() int
}
veggieMania.go
package main
type VeggieMania struct {
}
func (p *VeggieMania) getPrice() int {
return 15
}
tomatoTopping.go
package main
type TomatoTopping struct {
pizza IPizza
}
func (c *TomatoTopping) getPrice() int {
pizzaPrice := c.pizza.getPrice()
return pizzaPrice + 7
}
cheeseTopping.go
package main
type CheeseTopping struct {
pizza IPizza
}
func (c *CheeseTopping) getPrice() int {
pizzaPrice := c.pizza.getPrice()
return pizzaPrice + 10
}
main.go
package main
import "fmt"
func main() {
pizza := &VeggieMania{}
//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())
}
// Price of veggeMania with tomato and cheese topping is 32
Rust Implementation
There is a practical example in Rust’s standard library for input/output operations.
A buffered reader decorates a vector reader adding buffered behavior.
Example: Input streams decoration
main.rs
use std::io::{BufReader, Cursor, Read};
fn main() {
let mut buf = [0u8; 10];
// A buffered reader decorates a vector reader which wraps input data.
let mut input = BufReader::new(Cursor::new("Input data"));
input.read(&mut buf).ok();
print!("Read from a buffered reader: ");
for byte in buf {
print!("{}", char::from(byte));
}
println!();
}
// Read from a buffered reader: Input data