12.07.2015 Views

DEV475 Mastering Object-Oriented Analysis and Design with UML ...

DEV475 Mastering Object-Oriented Analysis and Design with UML ...

DEV475 Mastering Object-Oriented Analysis and Design with UML ...

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Module 2 - Concepts of <strong>Object</strong> OrientationExample: PolymorphismExample: PolymorphismfinancialInstrument.getCurrentValue()getCurrentValue()getCurrentValue()getCurrentValue()Stock Bond Mutual Fund<strong>Mastering</strong> <strong>Object</strong> <strong>Oriented</strong> <strong>Analysis</strong> <strong>and</strong> <strong>Design</strong> <strong>with</strong> <strong>UML</strong>Copyright © 2003 Rational Software, all rights reserved 29In this example, a requesting object would like to know the current value of afinancial instrument. However, the current value for each financial instrument iscalculated in a different fashion. The stock needs to determine the current askingprice in the financial market that it is listed under. The bond needs to determine thetime to maturity <strong>and</strong> interest rates. A mutual fund needs to look up the closing pricefor the day from the fund management company.In a non object-oriented development environment, we would write code that maylook something like this:IF financialInstrument = Stock THENcalcStockValue()IF financialInstrument = Bond THENcalcBondValue()IF financialInstrument = MutualFund THENcalcMutualFundValue()With object technology, each financial instrument can be represented by a class, <strong>and</strong>each class will know how to calculate its own value. The requesting object simplyneeds to ask the specific object (for example, Stock) to get its current value. Therequesting object does not need to keep track of three different operation signatures.It only needs to know one. Polymorphism allows the same message to be h<strong>and</strong>led indifferent ways, depending on the object that receives it.2 - 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!