Booa8 Slide 07


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Booa8 Slide 07

    1. 1. Buliding Object-Oriented Applications in PowerBuilder Module 7: Encapsulation
    2. 2. Objectives <ul><li>Define encapsulation </li></ul><ul><li>Describe the techniques associated with encapsulation </li></ul><ul><li>Encapsulate properties, methods, functions, and window controls </li></ul>
    3. 3. Topics <ul><li>In troduction </li></ul><ul><li>Information Hiding </li></ul><ul><li>Encapsulating Properties </li></ul><ul><li>Encapsulating Methods </li></ul><ul><li>Window Controls and User Object Controls Encapsulation </li></ul><ul><li>Read / Write Access </li></ul>
    4. 4. Encapsulation <ul><li>Encapsulation is the process of defining a class of objects that: </li></ul><ul><ul><li>Perform behavior upon request using input data </li></ul></ul><ul><ul><li>Provide the results of that behavior as output data </li></ul></ul><ul><ul><li>Require no external knowledge of the internal methods </li></ul></ul>
    5. 5. Encapsulation 1. Clicked! 2. Send message to DataWindow control: &quot;Scroll to next page&quot;
    6. 6. Encapsulation — Information Hiding <ul><li>No direct access to manipulate properties (data) </li></ul><ul><li>Properties accessed only via methods </li></ul><ul><li>Object controls access to its data </li></ul><ul><li>&quot;Published&quot; functional interface to make use of the object's behavior </li></ul>
    7. 7. Encapsulation in PowerBuilder <ul><li>Instance variables declared private for the object </li></ul><ul><li>Data can be manipulated outside the object only through user-defined functions </li></ul><ul><li>Communication with the object occurs via user-defined events or user-defined functions </li></ul>
    8. 8. Encapsulation — Benefits <ul><li>Prevents unexpected and undesirable changes from external sources </li></ul><ul><li>No external impact from internal implementation changes </li></ul>
    9. 9. Encapsulating Properties — Implementation <ul><li>Encapsulate instance variables by defining their access as private or protected </li></ul><ul><li>Declare &quot;Get&quot; and &quot;Set&quot; functions for access </li></ul><ul><li>To make a property read-only, declare &quot;Get&quot; functions only </li></ul>
    10. 10. Encapsulating Properties <ul><li>Define access rights for instance variables: </li></ul><ul><ul><li>Public – Any scripts in the application (default) </li></ul></ul><ul><ul><li>Private – Scripts for the object for which the variable was declared </li></ul></ul><ul><ul><li>Protected – Scripts for the object and descendants of the object </li></ul></ul>
    11. 11. Encapsulating Properties
    12. 12. Encapsulating Properties — Access and Scope <ul><li>Instance variables default to public — can make private or protected </li></ul><ul><li>Global variables are always public </li></ul><ul><li>Shared variables are always private </li></ul><ul><li>PowerBuilder-defined properties are always public (for example, Window.X) </li></ul>
    13. 13. Encapsulating Properties <ul><li>Access to PowerBuilder-defined properties cannot be modified; they are always public </li></ul><ul><li>For example: </li></ul><ul><ul><li>x and y are properties of a window </li></ul></ul><ul><ul><li>Using Move(300, 200) sets their values and does not require knowledge of property names </li></ul></ul><ul><ul><li>But, x and y values can be directly set for a window: </li></ul></ul><ul><ul><ul><li>x = 300 </li></ul></ul></ul><ul><ul><ul><li>y = 200 </li></ul></ul></ul>
    14. 14. Encapsulating Methods <ul><li>Some user-defined methods should not be open to public access </li></ul><ul><li>Events are always public </li></ul><ul><li>Object functions can be made private or protected </li></ul><ul><li>Specify access rights in the object Function painter definition window </li></ul>
    15. 15. Encapsulating Methods — Function Access
    16. 16. Encapsulating Methods — Function Access <ul><li>Menu, window, and user-object functions can be private, protected, or public (default) </li></ul><ul><li>All PowerBuilder functions are public — no access modifiers </li></ul><ul><li>Globally defined user functions are always public — no access modifiers </li></ul><ul><li>Private functions are meaningful only to the internal behavior of objects; protected functions may need to be called from descendants </li></ul>
    17. 17. Encapsulating Methods Benefits <ul><li>Implementation of behavior can be changed or enhanced without affecting &quot;clients&quot; </li></ul><ul><li>Platform-dependent behavior is implemented through &quot;plug and play&quot; </li></ul><ul><li>Using encapsulated behavior does not require external knowledge of implementation </li></ul>
    18. 18. Encapsulation — Controls <ul><li>Window controls or user object controls are secondary classes </li></ul><ul><li>Secondary classes can: </li></ul><ul><ul><li>Be accessed only through the primary class, for example: w_cust.cb_ok.Hide( ) </li></ul></ul><ul><ul><li>Access private and protected instance variables of the primary class </li></ul></ul><ul><ul><li>Access private and protected functions of the primary class </li></ul></ul>
    19. 19. Read / Write Access <ul><li>Keyword restricting ability of scripts to read / write a variable's value — called operational access </li></ul><ul><li>Fine-tunes access rights </li></ul><ul><li>Declared with the instance variable declaration </li></ul><ul><li>Used for variables only, not functions </li></ul>
    20. 20. Read Access <ul><li>ProtectedRead </li></ul><ul><ul><li>Only scripts in object and descendants can read </li></ul></ul><ul><ul><li>Only valid if access right is public </li></ul></ul><ul><ul><li>public ProtectedRead integer ii_count </li></ul></ul><ul><li>PrivateRead </li></ul><ul><ul><li>Only scripts in object can read </li></ul></ul><ul><ul><li>Only valid if access right is public or protected </li></ul></ul><ul><ul><li>protected PrivateRead integer ii_maxcount </li></ul></ul>
    21. 21. Write Access <ul><li>ProtectedWrite </li></ul><ul><ul><li>Only scripts in object and descendants can write </li></ul></ul><ul><ul><li>Only valid if access right is public </li></ul></ul><ul><ul><li>public ProtectedWrite int ii_count </li></ul></ul><ul><li>PrivateWrite </li></ul><ul><ul><li>Only scripts in object can write </li></ul></ul><ul><ul><li>Only valid if access right is public or protected </li></ul></ul><ul><ul><li>protected PrivateWrite int ii_maxcount </li></ul></ul>
    22. 22. Read / Write Access <ul><li>Can be combined in declaration public PrivateRead PrivateWrite int ii_count </li></ul><ul><ul><li>Net effect: </li></ul></ul><ul><ul><ul><li>Compiler generates warning if there is an attempt to redefine access in the descendant </li></ul></ul></ul>
    23. 23. Summary <ul><li>Encapsulation is the process of incorporating properties and methods together in an object. </li></ul><ul><li>Information hiding limits access to an object's properties and implementation of its behavior. </li></ul><ul><li>PowerBuilder allows encapsulation, but it does not enforce it. </li></ul><ul><li>Access rights can be assigned to user functions and instance variables. </li></ul>
    24. 24. Summary <ul><li>Events are always public. </li></ul><ul><li>Operational access rights are used to fine-tune access to instance variables (for example, make instance variables public, but read-only). </li></ul>
    25. 25. Summary Questions
    26. 26. Lab Setup <ul><li>What you will need to do the lab: </li></ul><ul><ul><li>· Define a custom visual user class </li></ul></ul><ul><ul><li>· Encapsulate methods and properties in a custom visual user class </li></ul></ul><ul><ul><li>· Restrict access to properties </li></ul></ul>
    27. 27. Lab Debriefing <ul><li>Initialize the DataWindow control reference </li></ul><ul><li>Then use it. </li></ul>idw_control.ScrollToRow(1)
    28. 28. Lab Debriefing <ul><li>The report window uses the control </li></ul>uo_scroller.of_initdw( this) ue_postconstructor of Datawindow control
    29. 29. Lab Debriefing <ul><li>Discuss – what kinds of additional things could you have done in the of_initDW( ) </li></ul><ul><li>Could you have been assured that these were done if the variable idw_control had not been declared private? </li></ul>
    30. 30. Lab Debriefing <ul><li>Some Examples: </li></ul><ul><ul><li>Debugging – you can put a breakpoint on the of_initDW() </li></ul></ul><ul><ul><li>Logging – you could add code to log to a file </li></ul></ul><ul><ul><li>Validation – you could add code to validate </li></ul></ul><ul><ul><li>In Version 2 of your application, you could add an array of DataWindow controls </li></ul></ul><ul><li>If you had not declared your variable private, you could not be assured that some client was using the of_initDW() to initialize the reference. </li></ul>