diff --git a/Command/src/client/Client.java b/Command/src/client/Client.java index e9a4a1d..5f8fcfc 100644 --- a/Command/src/client/Client.java +++ b/Command/src/client/Client.java @@ -6,10 +6,11 @@ public class Client { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { Invoker invoker = new Invoker(new Receiver()); invoker.setCommand(TypeCommand.OPEN); invoker.execute(); + Thread.sleep(1500); invoker.setCommand(TypeCommand.EXIT); invoker.execute(); } diff --git a/Command/src/command/ExitCommand.java b/Command/src/command/ExitCommand.java index b27801b..0f9ac7d 100644 --- a/Command/src/command/ExitCommand.java +++ b/Command/src/command/ExitCommand.java @@ -12,7 +12,7 @@ public ExitCommand(Receiver receiver) { @Override public void execute() { - receiver.close(); + receiver.exit(); } } diff --git a/Command/src/receiver/Receiver.java b/Command/src/receiver/Receiver.java index 01b64bb..ec2264b 100644 --- a/Command/src/receiver/Receiver.java +++ b/Command/src/receiver/Receiver.java @@ -3,10 +3,10 @@ public class Receiver { public void open() { - System.out.println("open."); + System.out.println("open"); } - public void close() { - System.out.println("close."); + public void exit() { + System.out.println("exit"); } } diff --git a/Strategy/.classpath b/Strategy/.classpath new file mode 100644 index 0000000..63b7e89 --- /dev/null +++ b/Strategy/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Strategy/.gitignore b/Strategy/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/Strategy/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/Strategy/.project b/Strategy/.project new file mode 100644 index 0000000..bccffdf --- /dev/null +++ b/Strategy/.project @@ -0,0 +1,17 @@ + + + Strategy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Strategy/.settings/org.eclipse.jdt.core.prefs b/Strategy/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/Strategy/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Strategy/src/ContextVisit.java b/Strategy/src/ContextVisit.java new file mode 100644 index 0000000..fcc7417 --- /dev/null +++ b/Strategy/src/ContextVisit.java @@ -0,0 +1,23 @@ + +public class ContextVisit { + + private Visit visit; + + private Node root; + + public ContextVisit(Node root) { + this.root = root; + } + + public void setRoot(Node root) { + this.root = root; + } + + public void setVisit(Visit visit) { + this.visit = visit; + } + + public void visit() { + visit.visit(root); + } +} diff --git a/Strategy/src/DeepVisit.java b/Strategy/src/DeepVisit.java new file mode 100644 index 0000000..80b917c --- /dev/null +++ b/Strategy/src/DeepVisit.java @@ -0,0 +1,11 @@ + +public class DeepVisit implements Visit { + + @Override + public void visit(Node root) { + System.out.print(root); + for(Node node: root.getChildren()) + visit(node); + } + +} diff --git a/Strategy/src/LevelVisit.java b/Strategy/src/LevelVisit.java new file mode 100644 index 0000000..f1ee668 --- /dev/null +++ b/Strategy/src/LevelVisit.java @@ -0,0 +1,16 @@ +import java.util.ArrayList; + +public class LevelVisit implements Visit { + + @Override + public void visit(Node root) { + ArrayList> queue = new ArrayList>(); + queue.add(root); + while(!queue.isEmpty()) { + System.out.print(queue.get(0)); + queue.addAll(queue.get(0).getChildren()); + queue.remove(0); + } + } + +} diff --git a/Strategy/src/Main.java b/Strategy/src/Main.java new file mode 100644 index 0000000..eafb925 --- /dev/null +++ b/Strategy/src/Main.java @@ -0,0 +1,29 @@ + +public class Main { + + public static void main(String[] args) { + Node node = new Node(1); + Node node1 = new Node(2); + Node node2 = new Node(3); + Node node3 = new Node(4); + Node node4 = new Node(5); + Node node5 = new Node(6); + Node node6 = new Node(7); + Node node7 = new Node(8); + node.addChildren(node1); + node.addChildren(node2); + node1.addChildren(node3); + node1.addChildren(node4); + node2.addChildren(node5); + node2.addChildren(node6); + node3.addChildren(node7); + + ContextVisit contextVisit = new ContextVisit(node); + contextVisit.setVisit(new DeepVisit()); + contextVisit.visit(); + System.out.println(); + contextVisit.setVisit(new LevelVisit()); + contextVisit.visit(); + } + +} diff --git a/Strategy/src/Node.java b/Strategy/src/Node.java new file mode 100644 index 0000000..bd8192b --- /dev/null +++ b/Strategy/src/Node.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; + +public class Node { + + private T value; + + private ArrayList> children; + + @SuppressWarnings("unchecked") + public Node(T value){ + this.value = value; + children = (ArrayList>) new ArrayList(); + } + + public ArrayList> getChildren() { + return children; + } + + public T getValue() { + return value; + } + + public void addChildren(Node node) { + children.add(node); + } + + @Override + public String toString() { + return "[" + value + "]"; + } + +} diff --git a/Strategy/src/Visit.java b/Strategy/src/Visit.java new file mode 100644 index 0000000..4f933d4 --- /dev/null +++ b/Strategy/src/Visit.java @@ -0,0 +1,6 @@ + +public interface Visit { + + public void visit(Node root); + +}