AllDirectionsPercolatorwill be able to percolate in all four directions. This could be used to model a gas diffusing through a medium.
It would be straightforward to implement this new class by copying
GravitationalPercolator developed in the previous
Mini-Lab and making a slight change to
getPercolationLocations method. The drawback of this
approach is that it results in three different classes that are nearly
act method would be exactly the same in
all three and the
percolateTo method would be nearly the
same. The classes would differ substantially only in their
getPercolationLocation methods. This type of code
repetition leads to confusing programs that are difficult to maintain.
The solution is to move the shared methods into an abstract superclass
that all three Percolators will inherit from.
There is a small difference between
percolateTo methods of
VerticalPercolator percolates to a location it creates
GravitationalPercolator percolates to a new location it
creates a new
GravitationalPercolator. In order to
address this you will add a new abstract method to the superclass
duplicate which will return an object of
AbstractPercolator. All off the percolator classes
will need to override this method to generate an object of the
appropriate type. In this way, the
AbstractPercolator will be able to generate the new
object to add by using the
It will not need to know the actual type of the new object.
Exercise 1 — Refactoring VerticalPercolator and Gravitational Percolator:
Exercise 2 — Implementing AllDirectionPercolator:
The work you have done for this mini-lab will be part of the Percolation Programming Project, so you don't need to submit anything at this point. You can get started on the programming project any time after completing this Mini-Lab.