Exploring Adapter Patterns in
Exploring Adapter Patterns in
Exploring Adapter Patterns in
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.
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.
PUBLIC SECTION.
INTERFACES:if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
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.
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.
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.
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.
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.
Prepared by [email protected]
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES:if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
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.
ENDCLASS.
Prepared by [email protected]
Prepared by [email protected]
Define business logic (Behavior DefiniƟon)
Prepared by [email protected]
Implement the business logic (Behavior ImplementaƟon)
Prepared by [email protected]
METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE zrap_project_data.
ENDCLASS.
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 ) ).
ENDMETHOD.
METHOD update.
ENDMETHOD.
METHOD delete.
ENDMETHOD.
METHOD read.
METHOD lock.
ENDMETHOD.
Prepared by [email protected]
ENDCLASS.
ENDCLASS.
METHOD finalize.
ENDMETHOD.
METHOD check_before_save.
ENDMETHOD.
METHOD save.
ENDMETHOD.
METHOD cleanup.
ENDMETHOD.
METHOD cleanup_finalize.
ENDMETHOD.
ENDCLASS.
Prepared by [email protected]
Create a service definiƟon
Click finish .
Prepared by [email protected]
Create a Service Binding
Prepared by [email protected]
Proxy
Façade
Flyweight
Composite
Property container
Decorator
Bridge
Prepared by [email protected]