# Store three search algorithms in a list (as method calls) and iterate over them in main?

Briefing: I have implemented three search algorithms to break a lock. A lock can be broken by the actions shaking, pulling, pulling, or poking. These are 4 methods can be applied to the given lock. This lock has a length anywhere from 1 to 16, meaning that if the length was 16, 16 back to back actions in the correct order will need to be done. For example, a length two lock that can be unlocked by pulling and then poking will need to be pulled and then poked. The data structure devised to solve this is a tree where each parent has 4 children that correspond to actions. These search algorithms climb the tree in many ways to find the correct solution to break a given lock.

Set-up of Solution & Problem: I have one class called Tree that has 3 Search algorithms: Breadth-First, Depth-Limited, and Iterative-Deepening Search. In this same class, I have 2 helper methods to help each algorithm break a lock combination (check, which checks if the sequence of actions up to the child being viewed is a solution, and depth, which determines the depth of a given child). I also have a Node class that is used by Tree to create the root and subsequent children. Now, I want to store each algorithm in an array, so that I can iterate over each algorithm, and collect data for each algorithm from a main function. I have looked a little into the Command Pattern regarding Polymorphism. It seems that it might work, but I am confused on how I would have to organize my current solution to adapt. Would I need to turn each algorithm methodintoPerhaps there is a better solution than the Command Pattern. I can create a main in the Tree and simply call each algorithm there, but that seems a bit “sloppy” to me. Any suggestions?

The code below is just to get a gist of my current format. I have would rather simplify the code to show organization to best get at how I can adapt to utilize something like the Command Pattern to store each algorithm in an array to iterate over each and collect certain data.

```public class Tree   {

Node root = new Node(0, null);
TheLock lock =  new TheLock("Michael");

Tree()
{this.root = root;}

public int runBST(TheLock lock){
}
public int it2runIDS(TheLock lock){
}

public int runDLS(int depthlim, TheLock lock){
}

public boolean check(Node child,TheLock lock) {
}

public int depth(Node child, int currd) {
}
}

public class Node {

int action;

Node parent;

public Node(int action, Node parent) {

this.action = action;

this.parent = parent;
}
}
```

## Answer

I would suggest the Visitor pattern, if you had to pick a specific one. Each algorithm will visit one lock and try to break it.

The following approach is not exactly an implementation of said pattern, but it shows one way you can store a list of objects with a similar method

Start with an Interface, and some model object to hold state of a Lock

```public interface LockAlgorithm {
// returns true if lock is broken
boolean break(Lock lock);
}
```

Some implementation, repeat for other types

```public class  BFSLockAlgorithm implements LockAlgorithm {
@Override
public boolean break(Lock lock) {
return false;  // TODO: implement
}
}
```

Then, you store a list of interface implementations to loop over and apply on some Lock object

```// in main
List<LockAlgorithm> algos = Arrays.asList(new BFSLockAlgorithm());
Lock l = new Lock("data");
for (LockAlgorithm a : algos) {
if (l.isLocked()) {
if (a.break(l)) System.out.print("success");
}
}
if (l.isLocked()) {
System.out.print("failed");
}
```

The inverted way to implement the link above would be to allow for `boolean Lock.unlockWith(LockAlgorithm a)`

Don’t forget to do adequate unit testing for each LockAlgorithm first