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.
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 for the demo:
- 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.
Lets check out the code lines:
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.
- ABAP Object Design Patterns – Singleton
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 1
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 2
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 3
- ABAP Objects Design Patterns – Decorator
- ABAP Objects Design Patterns – Factory Method
- ABAP Objects Design Patterns – Observer
- Case Study: Observer Design Pattern Usage
- ABAP Objects Design Patterns – Abstract Factory
- OO Design Pattern Decorator – Why do we need to use helper variable?
- ABAP Objects Design Patterns – Facade
- ABAP Objects Design Patterns – Adapter
- ABAP Objects Design Patterns – Composite
- ABAP Objects Design Patterns – Iterator
- Iterator Design Pattern to access Linked List in ABAP Objects
- ABAP Objects Design Patterns – Proxy
- ABAP Objects Design Patterns – Prototype
- ABAP Objects Design Patterns – Singleton Factory
- ABAP Object Oriented Approach for Reports – Initial Design
- ABAP Object Oriented Approach for Reports – Redesign
- ABAP Objects Design Patterns – Builder
- ABAP Objects Design Patterns Singleton Usage
- ABAP MVC – Model View Controller Discussion
- Object Oriented Approach for Reports with multiple Datasource
- OO ABAP – Selection Object with Direct Access
- OO ABAP – Selection Criteria Object using RS_REFRESH_FROM_SELECT OPTIONS
- ABAP Factory Method Using SWITCH