James Wood Object-Oriented Programming with ABAP Objects vp- pRESS Object-Oriented Programming I with ABAP Objects I Bonn • Boston Introduction With all of the hype surrounding object-oriented programming, it can be difficult to separate the truth from fantasy. Ifyou 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 ABAP5" 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. Ifyou 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 0http://sdn.sap.com) that you can install on your local PC. From the main page, select DOWNLOADS • SOFTWARE DOWNLOADS • 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 Introduction age comes with a set of instructions to help you get started. The SAP Developer Network forums can also provide useful tips if you run into problems. Structure of the Book In many ways, this was a veiy difficult book to write. Doing a topic like this jus- tice requires a healthy balance between theoretical and practical concepts, so many practical examples are included that illustrate theoretical concepts. The first part of the book helps get you started quickly by describing basic object- oriented concepts using a series of simple object-oriented programs. The second part of the book covers core object-oriented concepts such as encapsulation, inheritance, and polymorphism. The final part of the book teaches you how to apply these concepts using the tools and services available in the SAP NetWeaver Application Server. The end of each chapter includes a brief tutorial on the Unified Modeling Lan- guage (UML). These tutorials show you how to express your object-oriented designs using a graphical notation that is commonly used throughout the indus- try. In detail, the chapters provide the following content: • Chapter 1: Introduction to Object-Oriented Programming Object-oriented programming is steeped in theoiy. Therefore, before we delve into the creation of classes, we need to review this theoiy so that you can understand how eveiything fits together. The concepts described in this chap- ter provide you with the foundation you need to start developing classes. • Chapter 2: Working with Objects This chapter reinforces the theoretical concepts covered in Chapter 1 by allow- ing you to get your hands dirty by creating some simple object-oriented pro- grams written in ABAP Objects. Here, we will spend a lot of time looking at the ABAP Objects syntax for defining classes, methods, and so on. This syntax is highlighted by a series of examples that illustrate how classes can be used in practical situations. • Chapter 3: Encapsulation and Implementation Hiding This chapter introduces you to two important concepts in object-oriented design: encapsulation and implementation hiding. First, the importance of these concepts is demonstrated by observing some problems with code librar- 16 Introduction ies developed using procedural methods. Then, you will learn how to avoid these problems in class libraries through the use of access specifiers. Finally, we will take a step back and look at ways to develop reusable classes using a technique called design-by-contract. Chapter 4: Object Initialization and Cleanup This chapter walks you through the lifecycle of objects from creation to dele- tion. Along the way. you will learn how to interact with this process to maxi- mize performance and improve the integrity of your designs. Chapter 5: Inheritance One of the potential side effects of good object-oriented designs is the ability to reuse code. In this chapter, you will learn how to reuse classes using the con- cept of inheritance. We will also consider an alternative form of class reuse known as composition. Chapter 6: Polymorphism This chapter shows you how to exploit inheritance relationships described in Chapter 5 using a technique referred to as polymorphism. This discussion is highlighted by the introduction of interfaces, which are pure elements of design. Chapter 7: Component-Based Design Concepts After covering the basics of object-oriented programming in Chapters 1-6, this chapter broadens the focus a bit by showing you how the ABAP Package Con- cept can be used to organize your class libraries into coarse-grained develop- ment components. Chapter 8: Error Handling with Exceptions This chapter explains how to deal with exceptions in your classes and pro- grams using the ABAP class-based exception handling concept. Chapter 9: Unit Testing with ABAP Unit This chapter shows you how to develop automated unit tests using the ABAP Unit test framework. These tests help you ensure that your classes deliver on the functionality described in their API contracts. Chapter 10: Working with the SAP List Viewer This chapter is the first of three case study chapters that show you how ABAP Objects classes can be used in many common development tasks. In this chap- ter, you see how to create interactive reports using the new object-oriented Introduction ALV Object Model. This chapter also provides a practical example for working with events in ABAP Objects. • Chapter 11: ABAP Object Services This chapter demonstrates the use of the services provided by the ABAP Object Services framework. In particular, you will learn how to use these services to develop persistent classes whose state can be stored in the database without having to write a single line of SQL. • Chapter 12: Working with XML This chapter concludes the case study series by showing you how to work with XML documents using the object-oriented iXML library provided with the SAP NetWeaver Application Server. This discussion also provides you with an opportunity to develop an abstract data type that uses most of the concepts described throughout the course of the book. • Chapter 13: Where to Go From Here In this final chapter, we will look ahead to see how to apply the object-oriented concepts learned in this book in real-world projects. • Appendix: Debugging Objects In this appendix, we will look at how to use the ABAP Debugger tool to debug object-oriented programs. Conventions This book contains many examples demonstrating syntax, functionality, and so on. Therefore, to distinguish these sections, we use a font similar to the one used in many integrated development environments to improve code readability: CLASS lcl.test DEFINITION. PUBLIC SECTION. ENDCLASS. As new syntax concepts are introduced, these statements are highlighted using a bold listing font (i.e., the PUBLIC SECTION statement in the preceding code snip- pet). 18 Introduction Acknowledgments I am a firm believer in the saying "you are what you read." As such, I am indebted to so many great authors whose works have planted the seeds from which this book took form. I have similarly been fortunate enough to have the opportunity to work with so many talented software development professionals who have taught me so much. It is my sincere hope that this work represents a small token of my appreciation for all of their hard work and dedication to the field. I would like to thank Dr. Stephen Yuan and Dr. Jason Denton for opening my eyes to the world of software engineering. I would also like to thank Russell Sloan at IBM and Colin Norton at SAP America for giving me a chance to spread my wings. Much of the inspiration from this book came during my time working on a project at Raytheon. A special thanks to the good people there who allowed me to play "mad scientist" with their development methodology. I am also grateful for the fresh perspective offered by members of the OneAero development team at Lockheed Martin. In particular, I would like to thank Greg Hawkins from SAP America, who graciously offered valuable insight whenever I needed someone to look at this from a different angle. To my editor, Stefan, thank you so much for your support throughout this pro- cess. I would not have been able to do this without you. To my Dad. and my Mom who still corrects my grammar to this day, thanks for always being there for me. To my children, Andersen and Paige, thank you for all of your love and support. I will always cherish our writing breaks playing on the floor in my office. To my wife Andrea. I just want to say how much I feel loved and supported by you. Without your influence on my life, I would not be where I am today. I con- sider myself very fortunate to have met someone as special as you. And finally, to Him who is, and was, and is to come again: Soli Deo Gloria. James Wood Principal SAP NetWeaver Software Consultant, Bowdark Consulting, Inc.. Flower Mound. TX 19 PART I Basics I This chapter provides an overview of object-oriented programming from a conceptual point of view. The concepts described in this chapter lay the foundation for the remainder of the book. 1 Introduction to Object-Oriented Programming Object-oriented programming (OOP) is a programming methodology that is used to simplify software designs to make them easier to understand, maintain, and reuse. Like procedural programming before it. OOP represents a different way of thinking about writing software. The beauty of OOP lies in its simplicity. As you will see, the expressiveness of OOP makes it easier to deliver quality software components on time and under budget. The purpose of this chapter is to introduce you to the basic concepts that you need to understand to effectively design and develop object-oriented programs. These concepts apply to most modern OOP languages such as C++, Java, and, of course, ABAP Objects. This chapter also begins an introduction to the Unified Modeling Language (UML), which is the de facto object modeling language used in the industry today. 1.1 The Need for a Better Abstraction The most important goal for any software development project is to deliver a product that solves the problem(s) it was designed to address. To be effective in this endeavor, developers must work collaboratively with business analysts to formulate a good design approach. Many projects fail at this stage because it can be very difficult for functional and technical team members to communicate in terms that are well understood by their counterparts. A way to simplify complex designs into a form that can be easily interpreted by all project stakeholders — a common language — is needed. Over the years, numerous attempts have been made to develop programming languages and development methodologies to 23
Description: