Last active
August 29, 2015 14:25
-
-
Save floralvikings/f31f3501ed9476bc8174 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
import java.util.Collection; | |
import java.util.LinkedList; | |
public class Main | |
{ | |
public static void main(String[] args) { | |
Node carNode = new CarNode(); | |
for (int i = 1; i <= 4; i++) { | |
carNode.getChildren().add(new TireNode(i)); | |
} | |
// Fifthwheel node is defined outside the "library" | |
carNode.getChildren().add(new FifthWheelNode()); | |
// As is ExtendedTask | |
carNode.doTask(new ExtendedTask()); | |
/* | |
Output: | |
Car | |
Tire: 1 | |
Tire: 2 | |
Tire: 3 | |
Tire: 4 | |
Tire: 5 | |
Notice that the last print statement (Tire: 5) doesn't actually print the message defined in ExtendedTask, | |
but instead prints the default doTask(TireNode tireNode) method as defined in the Task interface. I could | |
add the doTask(FifthWheelNode) to the Task interface, but what about a developer using the library, who couldn't | |
modify the original source? | |
*/ | |
} | |
private interface Task | |
{ | |
default void doTask(Node node) { System.out.println("Node"); } | |
default void doTask(CarNode carNode) { System.out.println("Car"); } | |
default void doTask(TireNode tireNode) { System.out.println("Tire: " + tireNode.size); } | |
} | |
private abstract static class Node | |
{ | |
public abstract Collection<Node> getChildren(); | |
public abstract void doTask(Task task); | |
} | |
private static class CarNode extends Node | |
{ | |
private Collection<Node> tires; | |
public CarNode() { tires = new LinkedList<>(); } | |
@Override | |
public void doTask(Task task) { | |
task.doTask(this); | |
getChildren().forEach(child -> child.doTask(task)); | |
} | |
@Override | |
public Collection<Node> getChildren() { return tires; } | |
} | |
private static class TireNode extends Node | |
{ | |
private int size; | |
private TireNode(int size) { this.size = size; } | |
@Override | |
public void doTask(Task task) { | |
task.doTask(this); | |
getChildren().forEach(child -> child.doTask(task)); | |
} | |
@Override | |
public Collection<Node> getChildren() { | |
return new LinkedList<>(); | |
} | |
} | |
// ~~~~~~~ New Type and task added by developer using this library ~~~~~~~ | |
private static class ExtendedTask implements Task | |
{ | |
public void doTask(FifthWheelNode fifthWheelNode) { System.out.println("Fifth Wheel"); } | |
} | |
private static class FifthWheelNode extends TireNode | |
{ | |
FifthWheelNode() | |
{ | |
super(5); | |
} | |
@Override | |
public void doTask(Task task) { | |
task.doTask(this); | |
getChildren().forEach(child -> child.doTask(task)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment