< Day Day Up > |
Now let's consider adding more application functionality to the Loan class hierarchy using multiple interfaces. The loan calculator has basic functionality for giving customers estimates of actual loan payments. It would also be helpful if a customer could complete a loan application, print the application, and submit the application to the bank using our code. To facilitate this, we can use our existing loan calculator application and add the functionality using interfaces. Listing 6.7 shows the Printable interface and the application interface that can be used by the loan object hierarchy. Listing 6.7. Interfaces for the LoanCalc Exampleinterface Printable{ function print(); } interface Application{ function fillOut(); function sign(); function submit(); } The Loan class can implement both interfaces, as shown in Listing 6.8. By implementing this functionality in the Loan class, we ensure that all the Loan descendants have the printing and application functionality. Listing 6.8. Using Multiple Interfaces with the LoanCalc Exampleclass Loan implements Printable, Application { function print( ) { trace("Loan printing: "+this); //using the interface here //do some formatting and print } function fillOut(){ /*show a form and collect info*/ } function sign(){ /*sign (electronic) the form*/ } function submit(){ /*submit the form*/ } } function calc(loan:Loan){ //function called by the button handler //calculate the monthly payment var monthlyPmtVal:Number = loan.getMonthlyPayment(); //display monthly payment monthlyPmt.text = monthlyPmtVal; var totalPmtVal:Number = loan.getTotalPayment(); //display total payments totalPmt.text = totalPmtVal; //print the loan this.print(loan); this.apply(loan); } function print( item:Printable ){ trace ("in loancalc.print"); item.print(); } function apply( item:Application ){ trace ("in loancalc.print"); item.fillOut(); item.sign(); item.submit(); } |
< Day Day Up > |