java-algorithms-implementation
java-algorithms-implementation copied to clipboard
Report on possible code improvements applying design patterns
Hi, due to academic issues I have reviewed your code and I have detected possible design patterns that can be implemented in order to improve the appearance of the code.
Strategy Pattern
A new interface called "WithOrigin" is created which refers to methods specific to shortest path algorithms of a graph considering an initial vertex and an end.
The Dijkstra and Bellman-Ford classes represent concrete classes that implement the interface described above. Each class will perform its own implementation of the methods provided by the interface.
In addition, the pattern gives rise to a new class called "ShortestWay" that will represent a handler for these algorithms. It communicates directly with the client and provides it with a kind of facade (not considering the pattern) that allows to choose the algorithm of interest.
The class diagram would be as follows:
Decorator Pattern
For this solution an interface called Node<T> was created that will be joined with the abstract class LinkedList<T> with a composition relation of multiplicity 2. The concrete classes called SinglyNode<T> and DoublyNode<T> will implement the Node<T> interface.
It should be emphasized that with this solution the List<T> file, original of the analyzed code is segregated into other classes. Thus, in order to provide greater modularity.
The ArrayList<T> and LinkedList<T> classes, including the latter's child classes, are separated into separate files.
LinkedList<T> child classes.
The class diagram would be as follows:
Iterator Pattern
For this solution, an extra operation is added to the IGraph interface called createIterator() that will allow choosing the type of path of interest.
An Iterator interface is added with the operations used by the traversal algorithms. The concrete classes BreadthFirst and DepthFirst implement this interface.
Iterator<T> interface.
Concrete classes that implement the interface.
The class diagram would be as follows: