Last active
May 14, 2018 13:40
-
-
Save dmytro-anokhin/adca49572d354ce3e88ef76b31245b0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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