Skip to content

Instantly share code, notes, and snippets.

@dmytro-anokhin
Last active May 14, 2018 13:40
Show Gist options
  • Select an option

  • Save dmytro-anokhin/adca49572d354ce3e88ef76b31245b0e to your computer and use it in GitHub Desktop.

Select an option

Save dmytro-anokhin/adca49572d354ce3e88ef76b31245b0e to your computer and use it in GitHub Desktop.
protocol Component {
func foo()
}
class Composite: Component {
var children: [Component] = []
func foo() {
for child in children {
child.foo()
}
}
}
class LeafA: Component {
func foo() {
print("Hi, I'm Leaf A")
}
}
class LeafB: Component {
func foo() {
print("Hi, I'm Leaf B")
}
}
let composite = Composite()
composite.children = [ LeafA(), LeafB() ]
composite.foo()
// Hi, I'm Leaf A
// Hi, I'm Leaf B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment