Java Reference
In-Depth Information
Figure 16.1. You can instantiate one of these classes to check a customer's credit,
depending on whether the online credit agency is up.
With the classes in Figure 16.1 you can provide credit limit information whether or not
the credit agency is online. The problem now is that the user of your classes needs to know
which class to instantiate. But you are the one who knows whether the agency is up!
In this scenario, you need to commit to the interface for creating an object but keep control of
which class to instantiate. One solution is to change both classes to implement a standard
interface and to create a factory method that returns an object of that type. Specifically, you
might
Make CreditCheck a Java interface that includes the creditLimit() method
Change both credit check classes to declare that they implement the CreditCheck
interface
Create a CreditCheckFactory class that provides a createCreditCheck()
method that returns an object whose type is CreditCheck
When you implement createCreditCheck() , you will use your knowledge of the credit
agency's status to decide which class to instantiate.
Search WWH ::




Custom Search