James E. McDonough

Object-Oriented Design with ABAP

A Practical Approach

James E. McDonough

Pennington, New Jersey, USA

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484228371 . For more detailed information, please visit http://www.apress.com/source-code .

ISBN 978-1-4842-2837-1

e-ISBN 978-1-4842-2838-8

DOI 10.1007/978-1-4842-2838-8

Library of Congress Control Number: 2017943501

© James E. McDonough 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

To my wife, Lorraine, who, at a time in my life when I needed a career change, suggested I consider the field of computer programming.

Introduction

Although many books have been published on the subject of object-oriented design, this book caters specifically to programmers familiar primarily with ABAP, a language with an origin in procedural design, but after years of evolution, now accommodates both the procedural and object-oriented design paradigms. A significant aspect of this book is its accompaniment by a set of exercise programs, each one of which reinforces an object-oriented design concept presented in the book.

For Whom This Book Is Applicable

This book is applicable to virtually all experienced ABAP programmers in one way or another. It is subdivided into three parts, each part applicable to a specific level of familiarity and comfort by the reader with the content it presents.

The first part of the book is intended for ABAP programmers already skilled with the procedural aspects of writing code but who either know nothing about object-oriented programming or simply want to become more comfortable with the object-oriented paradigm. The basic principles of object-oriented programming and design are covered here.

The second and third parts of the book are intended for ABAP programmers already familiar with the basic principles of object-oriented programming but not yet familiar or comfortable with design patterns. The second part of the book presents an introduction to Unified Modeling Language and the third part of the book introduces many of the various design patterns typically associated with object-oriented design.

Those ABAP programmers already familiar with both the object-oriented basic principles as well as Unified Modeling Language and design patterns may find this book covers some design patterns that are altogether new to them or described and illustrated in a context applicable to ABAP programming.

How This Book Should Be Used

ABAP programmers unfamiliar with object-oriented programming should read the book sequentially from the beginning. Indeed, the book is organized primarily for the benefit of such programmers, with each subsequent chapter referring to concepts covered in previous chapters.

ABAP programmers already familiar with object-oriented concepts may skip the first part of the book and start with the second part, to become more familiar with the Unified Modeling Language and design patterns.

ABAP programmers already familiar with design patterns may find it most helpful simply to use this book as a reference.

Regardless of the level of comfort with object-oriented design, this book is modeled on the “learn by doing” premise. Accordingly, Appendix B contains information about retrieving the functional and technical requirements documentation for the accompanying comprehensive set of executable ABAP exercise programs, with each exercise program illustrating some new concept introduced in the book, from the most basic principles of object-oriented programming to the most advanced design patterns. This provides for a multitude of options for using the book and doing the corresponding exercise programs, among them:

  • Writing each new exercise program based solely on the information provided by the requirements documentation and the diagrams accompanying the collection of executable example ABAP exercise program

  • Writing each new exercise program after looking at how the new concepts were implemented in the corresponding executable example ABAP exercise program

  • Dispensing entirely with writing any code and simply relying on the corresponding executable example ABAP exercise programs to illustrate the implementation

Refer to Appendix B for instructions for retrieving the accompanying collection of executable example ABAP exercise programs and their corresponding diagrams.

Why This Book Was Written

In March, 2013 I began presenting a series of weekly one-hour lunch-and-learn lectures on object-oriented programming concepts. These “Object-Oriented Chalk Talks,” as I called them, were attended by my colleagues, all ABAP programmers and all highly skilled in the procedural style of coding, but, having learned their programming skills when ABAP was still a procedural language, they mostly were uncomfortable with the new object-oriented aspects recently introduced to the ABAP language. I pitched the class as one where I would cover general object-oriented concepts in a lecture format, mostly using nothing more than a white board, but that the class was specifically not about ABAP objects. 1 My goal was to introduce to my colleagues the reasons why there is an object-oriented paradigm for writing programs, and not merely to show how these concepts could be coded in ABAP, the syntax of which all of them already knew from having attended extensive training on that subject a few months earlier. I stated up front that students probably would be making a commitment of about 26 weeks (half a year) before we would complete all the material to be covered. Although the lectures were to focus on concepts and not a specific language, all the exercises accompanying the lectures were written in ABAP. The idea behind this arrangement was that students would attend the lecture, learn the concepts, and then go perform the associated exercises on their own before the next lecture.

This first class began with about 16 students, fewer than the 20 students I considered to be the maximum number of students per class considering the facilities at our disposal. Many others had heard about the Object-Oriented Chalk Talks and expressed interest in attending. I soon announced another section would begin in July of 2013, to which the response was so overwhelming that I needed to schedule two other concurrent sections of the Object-Oriented Chalk Talks to accommodate the unexpectedly high number of students wanting to attend. With still others expressing interest in the class, I started a fourth section in September of 2013, and other sections soon followed.

From the feedback I received, the classes proved to be wildly popular and interesting to my colleagues. Considering that so many of my fellow ABAP programmers were finding so much value in the Object-Oriented Chalk Talks, I reasoned there probably are thousands of other ABAP programmers who find themselves in the same situation: being very capable programmers with the procedural aspects of ABAP but having difficulty making the leap to the object-oriented paradigm. I wrote this book to share the material covered in the Object-Oriented Chalk Talks with other programmers beyond my reach in a classroom format.

Credentials of the Author

My formal training in the data processing industry consists of one year at a community college learning mainframe languages (IBM assembler, COBOL, and PL/I) and, nearly 15 years later, a six-week seminar on ABAP programming. Compared with some of my colleagues over the years, I have very little formal training in computer programming. Indeed, I have absolutely no formal training in the concepts of object-oriented programming; everything I know on that subject I learned on my own. So, what makes me think I am qualified to teach anyone else about these concepts?

Prior to getting into the data processing industry over 30 years ago, I earned a college degree in Music Education and taught instrumental music for two years in two different public school districts in the state of New Jersey. During my college years I made an effort to learn and gain some modicum of proficiency with all of the band and orchestra instruments. My perception then was that I could be a better music educator by understanding more about the struggles students endure when they endeavor to learn to play a musical instrument. How, I thought, could I presume to teach a 7 th grader how to play the trombone if I were not able to play it myself?

This philosophy on education served me well those two years I taught in the public schools, and I have continued with this approach ever since. Accordingly, although my credentials in data processing may not be as impressive as those of some of my colleagues, my background as an educator enables me to perceive the problems students are likely to encounter when learning any new skill. So I have learned all I could, on my own, about object-oriented programming, and over the past few years have been able to use this programming style with most of my ABAP development efforts. I believe that now, having gained a certain level of proficiency in this subject, I am ready to impart what I know to others who also wish to become familiar with this fascinating field of object-oriented programming.

Internationalization Considerations

I have made an attempt to consider the various backgrounds of potential readers, and consequently to avoid phrases and references that could be expected to be understood only by programmers who are familiar with daily life in the United States. However, having been exposed only to US culture all my life, the tone of the book exhibits a corresponding slant. This is particularly evident when describing examples where weights and measures are involved, since often the reader will be subjected to the U.S. Customary System of measurement, a system used by virtually no other nations of the world, instead of the much more logical International System of Units (a.k.a. the metric system) used virtually everywhere else. I trust this will not present too formidable a challenge to readers primarily steeped in other cultures.

Acknowledgments

I could not have done this project without the help of others.

I extend my gratitude to Brian Brennan, who introduced me to the book Head First Design Patterns , which started me on the road to discovery of the power of object-oriented programming and eventually led to my decision to create the Object-Oriented Chalk Talks curriculum, lecture series, and accompanying exercises.

Thanks go to those of my colleagues on a project in southern New Jersey who dedicated their time and effort to attend the Object-Oriented Chalk Talks and perform the accompanying exercises, providing me with a laboratory setting in which I could receive the kind of feedback enabling me to refine the content in this book.

I am very grateful to Paul Hardy for agreeing to undertake the task of reviewing the content of the book and for doing such a magnificent job at it, offering many suggestions for improvement.

Susan McDermott, Rita Fernando, and Laura Berendson, my editors at Apress Media, LLC, were of enormous help in guiding me through the publication process and resolving the technical glitches we encountered along the way.

Finally, it would have been much more difficult to complete this project without the love and understanding I received from my family for tolerating my absences during those long hours on weekends and holidays while I was secluded in deep thought about how to organize and present this content.

Contents

  1. Part I: Understanding the Concepts of Object-Oriented Design
    1. Chapter 1:​ Preparing to Take the First Step
      1. Road Map to Object-Oriented Design
        1. Where We Are Now
        2. Where We Are Going
        3. Why We Are Going There
        4. How We Are Going to Get There
      2. Overcoming Psychological Barriers
        1. Flip Turns
        2. Baseball Bats
        3. Keyboards
        4. Programming from the Subconscious
        5. Getting Back on the Horse
      3. Ready to Take the First Step
    2. Chapter 2:​ The Elements of Object-Oriented Programming
      1. A Simple Approach to Object-Oriented Design
      2. Pillars of Object-Oriented Design
      3. Summary
      4. Exercise Preparation
    3. Chapter 3:​ Encapsulation
      1. Separation of Concerns
      2. Visibility
      3. Why Visibility Matters
      4. Realm
      5. The Encapsulation Unit
        1. Instance Encapsulation Units
        2. Static Encapsulation Units
        3. Hybrid Encapsulation Units
      6. Constructors and Destructors
        1. Instance Constructors and Destructors
        2. Static Constructors and Destructors
      7. Friendship
      8. Considerations for Using Encapsulation Effectively
      9. ABAP Language Support for Encapsulation
      10. Encapsulation Units in ABAP
      11. Managing Class Encapsulation Units in the ABAP Repository
      12. Orienting Ourselves After Having Traversed Encapsulation
      13. Summary
      14. Encapsulation Exercises
    4. Chapter 4:​ Abstraction
      1. Abstract Art
      2. Representing Real-World Entities
      3. Class Cohesion
      4. Reusable Components
      5. Establishing a Level of Abstraction
      6. Multiple Instantiations
      7. Relationship Between Abstraction and Encapsulation
      8. ABAP Language Support for Abstraction
      9. Orienting Ourselves After Having Traversed Abstraction
      10. Summary
      11. Abstraction Exercises
    5. Chapter 5:​ Inheritance
      1. Class Hierarchy
      2. Paths of Inheritance
        1. Single Inheritance
        2. Multiple Inheritance
      3. Overriding Inherited Behaviors
      4. Inheriting Unimplemented Behaviors
      5. Controlling Inheritance
      6. Effects of Inheritance
        1. Effect of Inheritance upon Member Visibility
        2. Effect of Inheritance upon Constructor Methods
        3. Effect of Inheritance upon Static Constructor Methods
        4. Effect of Inheritance upon Instance Constructor Methods
        5. Effect of Inheritance upon Reference Variables
        6. Effect of Inheritance upon Subsequent Maintenance
        7. Effect of Inheritance upon Design
      7. Manipulating Values in Reference Variables
      8. Casting
      9. Metaphors for Understanding the Concept of Reference Variable Type
        1. Postal Metaphor
        2. Cinematic Metaphor
      10. ABAP Language Support for Inheritance
      11. Orienting Ourselves After Having Traversed Inheritance
      12. Summary
      13. Inheritance Exercises
    6. Chapter 6:​ Polymorphism
      1. Static Polymorphism
      2. Dynamic Polymorphism
      3. Eliminating Conditional Logic
      4. Effect of Polymorphism upon Subsequent Maintenance
      5. ABAP Language Support for Polymorphism
      6. Orienting Ourselves After Having Traversed Polymorphism
      7. Summary
      8. Polymorphism Exercises
    7. Chapter 7:​ Interfaces
      1. Supplementing Public Visibility of Classes
      2. Interface Reference Variables
      3. Interfaces and Polymorphism
      4. Interfaces and Inheritance
      5. Class Coupling
      6. ABAP Language Support for Interfaces
      7. Orienting Ourselves After Having Traversed Interfaces
      8. Summary
      9. Interfaces Exercises
    8. Chapter 8:​ Welcome to Objectropolis
      1. Establishing Dual Residency
      2. Beyond Objectropolis
  2. Part II: Communicating Design Through Design Diagrams
    1. Chapter 9:​ Introduction to the Unified Modeling Language
      1. UML Class Diagrams
        1. Class-Level Relationships
        2. Instance-Level Relationships
        3. General Relationships
      2. Examples of UML Class Diagrams
      3. Summary
  3. Part III: Leveraging Object-Oriented Concepts Through Design Patterns
    1. Chapter 10:​ Design Patterns
      1. The Gang of Four
      2. The Design Pattern Name
      3. Patterns Explored in this Book
      4. Use of UML Class Diagrams to Illustrate Design Patterns
      5. Summary
    2. Chapter 11:​ Singleton Design Pattern
      1. One and Only One
      2. Singleton in ABAP
      3. Summary
      4. Singleton Exercises
    3. Chapter 12:​ Strategy Design Pattern
      1. The Right Tool for the Job
      2. A Family of Interchangeable Features
      3. Strategy in ABAP
      4. Summary
      5. Strategy Exercises
    4. Chapter 13:​ Observer Design Pattern
      1. Transportation Business Scenario
      2. New Business Requirement
      3. Under Observation
      4. Practical Observations
      5. Uses of the Observer Pattern in Technology Today
      6. Exchanging Information Between Subject and Observer
      7. Observer Design Pattern in ABAP
      8. Summary
      9. Observer Exercises
    5. Chapter 14:​ Factory Design Patterns
      1. Varieties of Manufacturers
      2. Simple Factory Design Pattern
      3. Simple Factory in ABAP
      4. Simple Factory Similarity with Singleton
      5. Factory Method Design Pattern
      6. Factory Method in ABAP
      7. Abstract Factory Design Pattern
      8. Abstract Factory in ABAP
      9. Summary
      10. Factory Exercises
    6. Chapter 15:​ Adapter Design Pattern
      1. Change is Inevitable
      2. Minimizing the Effects of Change
      3. Adapting to Change
      4. Adaptive Behavior
      5. Adapter in ABAP
        1. Adapter in ABAP Using class Scope
        2. Adapter in ABAP Using object Scope
        3. A Variation on a Theme
      6. Summary
      7. Adapter Exercises
    7. Chapter 16:​ Decorator Design Pattern
      1. The Practical Limits of Inheritance
      2. Extending Functionality with Decorator
      3. Decorator in ABAP
        1. Defining the Decorator
        2. Constructing the Decorator
        3. Variation on Decorator Theme
      4. Uses of the Decorator Pattern in Technology Today
      5. Summary
      6. Decorator Exercises
    8. Chapter 17:​ Chain of Responsibility Design Pattern
      1. Lost in Translation
      2. Loose Coupling with Chain of Responsibility
      3. Chain of Responsibility in ABAP
        1. Defining the Chain
        2. Constructing the Chain
      4. Summary
      5. Chain of Responsibility Exercises
    9. Chapter 18:​ Iterator Design Pattern
      1. Programming on a Need-to-Know Basis
      2. Iterator Helps Out
      3. Iterator in ABAP
      4. Summary
      5. Iterator Exercises
    10. Chapter 19:​ Template Method Design Pattern
      1. Providing Flexibility While Enforcing a Specific Sequence of Operations
      2. Template Method in ABAP
      3. Summary
      4. Template Method Exercises
    11. Chapter 20:​ Command Design Pattern
      1. Plumbing Business Scenario
      2. A Command Performance
      3. Command in ABAP
      4. Dependencies and Dependency Injection
      5. Summary
      6. Command Exercises
    12. Chapter 21:​ Null Object Pattern
      1. The Placebo Effect
      2. Null Object in ABAP
      3. Summary
      4. Null Object Exercises
    13. Chapter 22:​ State Design Pattern
      1. State Diagram
      2. Aerial Maneuvers
      3. Maintaining Control Over State Conditional Logic
      4. State in ABAP
      5. ABAP Class Interdependency Considerations
      6. Summary
      7. State Exercises
    14. Chapter 23:​ Lazy Initialization Technique
      1. Let There Be Light
      2. Delay Tactics
      3. Lazy Initialization in ABAP
      4. Summary
      5. Lazy Initialization Exercises
    15. Chapter 24:​ Flyweight Design Pattern
      1. Sharing Resources
      2. Flyweight in ABAP
      3. Summary
      4. Flyweight Exercises
    16. Chapter 25:​ Memento Design Pattern
      1. Chess Anyone?​
      2. Player’s Remorse
      3. Forget-Me-Not
      4. Memento in ABAP
      5. Plugging a Security Exposure
      6. Summary
      7. Memento Exercises
    17. Chapter 26:​ Visitor Design Pattern
      1. Implementation of a New City-Wide Safety Policy
      2. Inspections-R-Us
      3. Sorry, We’re Closed
      4. Just Visiting
      5. Visiting Hours
      6. Visitor in ABAP
      7. Adhering to the Open/​Closed Principle
      8. Visitation Pros and Cons
      9. Revisiting Static Polymorphism
      10. Summary
      11. Visitor Exercises
    18. Chapter 27:​ Design Anti-Patterns
    19. Chapter 28:​ Solidifying Robust Design Habits
    20. Chapter 29:​ Where No One Has Gone Before
    21. Appendix A:​ Comparison of Capabilities Between Function Groups and Classes
    22. Appendix B:​ Requirements Documentation and ABAP Exercise Programs
  4. Index

Contents at a Glance

About the Author and About the Technical Reviewer

About the Author

A447555_1_En_BookFrontmatter_Figb_HTML.jpg

James E. McDonough received a degree in music education from Trenton State College. After teaching music for only two years in the New Jersey public school system, he changed careers. He’s spent the past 35 years as a computer programmer while also maintaining an active presence as a freelance jazz bassist between New York and Philadelphia. Having switched from mainframe programming to ABAP 20 years ago, he now works as a contract ABAP programmer designing and writing ABAP programs on a daily basis. An advocate of using the object-oriented programming features available with ABAP, he has been teaching private ABAP education courses over the past few years, where his background in education enables him to present and explain complicated concepts in a way that makes sense to beginners.

About the Technical Reviewer

A447555_1_En_BookFrontmatter_Figc_HTML.jpg

Paul Hardy joined Heidelberg Cement in the UK in 1990. For the first seven years, he worked as an accountant. In 1997, a global SAP rollout came along; he jumped on board and has never looked back. He has worked on country-specific SAP implementations in the United Kingdom, Germany, Israel, and Australia.

After starting off as a business analyst configuring the good old IMG, Paul swiftly moved on to the wonderful world of ABAP programming. After the initial run of data conversion programs, ALV reports, interactive DYNPRO screens, and (urrggh) SAPscript forms, he yearned for something more and since then has been eagerly investigating each new technology as it comes out, which culminated in him writing the SAP Press book ABAP To The Future .

Paul became an SAP mentor in March 2017 and can regularly be found blogging away on the SCN site. He wrote a series of blogs comparing the merits of object-orientated vs. procedural programming.

He often presents at SAP conferences in Australia (Mastering SAP Technology and the SAP Australian User Group annual conference) and at SAP TECHED Las Vegas. If you ever happen to be at one of these conferences, Paul invites you to have a drink with him at the networking event in the evening and to ask him the most difficult questions you can think of, preferably about SAP.

Footnotes

1 ABAP Objects is the main title of two books by Horst Keller and Sacha Krűger ( ABAP Objects: An Introduction to Programming SAP Applications , Addison Wesley, 2003) and its successor, ABAP Objects: ABAP Programming in SAP NetWeaver, 2 nd edition, Galileo Press, 2007). Many developers now refer to ABAP Objects to mean the object-oriented aspects of the ABAP language.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.160.43