235.book Seite 3 Montag, 22. Dezember 2008 2:21 14 James Wood Object-Oriented Programming with ABAP™ Objects Bonn (cid:2) Boston 235.book Seite 5 Montag, 22. Dezember 2008 2:21 14 Contents at a Glance PART I Basics 1 Introduction to Object-Oriented Programming ........................... 23 2 Working with Objects ................................................................ 41 PART II Core Concepts 3 Encapsulation and Implementation Hiding ................................. 89 4 Object Initialization and Cleanup ............................................... 107 5 Inheritance ................................................................................. 127 6 Polymorphism ............................................................................ 155 7 Component-Based Design Concepts ........................................... 183 8 Error Handling with Exceptions .................................................. 201 9 Unit Testing with ABAP Unit ...................................................... 233 PART III Case Studies 10 Working with the SAP List Viewer .............................................. 257 11 ABAP Object Services ................................................................. 279 12 Working with XML ..................................................................... 303 13 Where to Go From Here ............................................................. 329 APPENDIX A Debugging Objects ..................................................................... 333 B The Author ................................................................................. 343 235.book Seite 7 Montag, 22. Dezember 2008 2:21 14 Contents Introduction ..................................................................................................... 15 PART I: Basics 1 Introduction to Object-Oriented Programming ........................ 23 1.1 The Need for a Better Abstraction .................................................. 23 1.2 Classes and Objects ........................................................................ 24 1.3 Establishing Boundaries .................................................................. 27 1.4 Reuse ............................................................................................. 29 1.4.1 Composition ...................................................................... 29 1.4.2 Inheritance ........................................................................ 30 1.4.3 Polymorphism .................................................................... 31 1.5 Object Management ....................................................................... 33 1.6 UML Tutorial: Class Diagram Basics ................................................ 33 1.6.1 Classes ............................................................................... 34 1.6.2 Attributes .......................................................................... 36 1.6.3 Operations ......................................................................... 37 1.6.4 Associations ....................................................................... 38 1.6.5 Notes ................................................................................. 39 1.7 Summary ........................................................................................ 39 2 Working with Objects ................................................................ 41 2.1 Syntax Overview ............................................................................. 41 2.1.1 Defining Classes ................................................................. 42 2.1.2 Declaring Components ....................................................... 42 2.1.3 Implementing Methods ...................................................... 51 2.2 Creating and Using Objects ............................................................. 52 2.2.1 Object References .............................................................. 52 2.2.2 Creating Objects ................................................................ 53 2.2.3 Object Reference Assignments ........................................... 53 2.2.4 Working with Instance Components .................................. 54 2.2.5 Working with Class Components ........................................ 57 2.2.6 Creating Complex Expressions Using Functional Methods ............................................................................ 61 7 235.book Seite 8 Montag, 22. Dezember 2008 2:21 14 Contents 2.3 Building Your First Object-Oriented Program .................................. 64 2.4 Getting Started with the Class Builder ............................................. 71 2.4.1 Class Pools ......................................................................... 71 2.4.2 Accessing the Class Builder ................................................ 71 2.4.3 Creating Classes ................................................................. 72 2.4.4 Defining Class Components ................................................ 74 2.4.5 Editing the Class Definition Section Directly ....................... 82 2.5 Case Study: Working with Regular Expressions ............................... 82 2.6 UML Tutorial: Object Diagrams ...................................................... 84 2.7 Summary ........................................................................................ 86 PART II: Core Concepts 3 Encapsulation and Implementation Hiding ............................... 89 3.1 Lessons Learned from the Procedural Approach .............................. 89 3.1.1 Decomposing Functional Decomposition ........................... 90 3.1.2 Case Study: A Procedural Code Library in ABAP ................. 91 3.1.3 Moving Toward Objects ..................................................... 94 3.2 Data Abstraction with Classes ......................................................... 94 3.3 Defining Component Visibilities ...................................................... 95 3.3.1 Visibility Sections ............................................................... 96 3.3.2 Friends ............................................................................... 99 3.4 Hiding the Implementation ............................................................. 101 3.5 Designing by Contract .................................................................... 102 3.6 UML Tutorial: Sequence Diagrams .................................................. 103 3.7 Summary ........................................................................................ 105 4 Object Initialization and Cleanup .............................................. 107 4.1 Creating Objects ............................................................................. 107 4.2 Controlling Object Initialization with Constructors .......................... 111 4.3 Taking Control of the Instantiation Process ..................................... 117 4.4 Garbage Collection ......................................................................... 121 4.5 Tuning Performance ....................................................................... 122 4.5.1 Design Considerations ........................................................ 123 4.5.2 Lazy Initialization ............................................................... 123 4.5.3 Reusing Objects ................................................................. 124 4.5.4 Using Class Attributes ........................................................ 124 8 235.book Seite 9 Montag, 22. Dezember 2008 2:21 14 Contents 4.6 UML Tutorial: State Machine Diagrams .......................................... 125 4.7 Summary ........................................................................................ 126 5 Inheritance .................................................................................127 5.1 Generalization and Specialization ................................................... 128 5.2 Inheriting Components ................................................................... 133 5.2.1 Designing the Inheritance Interface .................................... 133 5.2.2 Visibility of Instance Components in Subclasses ................. 135 5.2.3 Visibility of Class Components in Subclasses ....................... 136 5.2.4 Redefining Methods ........................................................... 136 5.2.5 Instance Constructors ......................................................... 138 5.2.6 Class Constructors .............................................................. 140 5.3 The Abstract and Final Keywords .................................................... 140 5.3.1 Abstract Classes and Methods ............................................ 140 5.3.2 Final Classes ....................................................................... 143 5.3.3 Final Methods .................................................................... 144 5.4 Inheritance Versus Composition ..................................................... 145 5.5 Using the Refactoring Assistant ....................................................... 148 5.6 UML Tutorial: Advanced Class Diagrams Part I ................................ 150 5.6.1 Generalization .................................................................... 150 5.6.2 Dependencies and Composition ......................................... 151 5.6.3 Abstract Classes and Methods ............................................ 152 5.7 Summary ........................................................................................ 153 6 Polymorphism ............................................................................155 6.1 Object Reference Assignments Revisited ......................................... 155 6.1.1 Static and Dynamic Types .................................................. 156 6.1.2 Casting ............................................................................... 158 6.2 Dynamic Method Call Binding ........................................................ 160 6.3 Interfaces ....................................................................................... 163 6.3.1 Interface Inheritance Versus Implementation Inheritance ... 164 6.3.2 Defining Interfaces ............................................................. 165 6.3.3 Implementing Interfaces .................................................... 167 6.3.4 Working with Interfaces ..................................................... 170 6.3.5 Nesting Interfaces .............................................................. 177 6.4 UML Tutorial: Advanced Class Diagrams Part II .............................. 180 6.4.1 Interfaces ........................................................................... 180 9 235.book Seite 10 Montag, 22. Dezember 2008 2:21 14 Contents 6.4.2 Providing and Required Relationships with Interfaces ........ 181 6.4.3 Static Attributes and Methods ........................................... 182 6.5 Summary ........................................................................................ 182 7 Component-Based Design Concepts ......................................... 183 7.1 Understanding the SAP Component Model .................................... 183 7.2 The Package Concept ..................................................................... 185 7.2.1 What Is a Package? ............................................................ 186 7.2.2 Creating and Organizing Packages Using the Package Builder ............................................................................... 188 7.2.3 Embedding Packages ......................................................... 191 7.2.4 Defining Package Interfaces ............................................... 192 7.2.5 Creating Use Accesses ........................................................ 194 7.2.6 Performing Package Checks ................................................ 195 7.2.7 Package Design Concepts ................................................... 196 7.3 UML Tutorial: Package Diagrams .................................................... 197 7.4 Summary ........................................................................................ 199 8 Error Handling with Exceptions ................................................. 201 8.1 Lessons Learned from Prior Approaches .......................................... 201 8.2 The Class-Based Exception Handling Concept ................................. 203 8.3 Dealing with Exceptions ................................................................. 205 8.3.1 Handling Exceptions .......................................................... 205 8.3.2 Cleaning up the Mess ......................................................... 209 8.4 Raising and Forwarding Exceptions ................................................. 210 8.4.1 System-Driven Exceptions .................................................. 211 8.4.2 The RAISE EXCEPTION Statement ...................................... 211 8.4.3 Propagating Exceptions ...................................................... 215 8.5 Creating Exception Classes .............................................................. 219 8.5.1 Understanding Exception Class Types ................................. 220 8.5.2 Local Exception Classes ...................................................... 221 8.5.3 Global Exception Classes .................................................... 221 8.5.4 Defining Exception Texts .................................................... 223 8.5.5 Mapping Exception Texts to Message IDs .......................... 227 8.6 UML Tutorial: Activity Diagrams ..................................................... 229 8.7 Summary ........................................................................................ 231 10 235.book Seite 11 Montag, 22. Dezember 2008 2:21 14 Contents 9 Unit Testing with ABAP Unit .....................................................233 9.1 ABAP Unit Overview ...................................................................... 234 9.1.1 The Need for Unit Testing Frameworks .............................. 234 9.1.2 Unit Testing Terminology ................................................... 235 9.1.3 Understanding How ABAP Unit Works ............................... 236 9.2 Creating Unit Test Classes ............................................................... 237 9.2.1 Test Attributes ................................................................... 238 9.2.2 Test Methods ..................................................................... 239 9.2.3 Managing Fixtures ............................................................. 239 9.2.4 Generating Test Classes for Global Classes .......................... 240 9.3 Case Study: Creating a Unit Test in ABAP Unit ................................ 241 9.4 Executing Unit Tests ....................................................................... 244 9.4.1 Integration with the ABAP Workbench .............................. 245 9.4.2 Integration with the Code Inspector ................................... 245 9.5 Evaluating Unit Test Results ............................................................ 246 9.6 Moving Toward Test-Driven Development ..................................... 247 9.7 UML Tutorial: Use Case Diagrams ................................................... 248 9.7.1 Use Case Terminology ........................................................ 249 9.7.2 An Example Use Case ......................................................... 249 9.7.3 The Use Case Diagram ....................................................... 251 9.7.4 Use Cases for Requirements Verification ............................ 252 9.7.5 Use Cases and Testing ........................................................ 252 9.8 Summary ........................................................................................ 253 PART III: Case Studies 10 Working with the SAP List Viewer ............................................257 10.1 Overview of the SAP Control Framework ........................................ 257 10.1.1 Control Framework Architecture ........................................ 258 10.1.2 Survey of Available Controls ............................................... 259 10.2 Overview of the ALV Object Model ................................................ 260 10.3 Getting Started with the Flight Query Report .................................. 261 10.3.1 Understanding the Report Requirements ............................ 261 10.3.2 Report Design Using the MVC Design Pattern .................... 261 10.3.3 Developing the Flight Model Class ..................................... 262 10.3.4 Developing the Report Controller Class .............................. 264 10.3.5 Implementing the Report View .......................................... 267 11 235.book Seite 12 Montag, 22. Dezember 2008 2:21 14 Contents 10.4 Event Handling with the ALV Object Model ................................... 271 10.4.1 Integrating Event Handler Methods into the Controller ...... 271 10.4.2 Registering Event Handler Methods ................................... 272 10.4.3 Responding to Events ........................................................ 273 10.4.4 Triggering Events on the Frontend ..................................... 274 10.4.5 Timing of Event Handling ................................................... 275 10.5 UML Tutorial: Communication Diagrams ........................................ 275 10.6 Summary ........................................................................................ 277 11 ABAP Object Services ................................................................ 279 11.1 Object-Relational Mapping Concepts ............................................. 280 11.2 Persistence Service Overview .......................................................... 280 11.2.1 Managed Objects ............................................................... 282 11.2.2 Mapping Concepts ............................................................. 284 11.2.3 Understanding the Class Agent API .................................... 285 11.3 Building Persistent Classes .............................................................. 286 11.3.1 Creating a Persistent Class in the Class Builder ................... 287 11.3.2 Defining Persistent Attributes with the Mapping Assistant ............................................................................ 288 11.3.3 Working with Object References ........................................ 292 11.4 Working with Persistent Objects ..................................................... 293 11.4.1 Creating New Persistent Objects ........................................ 294 11.4.2 Reading Persistent Objects Using the Query Service ........... 295 11.4.3 Updating Persistent Objects ............................................... 297 11.4.4 Deleting Persistent Objects ................................................ 298 11.5 UML Tutorial: Advanced Sequence Diagrams .................................. 298 11.5.1 Creating and Deleting Objects ........................................... 298 11.5.2 Depicting Control Logic with Interaction Frames ................ 300 11.6 Summary ........................................................................................ 301 12 Working with XML .................................................................... 303 12.1 XML Overview ............................................................................... 303 12.1.1 Why Do We Need XML? .................................................... 304 12.1.2 Understanding XML Syntax ................................................ 305 12.1.3 Defining XML Semantics .................................................... 307 12.2 XML Processing Concepts ............................................................... 309 12.2.1 Processing XML Using a Parser ........................................... 309 12 235.book Seite 13 Montag, 22. Dezember 2008 2:21 14 Contents 12.2.2 Modeling XML with the DOM ........................................... 310 12.2.3 Features of the iXML Library .............................................. 310 12.3 Case Study: Developing a Reading List ADT .................................... 311 12.4 Case Study: Building an XML Document ......................................... 314 12.5 Case Study: Reading an XML Document ......................................... 320 12.6 UML Tutorial: Advanced Activity Diagrams ..................................... 325 12.7 Summary ........................................................................................ 327 13 Where to Go From Here .............................................................329 APPENDIX A Debugging Objects ....................................................................333 A.1 Debugging Objects Using the Classic ABAP Debugger .................... 333 A.1.1 Displaying and Editing Attributes ....................................... 333 A.1.2 Tracing Through Methods .................................................. 336 A.1.3 Displaying Events and Event Handler Methods ................... 336 A.1.4 Viewing Reference Assignments for an Object .................... 337 A.1.5 Troubleshooting Class-Based Exceptions ............................ 338 A.2 Debugging Objects Using the New ABAP Debugger ....................... 340 B The Author ..................................................................................343 Index ................................................................................................................ 345 13 235.book Seite 15 Montag, 22. Dezember 2008 2:21 14 Introduction With all of the hype surrounding object-oriented programming, it can be difficult to separate the truth from fantasy. If you have picked up this book, it is likely that you have developed an interest in learning more about what the excitement is all about. This book provides the answers that you are looking for. The goal of this book is to teach you how to think about writing ABAP™ software from an object-oriented point-of-view. After reading this book, you will be equipped to work with many of the new and exciting ABAP-based technologies based on ABAP Objects such as Web Dynpro, ABAP Object Services, SAP® Busi- ness Workflow, and Web Services. Target Group and Prerequisites This book is intended for ABAP application developers that have some basic expe- rience writing ABAP programs using the ABAP Development Workbench. Basic ABAP language concepts are not covered in this book, so you have not worked with ABAP before, read ABAP Objects – ABAP Programming in SAP NetWeaver (SAP PRESS, 2007). Of course, in an introductory book such as this, no prior object-oriented experience is expected. The object-oriented extensions to the ABAP programming language (i.e., the Objects part of ABAP Objects) were made available in SAP R/3 4.6C. Therefore, you do not have to have the latest version of the SAP NetWeaver Application Server ABAP (AS ABAP) to start working with most of the object-oriented con- cepts described in this book. However, additions to the standard that were added in subsequent releases are pointed out where appropriate. If you want to reproduce the examples in the book and don’t have access to an AS ABAP instance, you can download a trial version from the SAP Developer Net- work (http://sdn.sap.com) that you can install on your local PC. From the main page, select Downloads (cid:2) Software Downloads (cid:2) SAP NetWeaver Main Releases. There, you will find several versions of the AS ABAP that you can install depending on your preferred operating system, and so on. Each download pack- 15
Description: