AllDirectionsPercolator will 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
the GravitationalPercolator developed in the previous
Mini-Lab and making a slight change to
the getPercolationLocations method. The drawback of this
approach is that it results in three different classes that are nearly
identical: the 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
the percolateTo methods of
the VerticalPercolator and
the GravitationalPercolator: When
a VerticalPercolator percolates to a location it creates
a new VerticalPercolator. When
a 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
called duplicate which will return an object of
type AbstractPercolator. All off the percolator classes
will need to override this method to generate an object of the
appropriate type. In this way, the percolateTo method
of AbstractPercolator will be able to generate the new
object to add by using the duplicate method.
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.