Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns.

Similar presentations


Presentation on theme: "Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns."— Presentation transcript:

1 Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns

2 Fall 2009ACS-3913 R. McFadyen2 Problem: How should we handle alternatives that are based on the class an object belongs to? We want to avoid the use of conditional If-then-Else logic Polymorphism Solution: Use polymorphic operations Methods in different classes are given the same name Different classes may be said to implement the same interface Subclasses in an inheritance hierarchy override method implementation of a superclass

3 Fall 2009ACS-3913 R. McFadyen3 Multiple tax calculators must be supported. Each tax calculator has its behaviour – behaviour varies by type of calculator Polymorphism > ITaxCalculatorAdapter getTaxes(Sale) : list of TaxLineItems TaxMasterAdapter getTaxes(Sale) : list of TaxLineItems GoodAsGoldTaxProAdapter getTaxes(Sale) : list of TaxLineItems The names are the same; the implementations differ

4 Fall 2009ACS-3913 R. McFadyen4 ITaxCalculatorAdapter is stereotyped as an interface. An interface is a set of operations – their names and parameters (signature) but no implementation. Implementation is done by the subclasses. Polymorphism > ITaxCalculatorAdapter getTaxes(Sale) : list of TaxLineItems

5 Fall 2009ACS-3913 R. McFadyen5 When calculator receives the message getTaxes, the method defined in the class TaxMasterAdapter is executed Polymorphism calculator:TaxMasterAdapter getTaxes(aSale) The concept of polymorphism arises in GoF patterns: Adapter, Command, Composite, etc.

6 Fall 2009ACS-3913 R. McFadyen6 Indirection Problem: Classes are highly coupled. How can we assign responsibilities to classes in order to keep coupling low? Solution: Assign responsibilities to an intermediate class … this provides a level of indirection Later on, we discuss GoF patterns: Adapter, Façade, and Observer. These incorporate the concept of Indirection

7 Fall 2009ACS-3913 R. McFadyen7 Indirection Figure 22.3: Objects send messages to the real tax calculators through an intermediary object (an adapter object) s:Sale:TaxMasterAdapter > : TaxMaster taxes := getTaxes( s ) t := getTotal()


Download ppt "Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns."

Similar presentations


Ads by Google