Exploring Adapter Patterns in

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Inhaltsverzeichnis

STRUCTURAL DESIGN PATTERNS............................................................................................................... 1


REFERENCE MATERIAL ................................................................................................................................... 1
ADAPTER .................................................................................................................................................... 1
ADAPTER DESIGN PATTERN IN SAP ABAP RAP ................................................................................................ 6
CREATE A CUSTOM TABLE. .............................................................................................................................. 6
ADD RECORDS TO THE TABLE. ......................................................................................................................... 6
CREATE CDS VIEWS (DATA MODEL) ................................................................................................................ 7
DEFINE BUSINESS LOGIC (BEHAVIOR DEFINITION) .............................................................................................. 9
IMPLEMENT THE BUSINESS LOGIC (BEHAVIOR IMPLEMENTATION) ....................................................................... 10
CREATE A SERVICE DEFINITION ...................................................................................................................... 13
CREATE A SERVICE BINDING ......................................................................................................................... 14
PROXY ...................................................................................................................................................... 15
FAÇADE .................................................................................................................................................... 15
FLYWEIGHT ............................................................................................................................................... 15
COMPOSITE .............................................................................................................................................. 15
PROPERTY CONTAINER ................................................................................................................................ 15
DECORATOR .............................................................................................................................................. 15
BRIDGE .................................................................................................................................................... 15

Structural design paƩerns


Reference material
hƩps://www.sap-press.com/design-paƩerns-in-abap-objects_4277/

hƩps://medium.com/@rashadsh/adapter-design-paƩern-497374a2f56c

hƩps://zevolving.com/2012/01/abap-objects-design-paƩerns-%E2%80%93-adapter/

Adapter
 The Adapter design pattern in SAP ABAP Object-Oriented (OO) programming is a structural
pattern that allows objects with incompatible interfaces to work together.
 It acts like a bridge between two different interfaces or classes. This pattern is particularly useful
when you want to integrate new functionalities or systems with existing ABAP applications
without altering their code.

Simple Example: Voltage Converter


 To explain this in simpler terms, think of the Adapter pattern like a voltage converter that allows
an appliance designed for one voltage (say, 110V) to work with a different voltage (220V) without
any modifications to the appliance itself.

Prepared by [email protected]
Example 1:
The zcl_ps class is an illustrative example of applying the Adapter design pattern in an ABAP
environment. It's designed to interface between a project management system and its consumers,
enabling operations like creating projects, work breakdown structures (WBS), and activities through a
unified interface.
CLASS zcl_ps DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
TYPES:BEGIN OF ts_proj,
f1 TYPE char2,
f2 TYPE char3,
END OF ts_proj,
BEGIN OF ts_wbs,
f1 TYPE char2,
f2 TYPE char3,
END OF ts_wbs,
BEGIN OF ts_activity,
f1 TYPE char2,
f2 TYPE char3,
END OF TS_ACTIVITY.
METHODS: create_project IMPORTING is_proj TYPE ts_proj,
create_wbs IMPORTING is_wbs TYPE ts_wbs,
create_activity IMPORTING is_activity TYPE ts_activity.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

App Class
The zcl_app ABAP class demonstrates the Adapter design pattern by enabling operations between two
project management systems (zcl_ps and zcl_msproj) with different interfaces. It abstracts system-
specific functionalities (create_project vs. new_file, etc.) through conditional logic, allowing seamless

Prepared by [email protected]
interaction regardless of the underlying system. This illustrates the Adapter pattern's essence: facilitating
compatibility and interchangeability between disparate interfaces within a single application framework.

CLASS zcl_app DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
INTERFACES:if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_app IMPLEMENTATION.


METHOD if_oo_adt_classrun~main.
DATA: lv_app TYPE flag VALUE 'X',
ls_proj TYPE zcl_ps=>ts_proj,
ls_file TYPE zcl_msproj=>ts_file,
lt_task TYPE STANDARD TABLE OF zcl_msproj=>ts_task WITH DEFAULT KEY,
lt_sub_task TYPE STANDARD TABLE OF zcl_msproj=>ts_sub_task WITH DEFAULT KEY,
lt_wbs TYPE STANDARD TABLE OF zcl_ps=>ts_wbs WITH DEFAULT KEY,
lt_activity TYPE STANDARD TABLE OF zcl_ps=>ts_activity WITH DEFAULT KEY.
IF lv_app = abap_true.
DATA(lo_ps) = NEW zcl_ps( ).
lo_ps->create_project( ls_proj ).

LOOP AT lt_wbs ASSIGNING FIELD-SYMBOL(<ls_wbs>).


lo_ps->create_wbs( <ls_wbs> ).
ENDLOOP.

LOOP AT lt_activity ASSIGNING FIELD-SYMBOL(<ls_Activity>).


lo_ps->create_activity( <ls_activity> ).
ENDLOOP.
ELSE.

DATA(lo_ms) = NEW zcl_msproj( ).


lo_ms->convert_sap_ps_to_ms_proj( ).
lo_ms->new_file( ls_file ).
LOOP AT lt_task ASSIGNING FIELD-SYMBOL(<ls_task>).
lo_ms->add_task( <ls_task> ).
ENDLOOP.
LOOP AT lt_sub_task ASSIGNING FIELD-SYMBOL(<ls_sub_task>).
lo_ms->set_sub_task( <ls_sub_task> ).
ENDLOOP.

ENDIF.
ENDMETHOD.

ENDCLASS.

Prepared by [email protected]
Define the MicrosoŌ Project Class
This class simulates an interface to Microsoft Project with methods to create a new file, add tasks, and
set subtasks.

CLASS zcl_ps IMPLEMENTATION.


METHOD create_project.

ENDMETHOD.

METHOD create_activity.

ENDMETHOD.

METHOD create_wbs.

ENDMETHOD.

ENDCLASS.
Create the Adapter Class (zcl_msproj_adapter)
The Adapter class will inherit from zcl_ps and override its methods to call zcl_msproj methods, acƟng as a
bridge.

CLASS zcl_msproj_adapter DEFINITION


PUBLIC INHERITING FROM zcl_ps
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
METHODS: constructor ,
create_project REDEFINITION,
create_wbs REDEFINITION,
create_activity REDEFINITION.
PROTECTED SECTION.
PRIVATE SECTION.
DATA :go_ms TYPE REF TO zcl_msproj.
ENDCLASS.

CLASS zcl_msproj_adapter IMPLEMENTATION.


METHOD constructor.

super->constructor( ).
go_ms = NEW zcl_msproj( ).
ENDMETHOD.

METHOD create_activity.
super->create_activity( is_activity ).

Prepared by [email protected]
Data ls_sub_task TYPE zcl_msproj=>ts_sub_task.
" Adapt is_activity to ls_sub_task and call go_ms->set_sub_task
go_ms->set_sub_task( ls_sub_task ).
ENDMETHOD.

METHOD create_project.
super->create_project( is_proj ).
DATA ls_file TYPE zcl_msproj=>ts_file.
" Adapt is_proj to ls_file and call go_ms->new_file
go_ms->new_file( ls_file ).

ENDMETHOD.

METHOD create_wbs.
super->create_wbs( is_wbs ).
data ls_task TYPE zcl_msproj=>ts_task.
" Adapt is_wbs to ls_task and call go_ms->add_task
go_ms->add_task( ls_task ).
ENDMETHOD.
UƟlize the Adapter in Your ApplicaƟon
Now, you can use zcl_msproj_adapter in place of zcl_ps to interact with Microsoft Project as if it were
the SAP Project System.
CLASS zcl_msproj_app DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_msproj_app IMPLEMENTATION.


METHOD if_oo_adt_classrun~main.
DATA lo_project_system TYPE REF TO zcl_ps.
lo_project_system = NEW zcl_msproj_adapter( ).

DATA:ls_proj TYPE zcl_ps=>ts_proj,


ls_wbs TYPE zcl_ps=>ts_wbs,
ls_activity TYPE zcl_ps=>ts_activity.

lo_project_system->create_project( ls_proj ).
lo_project_system->create_wbs( ls_wbs ).
lo_project_system->create_activity( ls_activity ).

ENDMETHOD.

Prepared by [email protected]
ENDCLASS.

Summary
 The Adapter design pattern in ABAP OO allows incompatible interfaces to collaborate by
serving as a bridge between them. It's useful for integrating new functionalities into existing
ABAP applications without changing their code.
 For instance, the zcl_ps class is designed to interact with SAP's Project System (PS),
providing methods to create projects, work breakdown structures (WBS), and activities. In
contrast, the zcl_msproj class is intended to work with Microsoft Project, offering different
methods for managing projects.
 The zcl_msproj_adapter class uses the Adapter design pattern by inheriting from zcl_ps and
redefining its methods to delegate calls to an instance of zcl_msproj. This way,
zcl_msproj_adapter translates the interface of zcl_ps into operations compatible with Microsoft
Project.

Adapter Design PaƩern in SAP ABAP RAP


To create an end-to-end scenario for an SAP ABAP RAP unmanaged scenario using the
Adapter design paƩern, you’ll follow these general steps:
1. Create a custom table.
2. Add records to the table.
3. Create CDS views (Data Model).
4. Define business logic (Behavior DefiniƟon).
5. Implement the business logic (Behavior ImplementaƟon).
6. Create a service definiƟon.
7. Create a service binding.
8. Use the Adapter paƩern within the RAP applicaƟon.
Create a custom table.
@EndUserText.label : 'Project Application Table'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zta_projects {

key client : abap.clnt not null;


key project_id : abap.numc(10);
project_name : abap.char(100);
project_status : abap.char(10);

Add records to the table.


CLASS zcl_populate_projects DEFINITION
PUBLIC
FINAL

Prepared by [email protected]
CREATE PUBLIC .

PUBLIC SECTION.
INTERFACES:if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_populate_projects IMPLEMENTATION.


METHOD if_oo_adt_classrun~main.
TYPES:tt_projects TYPE STANDARD TABLE OF zta_projects WITH DEFAULT KEY.
DATA lt_projects TYPE tt_projects.
DO 100 TIMES.

DATA(lv_project_status) = COND char10( WHEN sy-index >= 50 THEN 'ACTIVE' ELSE 'PLANNED' ).
APPEND VALUE #( project_id = sy-index
project_name = |Project { sy-index }|
project_status = lv_project_status ) TO lt_projects.

ENDDO.

DELETE FROM zta_projects.


INSERT zta_projects FROM TABLE @lt_projects.
IF sy-subrc EQ 0.
out->write( 'Records inserted into Project Table successfully' ).
ENDIF.
ENDMETHOD.

ENDCLASS.

Create CDS views (Data Model)


@AbapCatalog.sqlViewName: 'ZRAP_PROJDATA'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view for Project Data'
define root view ZRAP_PROJECT_DATA as select from zta_projects
{
key project_id as ProjectId,
project_name as ProjectName,
project_status as ProjectStatus
}

Prepared by [email protected]
Prepared by [email protected]
Define business logic (Behavior DefiniƟon)

Click Finish and Activate it.

Prepared by [email protected]
Implement the business logic (Behavior ImplementaƟon)

CLASS lhc_ZRAP_PROJECT_DATA DEFINITION INHERITING FROM cl_abap_behavior_handler.


PRIVATE SECTION.

METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION


IMPORTING keys REQUEST requested_authorizations FOR zrap_project_data RESULT result.

METHODS create FOR MODIFY


IMPORTING entities FOR CREATE zrap_project_data.

Prepared by [email protected]
METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE zrap_project_data.

METHODS delete FOR MODIFY


IMPORTING keys FOR DELETE zrap_project_data.

METHODS read FOR READ


IMPORTING keys FOR READ zrap_project_data RESULT result.

METHODS lock FOR LOCK


IMPORTING keys FOR LOCK zrap_project_data.

ENDCLASS.

CLASS lhc_ZRAP_PROJECT_DATA IMPLEMENTATION.

METHOD get_instance_authorizations.
ENDMETHOD.

METHOD create.
DATA lt_projects TYPE STANDARD TABLE OF zta_projects WITH DEFAULT KEY.
lt_projects = VALUE #( FOR ls_entity IN entities (
project_id = ls_entity-ProjectId
project_name = ls_entity-ProjectName
project_status = ls_entity-ProjectStatus ) ).

SORT lt_projects BY project_id.


DELETE ADJACENT DUPLICATES FROM lt_projects COMPARING project_id .
DELETE lt_projects WHERE project_id = space.
INSERT zta_projects FROM TABLE @lt_projects.

ENDMETHOD.

METHOD update.
ENDMETHOD.
METHOD delete.
ENDMETHOD.

METHOD read.

SELECT project_id, project_name, project_status FROM zta_projects

FOR ALL ENTRIES IN @keys


WHERE project_id EQ @keys-ProjectId
INTO TABLE @DATA(lt_projects).
result = CORRESPONDING #( lt_projects ).
ENDMETHOD.

METHOD lock.
ENDMETHOD.

Prepared by [email protected]
ENDCLASS.

CLASS lsc_ZRAP_PROJECT_DATA DEFINITION INHERITING FROM cl_abap_behavior_saver.


PROTECTED SECTION.

METHODS finalize REDEFINITION.

METHODS check_before_save REDEFINITION.

METHODS save REDEFINITION.

METHODS cleanup REDEFINITION.

METHODS cleanup_finalize REDEFINITION.

ENDCLASS.

CLASS lsc_ZRAP_PROJECT_DATA IMPLEMENTATION.

METHOD finalize.
ENDMETHOD.

METHOD check_before_save.
ENDMETHOD.

METHOD save.
ENDMETHOD.

METHOD cleanup.
ENDMETHOD.

METHOD cleanup_finalize.
ENDMETHOD.

ENDCLASS.

Activate the handler class.

Prepared by [email protected]
Create a service definiƟon

Click finish .

Activate the service definition.

Prepared by [email protected]
Create a Service Binding

Publish the Service Binding and Activate it.

Prepared by [email protected]
Proxy
Façade
Flyweight
Composite
Property container
Decorator
Bridge

Prepared by [email protected]

You might also like