Java Reference
In-Depth Information
inheritance to build up our algorithm. Let's look at how we would implement our
LoanAp-
plication
algorithm this way, in
Example 8-23
!
Example 8-23. The special case of an employee applying for a loan
public
public class
class
LoanApplication
LoanApplication
{
private
private final
final
Criteria identity
;
private
private final
final
Criteria creditHistory
;
private
private final
final
Criteria incomeHistory
;
public
public
LoanApplication
(
Criteria identity
,
Criteria creditHistory
,
Criteria incomeHistory
) {
this
this
.
identity
=
identity
;
this
this
.
creditHistory
=
creditHistory
;
this
this
.
incomeHistory
=
incomeHistory
;
}
public
public
void
void
checkLoanApplication
()
throws
throws
ApplicationDenied
{
identity
.
check
();
creditHistory
.
check
();
incomeHistory
.
check
();
reportFindings
();
}
private
private
void
void
reportFindings
() {
As you can see, instead of having a series of abstract methods we've got fields called
iden-
tity
,
creditHistory
, and
incomeHistory
. Each of these fields implements our
Criteria
functional interface. The
Criteria
interface checks a criterion and throws a domain excep-
tion if there's an error in passing the criterion. We could have chosen to return a domain class
from the
check
method in order to denote failure or success, but continuing with an excep-
tion follows the broader pattern set out in the original implementation (see
Example 8-24
)
.
Example 8-24. A Criteria functional interface that throws an exception if our application
fails
public
public interface
interface
Criteria
Criteria
{
public
public
void
void
check
()
throws
throws
ApplicationDenied
;
}