1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102
© 2009 Progress Software Corporation. All rights reserved. 2 A reference type (or specification) that specifies behavior an implementing class must provide A contract to which the implementing class must adhere Defines Methods Properties Grouped functionality Models multiple inheritance Interface – Standard Generic Definition BaseClass Implementer Interface
© 2009 Progress Software Corporation. All rights reserved. 3 A reference type (or specification) that specifies behavior an implementing class must provide A contract to which the implementing class must adhere Defines Methods Properties Grouped functionality Models multiple inheritance This doesnt tell one When or How to create them! Interface – Standard Generic Definition BaseClass Implementer Interface
© 2009 Progress Software Corporation. All rights reserved. 4 Why Use An Interface? Interfaces provide polymorphism across different types
© 2009 Progress Software Corporation. All rights reserved. 5 Why Use An Interface? || O>> Interfaces provide polymorphism across different types
© 2009 Progress Software Corporation. All rights reserved. 6 Why Use An Interface? Interfaces provide polymorphism across different types || O>> iPod VCR DVD 8-Track WMP
© 2009 Progress Software Corporation. All rights reserved. Use Cases For Interfaces Group common functionality across types Generic programming Class abstraction Client API 7
© 2009 Progress Software Corporation. All rights reserved. Distribute Common Functionality Interfaces define polymorphic behavior across disparate types Clients program to the contract of the interface The class fulfills the contract, decides the behavior 8
© 2009 Progress Software Corporation. All rights reserved. 9 Common Functionality INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). END INTERFACE.
© 2009 Progress Software Corporation. All rights reserved. 10 Common Functionality INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). END INTERFACE. BaseData Customer Form
© 2009 Progress Software Corporation. All rights reserved. 11 Common Functionality INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). END INTERFACE. IIterate BaseData Customer Form
© 2009 Progress Software Corporation. All rights reserved. 12 Common Functionality INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). END INTERFACE. getNextEntry() IIterate BaseData Customer Form
© 2009 Progress Software Corporation. All rights reserved. 13 Common Functionality DEFINE VARIABLE IIterRef AS IIterate. IIterRef = IIterRef:getNextEntry(). INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). END INTERFACE. IIterate BaseData Customer Form
© 2009 Progress Software Corporation. All rights reserved. Avoid Hierarchy Bloat Creating a base class with common functionality may introduce bloat. What happens when classes should not have a common base class but we want them to have common functionality? 14
© 2009 Progress Software Corporation. All rights reserved. 15 Polymorphism From Base Class METHOD Object getNextEntry() METHOD LOGICAL getDisplayValue() BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 16 Polymorphism From Base Class METHOD Object getNextEntry() METHOD LOGICAL getDisplayValue() BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 17 Polymorphism From Base Class METHOD Object getNextEntry() METHOD LOGICAL getDisplayValue() BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 18 Polymorphism From Base Class METHOD Object getNextEntry() METHOD LOGICAL getDisplayValue() BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 19 Polymorphism From Base Class CLASS Base: USING Progress.Lang.*. METHOD Object getNextEntry(): RETURN ?. END METHOD. METHOD LOGICAL getDisplayValue(): RETURN NO. END METHOD. /* and maybe others */ END CLASS.
© 2009 Progress Software Corporation. All rights reserved. 20 Polymorphism From Base Class CLASS Base: USING Progress.Lang.*. METHOD Object getNextEntry(): RETURN ?. END METHOD. METHOD LOGICAL getDisplayValue(): RETURN NO. END METHOD. /* and maybe others */ END CLASS. Base BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 21 Polymorphism From Base Class METHOD Object getNextEntry() METHOD LOGICAL getDisplayValue() BaseData Customer Form XMLData
© 2009 Progress Software Corporation. All rights reserved. 22 Polymorphism From Interfaces METHOD Object getNextEntry(). METHOD LOGICAL getDisplayValue(). IIterate IDisplay
© 2009 Progress Software Corporation. All rights reserved. 23 Polymorphism From Interfaces IIterate IDisplay BaseData Customer
© 2009 Progress Software Corporation. All rights reserved. 24 Polymorphism From Interfaces IIterate IDisplay BaseData Customer IIterate Form
© 2009 Progress Software Corporation. All rights reserved. 25 Polymorphism From Interfaces IIterate IDisplay BaseData Customer IIterate Form IDisplay XMLData
© 2009 Progress Software Corporation. All rights reserved. Clients Write Less/Easier Code Simplifies the client code Interfaces lower the dependency of the clients Clients need only know about the interface – Not the whole of all the types The value of abstraction –Implementations or hierarchies can change but will not impact clients 26
© 2009 Progress Software Corporation. All rights reserved. 27 Functionality Defined In A Class getDisplayValue() BaseData Customer XMLData
© 2009 Progress Software Corporation. All rights reserved. 28 METHOD PUBLIC LOGICAL displayInfo (INPUT objRef AS Progress.Lang.Object):
© 2009 Progress Software Corporation. All rights reserved. 29 METHOD PUBLIC LOGICAL displayInfo (INPUT objRef AS Progress.Lang.Object): DEFINE VARIABLE charVal AS CHARACTER INITIAL "". DEFINE VARIABLE plcRef AS CLASS Progress.Lang.Class. plcRef = objRef:getClass(). CASE plcRef:typeName: END CASE.... /* Do the work on charVal */...
© 2009 Progress Software Corporation. All rights reserved. 30 METHOD PUBLIC LOGICAL displayInfo (INPUT objRef AS Progress.Lang.Object): DEFINE VARIABLE charVal AS CHARACTER INITIAL "". DEFINE VARIABLE plcRef AS CLASS Progress.Lang.Class. plcRef = objRef:getClass(). CASE plcRef:typeName: WHEN "Customer" THEN charVal = CAST(objRef, "Customer"):getDisplayValue(). WHEN "XMLData" THEN charVal = CAST(objRef, "XMLData"):getDisplayValue(). END CASE.... /* Do the work on charVal */...
© 2009 Progress Software Corporation. All rights reserved. 31 Functionality Defined In Interface METHOD LOGICAL getDisplayValue(). BaseData Customer XMLData IDisplay
© 2009 Progress Software Corporation. All rights reserved. 32 METHOD PUBLIC LOGICAL displayInfo (INPUT objRef AS IDisplay):
© 2009 Progress Software Corporation. All rights reserved. 33 METHOD PUBLIC LOGICAL displayInfo (INPUT objRef AS IDisplay): DEFINE VARIABLE charVal AS CHARACTER. charVal = objRef:getDisplayValue().... /* Do the work on charVal */...
© 2009 Progress Software Corporation. All rights reserved. Service Defines Contract Designing interfaces from required functionality The service need not implement the interface Any class that wish to make use of the service is required to implement the interface 34
© 2009 Progress Software Corporation. All rights reserved. 35 METHOD PUBLIC CHARACTER getStringValue (INPUT InterRef AS ISerialize): DEFINE VARIABLE StringValue AS CHARACTER. StringValue = InterRef:getNameString() + "_" + STRING(InterRef:getUniqueID(), ">>9"). /* StringValue now looks like Name_001 */ RETURN StringValue.
© 2009 Progress Software Corporation. All rights reserved. 36 METHOD PUBLIC CHARACTER getStringValue (INPUT InterRef AS ISerialize): DEFINE VARIABLE StringValue AS CHARACTER. StringValue = InterRef:getNameString() + "_" + STRING(InterRef:getUniqueID(), ">>9"). /* StringValue now looks like Name_001 */ RETURN StringValue. INTERFACE ISerialize: METHOD PUBLIC CHARACTER getNameString(). METHOD PUBLIC INTEGER getUniqueID(). END INTERFACE.
© 2009 Progress Software Corporation. All rights reserved. Misconceptions Interfaces Do not limit additional behavior in the class Do not preclude programming to the class 37
© 2009 Progress Software Corporation. All rights reserved. 38 IIterate Definition INTERFACE IIterate: METHOD PUBLIC IIterate getNextEntry(). METHOD PUBLIC IIterate getPrevEntry(). DEFINE PROPERTY PUBLIC currentName AS CHARACTER GET. /* NO SET. */ END INTERFACE. IIterate
© 2009 Progress Software Corporation. All rights reserved. 39 Client Uses IIterate Reference /* CLIENT Snippet */ IIterRef = IIterRef:getPrevEntry(). CName = IIterRef:currentName. Form Customer IIterate
© 2009 Progress Software Corporation. All rights reserved. 40 Client Uses Customer Reference /* CLIENT Snippet */ IIterRef = CustRef:getPrevEntry(). CustRef:methodNotInInterface(). Form Customer IIterate
© 2009 Progress Software Corporation. All rights reserved. 41 Client Uses Form Reference /* CLIENT Snippet */ IIterRef = FormRef:getNextEntry(). FormRef:currentName = CName. Form Customer IIterate
© 2009 Progress Software Corporation. All rights reserved. 42 Client Uses Three Different References /* CLIENT Snippet */ IIterRef = IIterRef:getPrevEntry(). CName = IIterRef:currentName. IIterRef = CustRef:getPrevEntry(). CustRef:methodNotInInterface(). IIterRef = FormRef:getNextEntry(). FormRef:currentName = CName. Form Customer IIterate
© 2009 Progress Software Corporation. All rights reserved. Conclusion Define an interface to Group common functionality across types Allow classes to support multiple types Promote more generic programming Interfaces distribute polymorphic behavior across disparate types 43
44 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102