Created
May 3, 2020 09:47
-
-
Save mudiadamz/c32525bd466535c586951e3c522052e7 to your computer and use it in GitHub Desktop.
This file contains 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
package visitorpatern; | |
import lombok.Data; | |
import java.util.Collections; | |
import java.util.List; | |
@Data | |
class Person { | |
private String name; | |
private List<Person> children; | |
@SuppressWarnings("unchecked") | |
public static Person ofName(String name){ | |
Person person = new Person(); | |
person.setName(name); | |
person.setChildren(Collections.EMPTY_LIST); | |
return person; | |
} | |
public static Person of(String name, List<Person> children){ | |
Person person = new Person(); | |
person.setName(name); | |
person.setChildren(children); | |
return person; | |
} | |
public void acceptChildren(PersonRecursiveVisitor visitor) { | |
myWalker( visitor, children); | |
} | |
public static void myWalker(PersonRecursiveVisitor visitor, List<Person> children) { | |
for (Person child : children) { | |
visitor.visit(child); | |
myWalker(visitor, child.getChildren()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment