Compare commits
3 Commits
fa5b8bf520
...
4af29e8f0f
Author | SHA1 | Date | |
---|---|---|---|
4af29e8f0f | |||
5220b27146 | |||
927a2232dd |
8
src/lab16_composite/ex1/Entity.java
Normal file
8
src/lab16_composite/ex1/Entity.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public interface Entity {
|
||||
void cry();
|
||||
void simulateInjury();
|
||||
void enterField();
|
||||
void shoot();
|
||||
}
|
33
src/lab16_composite/ex1/Game.java
Normal file
33
src/lab16_composite/ex1/Game.java
Normal file
@ -0,0 +1,33 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public class Game {
|
||||
public static void main(String[] args) {
|
||||
Player jo = new Player(1);
|
||||
Player jean = new Player(2);
|
||||
Player paul = new Player(3);
|
||||
jo.cry();
|
||||
jean.cry();
|
||||
jo.enterField();
|
||||
Team team1 = new Team();
|
||||
team1.add(jo);
|
||||
team1.add(jean);
|
||||
team1.add(paul);
|
||||
team1.enterField();
|
||||
team1.cry();
|
||||
team1.simulateInjury();
|
||||
Player martine = new Player(3);
|
||||
Player isabelle = new Player(4);
|
||||
Player mariePaule = new Player(5);
|
||||
|
||||
Team team2 = new Team();
|
||||
team2.add(martine);
|
||||
team2.add(isabelle);
|
||||
team2.add(mariePaule);
|
||||
team2.add(team1);
|
||||
team2.enterField();
|
||||
team2.cry();
|
||||
team2.simulateInjury();
|
||||
team2.remove(team1);
|
||||
team2.simulateInjury();
|
||||
}
|
||||
}
|
30
src/lab16_composite/ex1/Player.java
Normal file
30
src/lab16_composite/ex1/Player.java
Normal file
@ -0,0 +1,30 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
public class Player implements Entity {
|
||||
private int number;
|
||||
|
||||
public Player(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[Player " + number + "]";
|
||||
}
|
||||
|
||||
public void cry() {
|
||||
System.out.println(this + " ouin ouin");
|
||||
}
|
||||
|
||||
public void enterField() {
|
||||
System.out.println(this + " let's go !");
|
||||
}
|
||||
|
||||
public void simulateInjury() {
|
||||
System.out.println(this + " ouch !");
|
||||
}
|
||||
|
||||
public void shoot() {
|
||||
System.out.println(this + " encara Messi...");
|
||||
}
|
||||
}
|
44
src/lab16_composite/ex1/Team.java
Normal file
44
src/lab16_composite/ex1/Team.java
Normal file
@ -0,0 +1,44 @@
|
||||
package lab16_composite.ex1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Team implements Entity {
|
||||
private List<Entity> entities = new ArrayList<>();
|
||||
|
||||
public void add(Entity entity) {
|
||||
entities.add(entity);
|
||||
}
|
||||
|
||||
public void remove(Entity entity) {
|
||||
entities.remove(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cry() {
|
||||
for (Entity entity : entities) {
|
||||
entity.cry();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simulateInjury() {
|
||||
for (Entity entity : entities) {
|
||||
entity.simulateInjury();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterField() {
|
||||
for (Entity entity : entities) {
|
||||
entity.enterField();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shoot() {
|
||||
for (Entity entity : entities) {
|
||||
entity.shoot();
|
||||
}
|
||||
}
|
||||
}
|
74
src/lab16_composite/ex3/TestTraversalAlgorithms.java
Normal file
74
src/lab16_composite/ex3/TestTraversalAlgorithms.java
Normal file
@ -0,0 +1,74 @@
|
||||
package lab16_composite.ex3;
|
||||
|
||||
import lab16_composite.ex3.algorithm.BFSSearch;
|
||||
import lab16_composite.ex3.algorithm.DFSSearch;
|
||||
import lab16_composite.ex3.algorithm.TraversalAlgorithm;
|
||||
import lab16_composite.ex3.component.Component;
|
||||
import lab16_composite.ex3.composite.Composite;
|
||||
import lab16_composite.ex3.leaf.Leaf;
|
||||
|
||||
public class TestTraversalAlgorithms {
|
||||
public static void main(String[] args) {
|
||||
TestTraversalAlgorithms launcher = new TestTraversalAlgorithms();
|
||||
launcher.launch();
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
|
||||
Component treeRoot = this.createTreeStruct();
|
||||
|
||||
//Instanciating one searchAlgoritm of each type.
|
||||
TraversalAlgorithm dfs = new DFSSearch();
|
||||
TraversalAlgorithm bfs = new BFSSearch();
|
||||
|
||||
//Launching DFS search for one existing element.
|
||||
Component result = dfs.search(treeRoot, "Leaf 5");
|
||||
System.out.println("1 element found by DFS: " + result);
|
||||
|
||||
//Launching BFS search for one existing element.
|
||||
result = bfs.search(treeRoot, "Leaf 5");
|
||||
System.out.println("1 element found by BFS : " + result);
|
||||
}
|
||||
|
||||
//1. We create the tree.
|
||||
// node1
|
||||
// / \
|
||||
// node2 node3
|
||||
// / \ / \
|
||||
// l1 n4 n5 n6
|
||||
// /\ \ / \
|
||||
// l2 l3 l4 l5 l6
|
||||
//
|
||||
//Creating the leafs.
|
||||
private Component createTreeStruct() {
|
||||
Leaf l1 = new Leaf("Leaf 1");
|
||||
Leaf l2 = new Leaf("Leaf 2");
|
||||
Leaf l3 = new Leaf("Leaf 3");
|
||||
Leaf l4 = new Leaf("Leaf 4");
|
||||
Leaf l5 = new Leaf("Leaf 5");
|
||||
Leaf l6 = new Leaf("Leaf 6");
|
||||
|
||||
//Creating the nodes
|
||||
Composite n1 = new Composite("Node 1");
|
||||
Composite n2 = new Composite("Node 2");
|
||||
Composite n3 = new Composite("Node 3");
|
||||
Composite n4 = new Composite("Node 4");
|
||||
Composite n5 = new Composite("Node 5");
|
||||
Composite n6 = new Composite("Node 6");
|
||||
|
||||
//Assigning leaf and nodes to parents (nodes).
|
||||
n6.add(l5);
|
||||
n6.add(l6);
|
||||
n5.add(l4);
|
||||
n4.add(l2);
|
||||
n4.add(l3);
|
||||
n3.add(n5);
|
||||
n3.add(n6);
|
||||
n2.add(l1);
|
||||
n2.add(n4);
|
||||
n1.add(n2);
|
||||
n1.add(n3);
|
||||
|
||||
return n1;
|
||||
}
|
||||
}
|
13
src/lab16_composite/ex3/algorithm/BFSSearch.java
Normal file
13
src/lab16_composite/ex3/algorithm/BFSSearch.java
Normal file
@ -0,0 +1,13 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BFSSearch extends TraversalAlgorithm {
|
||||
|
||||
@Override
|
||||
protected void addSuccessor(ArrayList<Component> newSuccessors) {
|
||||
successors.addAll(newSuccessors);
|
||||
}
|
||||
}
|
12
src/lab16_composite/ex3/algorithm/DFSSearch.java
Normal file
12
src/lab16_composite/ex3/algorithm/DFSSearch.java
Normal file
@ -0,0 +1,12 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DFSSearch extends TraversalAlgorithm {
|
||||
@Override
|
||||
protected void addSuccessor(ArrayList<Component> newSuccessors) {
|
||||
successors.addAll(0, newSuccessors);
|
||||
}
|
||||
}
|
27
src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java
Normal file
27
src/lab16_composite/ex3/algorithm/TraversalAlgorithm.java
Normal file
@ -0,0 +1,27 @@
|
||||
package lab16_composite.ex3.algorithm;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TraversalAlgorithm {
|
||||
protected List<Component> successors = new ArrayList<>();
|
||||
|
||||
public Component search(Component root, String target) {
|
||||
successors = new ArrayList<>();
|
||||
successors.add(root);
|
||||
while (true) {
|
||||
if (successors.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Component component = successors.removeFirst();
|
||||
if (component.getName().equals(target)) {
|
||||
return component;
|
||||
}
|
||||
addSuccessor(component.getChildren());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void addSuccessor(ArrayList<Component> newSuccessors);
|
||||
}
|
23
src/lab16_composite/ex3/component/Component.java
Normal file
23
src/lab16_composite/ex3/component/Component.java
Normal file
@ -0,0 +1,23 @@
|
||||
package lab16_composite.ex3.component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Component {
|
||||
public abstract String getName();
|
||||
|
||||
public void add(Component c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void remove(Component c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Component getChild(int i) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public ArrayList<Component> getChildren() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
52
src/lab16_composite/ex3/composite/Composite.java
Normal file
52
src/lab16_composite/ex3/composite/Composite.java
Normal file
@ -0,0 +1,52 @@
|
||||
package lab16_composite.ex3.composite;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Composite extends Component {
|
||||
private String name;
|
||||
private final ArrayList<Component> children;
|
||||
|
||||
public Composite(String n) {
|
||||
name = n;
|
||||
children = new ArrayList<Component>();
|
||||
}
|
||||
|
||||
public void setName(String n) {
|
||||
name = n;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Component c) {
|
||||
children.add(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Component c) {
|
||||
children.remove(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getChild(int i) {
|
||||
return children.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Component> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String temp = "Composite " + name;
|
||||
for (Component n : this.children) {
|
||||
temp = temp + n.toString();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
30
src/lab16_composite/ex3/leaf/Leaf.java
Normal file
30
src/lab16_composite/ex3/leaf/Leaf.java
Normal file
@ -0,0 +1,30 @@
|
||||
package lab16_composite.ex3.leaf;
|
||||
|
||||
import lab16_composite.ex3.component.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Leaf extends Component {
|
||||
//Attributes
|
||||
|
||||
private final String name;
|
||||
|
||||
//Constructor
|
||||
public Leaf(String n) {
|
||||
name = n;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Leaf " + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Component> getChildren() {
|
||||
return new ArrayList<Component>();
|
||||
}
|
||||
}
|
5
src/learn/simple_composite/Component.java
Normal file
5
src/learn/simple_composite/Component.java
Normal file
@ -0,0 +1,5 @@
|
||||
package learn.simple_composite;
|
||||
|
||||
public interface Component {
|
||||
void operation();
|
||||
}
|
27
src/learn/simple_composite/Composite.java
Normal file
27
src/learn/simple_composite/Composite.java
Normal file
@ -0,0 +1,27 @@
|
||||
package learn.simple_composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Composite implements Component {
|
||||
private List<Component> children = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void operation() {
|
||||
for (Component component : children) {
|
||||
component.operation();
|
||||
}
|
||||
}
|
||||
|
||||
public void add(Component component) {
|
||||
children.add(component);
|
||||
}
|
||||
|
||||
public void remove(Component component) {
|
||||
children.remove(component);
|
||||
}
|
||||
|
||||
public Component getChild(int index) {
|
||||
return children.get(index);
|
||||
}
|
||||
}
|
8
src/learn/simple_composite/Leaf.java
Normal file
8
src/learn/simple_composite/Leaf.java
Normal file
@ -0,0 +1,8 @@
|
||||
package learn.simple_composite;
|
||||
|
||||
public class Leaf implements Component {
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("Leaf operation" + this);
|
||||
}
|
||||
}
|
18
src/learn/simple_composite/Main.java
Normal file
18
src/learn/simple_composite/Main.java
Normal file
@ -0,0 +1,18 @@
|
||||
package learn.simple_composite;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Composite root = new Composite();
|
||||
Composite node1 = new Composite();
|
||||
Composite node2 = new Composite();
|
||||
Leaf l1 = new Leaf();
|
||||
Leaf l2 = new Leaf();
|
||||
|
||||
root.add(node1);
|
||||
node1.add(node2);
|
||||
node2.add(l1);
|
||||
node2.add(l2);
|
||||
|
||||
root.operation();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user