Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Keller
Active Contributor
Dear community, you may know this Gothic novella by Scottish author Robert Louis Stevenson: "The Strange Case of Dr Jekyll and Mr Hyde". The novella is about the multifaceted nature of humans. About the good and bad in people. Most of the time, the truth lies somewhere in between. Many people are neither all good nor all bad.

The story is about Dr. Henry Jekyll, a good man. But he has a very evil side. In these moments he is Mr. Edward Hyde. Two personalities in one and the same person. That gave me the idea of building a small example with two interfaces and one class in ABAP.

First of all we need an interface for Dr. Henry Jekyll (good personality):
INTERFACE zmke_if_henry_jekyll PUBLIC.

METHODS do_kind_things.

METHODS switch_personality RETURNING VALUE(result) TYPE REF TO zmke_if_edward_hyde.

ENDINTERFACE.

Then we have an interface for Mr. Edward Hyde (bad personality):
INTERFACE zmke_if_edward_hyde PUBLIC.

METHODS do_malicious_things.

METHODS switch_personality RETURNING VALUE(result) TYPE REF TO zmke_if_henry_jekyll.

ENDINTERFACE.

Both interfaces are used by a class to represent Dr. Henry Jekyll as a person. Now we have the good and the bad personality in one class. I've used a Singleton design pattern to emphasize the unique character of an instance of this class.
CLASS zmke_cl_henry_jekyll DEFINITION PUBLIC FINAL CREATE PRIVATE.

PUBLIC SECTION.
INTERFACES zmke_if_henry_jekyll.
INTERFACES zmke_if_edward_hyde.

CLASS-METHODS get_instance RETURNING VALUE(result) TYPE REF TO zmke_if_henry_jekyll.

PROTECTED SECTION.

PRIVATE SECTION.
CLASS-DATA instance TYPE REF TO zmke_cl_henry_jekyll.

ENDCLASS.



CLASS zmke_cl_henry_jekyll IMPLEMENTATION.

METHOD zmke_if_henry_jekyll~do_kind_things.
ENDMETHOD.

METHOD zmke_if_edward_hyde~do_malicious_things.
ENDMETHOD.

METHOD get_instance.
IF instance IS NOT BOUND.
instance = NEW #( ).
ENDIF.

result = instance.
ENDMETHOD.

METHOD zmke_if_henry_jekyll~switch_personality.
result = instance.
ENDMETHOD.

METHOD zmke_if_edward_hyde~switch_personality.
result = instance.
ENDMETHOD.

ENDCLASS.

Finally, we have an executable example class.

In the "TEST_NO_PERSONALITY_CONTROL" method you can see an instance of the class that only allows certain good or bad options for action due to the different interface reference variables.

In the "TEST_WITH_PERSONALITY_CONTROL" method, a CAST can be used to gain overall control over both options. An option that Dr. Henry Jekyll unfortunately doesn't have in the story 😞
CLASS zmke_cl_interface_example DEFINITION PUBLIC FINAL CREATE PUBLIC.

PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.

PROTECTED SECTION.

PRIVATE SECTION.
METHODS test_no_personality_control IMPORTING out TYPE REF TO if_oo_adt_classrun_out.

METHODS test_with_personality_control IMPORTING out TYPE REF TO if_oo_adt_classrun_out.

ENDCLASS.



CLASS zmke_cl_interface_example IMPLEMENTATION.

METHOD if_oo_adt_classrun~main.
test_no_personality_control( out ).
test_with_personality_control( out ).
ENDMETHOD.

METHOD test_no_personality_control.
DATA henry_jekyll TYPE REF TO zmke_if_henry_jekyll.
DATA edward_hyde TYPE REF TO zmke_if_edward_hyde.

henry_jekyll = zmke_cl_henry_jekyll=>get_instance( ).
henry_jekyll->do_kind_things( ).

edward_hyde = henry_jekyll->switch_personality( ).
edward_hyde->do_malicious_things( ).

IF henry_jekyll = edward_hyde.
out->write( 'Dr. Henry Jekyll and Mr. Edward Hyde are the same person.' ).
ENDIF.
ENDMETHOD.

METHOD test_with_personality_control.
DATA henry_jekyll TYPE REF TO zmke_cl_henry_jekyll.
henry_jekyll ?= zmke_cl_henry_jekyll=>get_instance( ).

henry_jekyll->zmke_if_edward_hyde~do_malicious_things( ).
henry_jekyll->zmke_if_henry_jekyll~do_kind_things( ).
ENDMETHOD.

ENDCLASS.

All sources are here on GitHub. I hope you had some infotainment 🙂

 

Best regards, thanks for reading and stay healthy

Michael

 

 
6 Comments
Labels in this area