Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio authored and Fabrizio committed Jan 29, 2017
1 parent 42687bb commit 699b030
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Command/src/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion Command/src/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ExitCommand(Receiver receiver) {

@Override
public void execute() {
receiver.close();
receiver.exit();
}

}
6 changes: 3 additions & 3 deletions Command/src/receiver/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
6 changes: 6 additions & 0 deletions Strategy/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions Strategy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions Strategy/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Strategy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Strategy/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions Strategy/src/ContextVisit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class ContextVisit<T> {

private Visit<T> visit;

private Node<T> root;

public ContextVisit(Node<T> root) {
this.root = root;
}

public void setRoot(Node<T> root) {
this.root = root;
}

public void setVisit(Visit<T> visit) {
this.visit = visit;
}

public void visit() {
visit.visit(root);
}
}
11 changes: 11 additions & 0 deletions Strategy/src/DeepVisit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

public class DeepVisit<T> implements Visit<T> {

@Override
public void visit(Node<T> root) {
System.out.print(root);
for(Node<T> node: root.getChildren())
visit(node);
}

}
16 changes: 16 additions & 0 deletions Strategy/src/LevelVisit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.ArrayList;

public class LevelVisit<T> implements Visit<T> {

@Override
public void visit(Node<T> root) {
ArrayList<Node<T>> queue = new ArrayList<Node<T>>();
queue.add(root);
while(!queue.isEmpty()) {
System.out.print(queue.get(0));
queue.addAll(queue.get(0).getChildren());
queue.remove(0);
}
}

}
29 changes: 29 additions & 0 deletions Strategy/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

public class Main {

public static void main(String[] args) {
Node<Integer> node = new Node<Integer>(1);
Node<Integer> node1 = new Node<Integer>(2);
Node<Integer> node2 = new Node<Integer>(3);
Node<Integer> node3 = new Node<Integer>(4);
Node<Integer> node4 = new Node<Integer>(5);
Node<Integer> node5 = new Node<Integer>(6);
Node<Integer> node6 = new Node<Integer>(7);
Node<Integer> node7 = new Node<Integer>(8);
node.addChildren(node1);
node.addChildren(node2);
node1.addChildren(node3);
node1.addChildren(node4);
node2.addChildren(node5);
node2.addChildren(node6);
node3.addChildren(node7);

ContextVisit<Integer> contextVisit = new ContextVisit<Integer>(node);
contextVisit.setVisit(new DeepVisit<Integer>());
contextVisit.visit();
System.out.println();
contextVisit.setVisit(new LevelVisit<Integer>());
contextVisit.visit();
}

}
32 changes: 32 additions & 0 deletions Strategy/src/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.ArrayList;

public class Node<T> {

private T value;

private ArrayList<Node<T>> children;

@SuppressWarnings("unchecked")
public Node(T value){
this.value = value;
children = (ArrayList<Node<T>>) new ArrayList<T>();
}

public ArrayList<Node<T>> getChildren() {
return children;
}

public T getValue() {
return value;
}

public void addChildren(Node<T> node) {
children.add(node);
}

@Override
public String toString() {
return "[" + value + "]";
}

}
6 changes: 6 additions & 0 deletions Strategy/src/Visit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public interface Visit<T> {

public void visit(Node<T> root);

}

0 comments on commit 699b030

Please sign in to comment.