ABAP Objects Design Patterns – Facade

By | January 15, 2012 | ABAP Objects, OO Design Patterns | 20,510 | 0

Lets check out one of the simple and most used Design Patterns in Object oriented programming – Facade or Façade (Pronounce as /fəˈsɑːd/) in ABAP.

What is Facade

The word comes from the French language, literally meaning “frontage” or “face”.

If different part of any application are being developed by different developers, there is a good change that every one would end up creating there own interfaces to access similar type of data and process them. The end product due to this could be a very complex system accessing the same thing with different interfaces. This would in turn make very difficult to expand and maintain.

Facade would provide an abstract layer between clients and underlying different objects. It also provides unique interface which would be accessed by all the clients who generally don’t care how the underlying objects are accessed. Clients use the uniform interface provided by facade to pass the required argument. Then its Facade’s responsibility to map parameters from uniform interface to make it compatible to underlying objects interface. Facade may uses conditions to access other objects and generates the output.

BAPI FMs are typically the example of Facade in the procedural programming. BAPI provides an interface which every consumes to do certain tasks without going into implementation details.

Facade Implementation

To implement the Facade, we need to create a class, which would be accessed by the programs. This class would contain necessary methods with required parameters in the signature.

UML

UML for the demo:

OO Design Pattern Facade UML

In words:

  • Under Lying Classes: LCL_DATA, LCL_WRITE_ALV and LCL_WRITE_LOG are underlying classes which are used to perform certain activity.
  • Facade: LCL_FACADE provides unique interface which is accessed by Client. The method PROCESS_REPORT( ) contains all the logic required to generate the report based on the parameter values in the signature. This implementation is hidden from others.
  • Client: Program which uses the class LCL_FACADE to process the output.

Code Lines

Lets check out the code lines:

Facade Demo

 
REPORT  z_np_dp_facade.
 
*
CLASS lcl_data DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor.
ENDCLASS.                    "lcl_Data DEFINITION
*
INTERFACE lif_write.
  METHODS: write_data.
ENDINTERFACE.                    "lif_write DEFINITION
*
CLASS lcl_write_alv DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_write.
ENDCLASS.                    "lcl_write_alv DEFINITION
*
CLASS lcl_write_log DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_write.
ENDCLASS.                    "lcl_write_log DEFINITION
*
CLASS lcl_facade DEFINITION.
  PUBLIC SECTION.
    METHODS: process_report IMPORTING iv_write_type TYPE char1.
ENDCLASS.                    "lcl_facade DEFINITION
*
CLASS lcl_data IMPLEMENTATION.
  METHOD constructor.
    WRITE: / 'Getting Data'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_Data IMPLEMENTATION
*
CLASS lcl_write_alv IMPLEMENTATION.
  METHOD lif_write~write_data.
    WRITE: / 'Writing data in ALV'.
  ENDMETHOD.                    "lif_write~write_Data
ENDCLASS.                    "lcl_write_alv IMPLEMENTATION
*
CLASS lcl_write_log IMPLEMENTATION.
  METHOD lif_write~write_data.
    WRITE: / 'writing data in Log'.
  ENDMETHOD.                    "lif_write~write_Data
ENDCLASS.                    "lcl_write_log IMPLEMENTATION
*
CLASS lcl_facade IMPLEMENTATION.
  METHOD process_report.
    DATA: lo_data TYPE REF TO lcl_data.
    CREATE OBJECT lo_data.
 
    DATA: lo_write TYPE REF TO lif_write.
    IF iv_write_type = 'A'.
      CREATE OBJECT lo_write TYPE lcl_write_alv.
    ELSE.
      CREATE OBJECT lo_write TYPE lcl_write_log.
    ENDIF.
    lo_write->write_data( ).
  ENDMETHOD.                    "process_report
ENDCLASS.                    "lcl_facade IMPLEMENTATION
 
START-OF-SELECTION.
  DATA: lo_facade TYPE REF TO lcl_facade.
  CREATE OBJECT lo_facade.
  lo_facade->process_report( iv_write_type = 'A' ).
 
 

Check out all Design Patterns

You may also want to explore all other Design Patterns in OO ABAP.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.