DEV-08: Exploring Object-oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

DEV-08: Exploring Object-oriented Programming

Description:

Exploring Object-oriented Programming Shelley Chase Development Architect Progress OpenEdge – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 52
Provided by: Shell142
Category:

less

Transcript and Presenter's Notes

Title: DEV-08: Exploring Object-oriented Programming


1
DEV-08 Exploring Object-oriented Programming
  • Shelley Chase
  • Development Architect Progress OpenEdge

2
Todays Agenda
  • Basic Principles of Object-oriented Programming
  • Types, Classes, Objects, and Interfaces
  • Inheritance, Polymorphism, and Delegation

3
What are Objects?
  • You interact with objects everyday
  • A customer
  • An order
  • All objects contains state and behavior
  • What they can do and what changes when they do
  • Software objects represent these as
  • Data ( like 4GL variables )
  • Methods ( like 4GL procedures)
  • Your car
  • The telephone

4
Object-oriented Programming
  • Object-oriented programming is a
    method of implementation in which
    programs are organized as cooperative
    collections of objects, each of which
    represents an instance of some class...
  • Grady Booch

5
Object-oriented Application Development
  • A way to design and build applications
  • Objects bundle together data (state) and methods
    (behavior)
  • Objects facilitate separating definition from
    implementation
  • Much more than just syntax
  • You might have already done object-oriented
    programming in the 4GL

6
Designing an Object-oriented Application
Object Order
Take an Order
Object Customer
Check Credit
Create a Customer
Object beOrder
Assign Salesperson
7
Basic Object-oriented Principles
  • Abstraction
  • Encapsulation
  • Hierarchies

8
Abstraction
Public View of an Object
  • Abstraction is used to manage complexity
  • Focus on the essential characteristics
  • Eliminate the details
  • Find commonalities among objects
  • Defines the public contract
  • Public definition for users of the object
  • The Outside view
  • Independent of implementation

9
Abstraction - Example
What should an Order object do?
InternalOrder
Two types of Orders
ExternalOrder
10
Encapsulation
Hide Implementation Details
  • Encapsulation hides implementation
  • Promotes modular software design data and
    methods together
  • Data access always done through methods
  • Often called information hiding
  • Provides two kinds of protection
  • State cannot be changed directly from outside
  • Implementation can change without
    affecting users of the object

11
Encapsulation - Example
  • Implementation Outside View

Public methods of Order class
CreateOrder UpdateOrder GetOrderTotal Next
CreateOrder( ) UpdateOrder( ) GetOrderTotal(
) Next( )
12
Encapsulation - Example continued
Hmm... Id like to change
CalculatePrice to CalculateTotalPrice
Object Order
orderNum AS INT custNum AS INT CalculatePrice(
)
PUBLIC CreateOrder( ) UpdateOrder(
) GetOrderTotal( ) Next( )
  • GetOrderTotal calls CalculatePrice( )

13
Encapsulation - Example continued
This change was easy because users of the object
will not be affected.
Object Order
orderNum AS INT custNum AS INT CalculateTotalPri
ce( )
PUBLIC CreateOrder( ) UpdateOrder(
) GetOrderTotal( ) Next( )
  • GetOrderTotal now calls CalculateTotalPrice( )

14
Hierarchies
Object Relationships
  • Define relationships between objects
  • Objects defined in terms of other objects
  • Allows state and behavior to be shared and
    specialized as necessary
  • Encourages code reuse
  • Two important hierarchy types
  • Inheritance
  • Aggregation

15
Hierarchies - Example
Order uses ShipInfo (Aggregation)
Order
references
is a
is a
ExternalOrder
ShipInfo
InternalOrder
InternalOrder and ExternalOrder inherit from
Order (Inheritance)
16
Summary Object-oriented Principles
  • Abstraction
  • Break up complex problem
  • Focus on public view, commonalities
  • Encapsulation
  • Hide implementation details
  • Package data and methods together
  • Hierarchies
  • Build new objects by combining or extending other
    objects

17
Todays Agenda
  • Basic Principles of Object-oriented Programming
  • Types, Classes, Objects, and Interfaces
  • Inheritance, Polymorphism, and Delegation

18
Type
A Type is a definition
  • A Type defines the state and behavior
  • Identifies inheritance relationships with other
    types
  • No concern for implementation
  • Enables strong-typing
  • Early binding - types determined at compile time
  • Type-consistency enforced at compile time and
    runtime

19
Type - Example
  • Types
  • Order
  • InternalOrder
  • Subtype of Order
  • ExternalOrder
  • SubType of Order

Order
is a
is a
ExternalOrder
InternalOrder
A subtype can appear anywhere a super type is
expected
20
Benefits of Types (Strong-Typing)
  • Compile time checking for type consistency
  • myObj mySubObject. (must be subType)
  • myObjmethod(). (validates signature)
  • myObjdata 3. (validates data type)
  • Results in safer, bug-free code because all code
    paths checked at compile time

21
Class
A Class implements a Type
  • A Class defines and implements a
    user-defined type
  • A Class is a template (blueprint) for an object
  • Data
  • Methods
  • Relationships to other classes

Methods Data
22
Object
An Object is an instance of a Class
  • An Object is created at runtime
  • Maintains independent state in data members
  • Code shared among object instances
  • The term Object is often used to refer to both
    classes and instances

23
Interface
An Interface implements a Type
  • An Interface is a collection of method
    definitions for a set of behaviors a
    contract
  • No implementation provided
  • A Class can implement an interface
  • Must implement all methods in the interface
  • Behavior can be specialized
  • Compiler validates implementation of interface

24
Interface - Example
  • Class Order implements IList interface

Interface IList
PUBLIC Next( )
Compiler checks for method definition in
implementing class
25
Interface Example continued
  • Write generic routine using interface
  • MoveNext( listObj AS IList )
  • listObj.Next( ). / Calls method
  • in real object /
  • Call with any object that implements IList
  • myOrder NEW Order( ). / implements IList
    /
  • MoveNext( myOrder ).
  • or
  • myCust NEW Customer( ). / implements IList /
  • MoveNext( myCust ).

26
Benefits of Interfaces
  • Allows many different classes to be treated in a
    like manner
  • All classes that implement an interface are
    guaranteed to have same set of methods
  • Enables generic programming
  • IList example allows any collection of objects to
    be navigated using Next( )
  • Behavior can be specialized as needed

27
Summary Object-oriented Constructs
  • Type
  • Enforces type consistency at compile time
  • Class
  • Defines type with data and methods and provides
    implementation
  • Object
  • Runtime instantiation of class
  • Interface
  • Defines type with methods no implementation
    provided

28
Todays Agenda
  • Basic Principles of Object-oriented Programming
  • Types, Classes, Objects, and Interfaces
  • Inheritance, Polymorphism, and Delegation

29
Inheritance
Relationship between Classes
Super Class
  • Super Class (Base class)
  • Provides common functionality and data members
  • Subclass (Derived class)
  • Inherits public and protected members from the
    super class
  • Can extend or change behavior of super class by
    overriding methods


Subclass
30
Access Levels for Class Members
  • PRIVATE members available
  • Only within the class
  • PROTECTED members available
  • Within the class
  • Within the class hierarchy
  • PUBLIC members available
  • Within the class
  • Within the class hierarchy
  • To users outside the class

31
Inheritance Example
Class Order
PRIVATE orderNum AS INT custNum AS
INT CalculateTotalPrice( )
PROTECTED GetCredit( ) PUBLIC CreateOrder(
) UpdateOrder( ) GetOrderTotal( ) Next( )
32
Inheritance Example
  • Class InternalOrder inherits Order

Class Order
PRIVATE orderNum AS INT custNum AS
INT CalculateTotalPrice( )
InternalOrder
PROTECTED GetCredit( ) PUBLIC CreateOrder(
) UpdateOrder( ) GetOrderTotal( ) Next( )
PROTECTED GetCredit( ) PUBLIC CreateOrder(
) UpdateOrder( ) GetOrderTotal( ) Next( )
PROTECTED GetCredit( ) PUBLIC CreateOrder(
) UpdateOrder( ) GetOrderTotal( ) Next( )
33
Inheritance and Method Overriding
  • Method overriding used to specialize behavior
  • Subclass may override a method in its super class
    (hierarchy)
  • Method signatures must match
  • Overriden method can
  • Completely override behavior of super class
  • Augment behavior by providing its own behavior
    and calling super class method

34
Method Overriding Example 1
Order
PROTECTED GetCredit( ) credit
FindCreditScore( ).
  • Class InternalOrder inherits Order

InternalOrder
PROTECTED GetCredit ( ) credit -1.
/unlimited/
35
Method Overriding Example 2
Order
PROTECTED GetCredit( ) credit
CalculateCredit( ).
  • Class ExternalOrder inherits Order

ExternalOrder
PROTECTED GetCredit( ) credit
SUPERGetCredit( )
extraMoney.
36
Benefits of Inheritance and Overriding
  • Inheritance supports modular design
  • Common behavior put in super class and used by
    subclass
  • Subclass can override to specialize behavior
  • Inheritance is strongly-typed
  • InternalOrder myOrder NEW InternalOrder.
  • myOrder.GetCredit( ).

myOrder knows it is an InternalOrder
37
Polymorphism
One interface, many implementations
  • Execution of an overridden method in a subclass
    from a reference to a super class
  • superclassmethod( )
  • Code written using super class
  • Tightly coupled to inheritance and overriding
  • Super class used at compile time, subclass
    assigned at runtime
  • Method call on super class dispatched to
    subclass method at runtime

38
Polymorphism Example
Order
PROTECTED GetCredit( ) credit
CalculateCredit( ).
InternalOrder
ExternalOrder
PROTECTED GetCredit ( ) credit -1.
/unlimited/
PROTECTED GetCredit( ) credit
SUPERGetCredit( )
extraCreditPoints.
39
Polymorphism Example continued
DEFINE myOrder AS Order. if (bInternalCust
TRUE) myOrder NEW InternalOrder( ). else
myOrder NEW ExternalOrder( ). myOrderGetCred
it( ).
Super Class reference
Calls InternalOrderGetCredit( ) or
ExternalOrderGetCredit( )
40
Benefits of Polymorphism
  • Supports generic programming using super class or
    interface
  • Type used at compile time is super class or
    interface
  • Specialized behavior is called at runtime
    automatically
  • Built on inheritance and overriding

41
Delegation
  • Delegation is the use of other objects
    within a class
  • Class forwards method calls to the
    contained object
  • Class wraps the delegate object
  • Creates an instance of the object
  • Defines a stub method for any referenced
    methods that should be public
  • No access to protected or private members

42
Delegation Example
  • Class Order references a ShipInfo object

ShipInfo
PRIVATE id shipdate promisedate PUBLIC SetDate
( ) GetDate( )
calls
43
Benefits of Delegation
  • Delegation supports modular design
  • Purposed class does work
  • Class uses delegate to provide needed
    functionality
  • Class can determine what to put in API
  • With inheritance super class dictates API with
    delegation wrapper class decides what to expose

44
Thats Object-oriented ProgrammingWhat did we
learn
45
Terminology / Concept Review
  • Abstraction Public API
  • Encapsulation Hide implementation details
  • Hierarchy Relationships between classes
  • Strong-typing Type consistency enforced
  • Class Data members and methods
  • Object Runtime instance of a class
  • Interface Set of method definitions contract
  • Inheritance Inherit/specialize from super
    class
  • Polymorphism Most-derived method called
    from super class reference
  • Delegation Other objects do the work

46
Benefits of OO Programming
  • Promotes modular design
  • Data and methods that operate on that data are
    contained in one place
  • Commonalities put into super classes
  • Code reuse through hierarchies
  • Inheritance and delegation
  • Strong-typing
  • Compile-time type checking
  • Runtime type checking

47
Recommended OO Books
  • Object-Oriented Analysis and Design with
    Applications (2nd Edition) by Grady Booch
  • Object-Oriented Modeling and Design by James R
    Rumbaugh
  • Design Patterns, Elements of Reusable
    Object-oriented Software by Erich Gamma

48
In Summary
  • Object-oriented programming is more than syntax
    must be part of design
  • Many benefits in OO Programming
  • Can be combined with procedural programming, not
    all or nothing
  • OpenEdge 10.1 Language enhancements support
    object-oriented programming naturally
  • Attend Session DEV-10 for details

49
Questions?
50
Thank you for your time!
51
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com