ABAP Objects Design Patterns – Composite

By | January 17, 2012 | ABAP Objects, OO Design Patterns | 4,466 | 1

Composite allows clients to access individual object and composition objects in uniform way.

What is Composite?

Clients needs to work with hierarchical collection of objects – both “Primitive” and “Composite” objects. Primitive (individual) objects processing would be handled differently than the Composition of the objects. So, we will compose a tree structure which would contain both type of objects – Primitive as well as Collection of Primitive objects.

Sometimes we have to deal with Hierarchical or Tree type of data. The data which are related with each other and form a type of hierarchy. We can write the recursive logic to process related entries. But, the problem with the approach is : client has to distinguish between the type of objects, weather it is a end node or collection of nodes. It would become more complex as, we need to do this distinction all the time when we work with those objects. So, composite design pattern shows how to use recursive composition so that client doesn’t have to distinguish between the objects.

UML

Let check out the UML classes which we’ll use for the demo:

Composite Design Pattern in ABAP

We have these components within the UML:

  • LCL_SHAPE – This is an abstract class. Client would use this class to perform the operations. This is also referred as “Component”. Since it’s an abstract class, we can also implement default behavior.
  • LCL_LINE – This is a concrete class inheriting from LCL_SAHPE. This would define the individual behavior of an object. This is known as “Leaf”. Leaf won’t have any kids, meaning no composition.
  • LCL_PICTURE – Class which has a collection of objects. Combination of Lines would create the Picture. This is a “Composite” object. This class would have an internal table defined with referenced to Component. This internal table would contain all the “Kids” which would involve processing of the object. This objects executes operation of each and every individual object. Client would not have to distinguish between the Leaf and Composition. We can simply call the Composition behavior and all it would take care of processing each and every kids operations.

Code Lines

Here is the demo program to show the Composite design pattern implementation in ABAP.

Composite Design Pattern Demo

 
REPORT ZNP_DP_COMPOSITE.
 
class lcl_shape DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODs: constructor IMPORTING iv_name type string,
             add ABSTRACT IMPORTING io_shape type ref to lcl_shape,
             remove ABSTRACT IMPORTING io_shape type ref to lcl_shape,
             display ABSTRACT IMPORTING indent type i.
  PROTECTED SECTION.
    datA: v_name type string.
ENDCLASS.
*
class lcl_shape IMPLEMENTATION.
  METHOD constructor.
    me->v_name = iv_name.
  ENDMETHOD.
ENDCLASS.
*
class lcl_line DEFINITION INHERITING FROM lcl_shape.
  PUBLIC SECTION.
    METHODs: constructor IMPORTING iv_name type string.
    methods: add REDEFINITION,
             remove REDEFINITION,
             display REDEFINITION.
ENDCLASS.
*
class lcl_line IMPLEMENTATION.
  method constructor.
    super->constructor( iv_name ).
  ENDMETHOD.
  METHOD add.
    write : / 'Can not add'.
  ENDMETHOD.
  method remove.
    write : / 'Can not delete'.
  ENDMETHOD.
  method display.
    write : / ".
    Do indent TIMES.
      write: '-'.
    enddo.
    write : v_name.
  endmethod.
endclass.
*
class lcl_picture DEFINITION INHERITING FROM lcl_shape.
  PUBLIC SECTION.
    methods:constructor IMPORTING iv_name type string.
    methods: add REDEFINITION,
             remove REDEFINITION,
             display REDEFINITION.
  PRIVATE SECTION.
    types: BEGIN OF lty_shapes,
            o_shape type ref to lcl_shape,
           end   of lty_shapes.
*    data: li_shapes type STANDARD TABLE OF lty_shapes.
    data: li_shapes type STANDARD TABLE OF ref to lcl_shape.
ENDCLASS.
*
CLASS lcl_picture IMPLEMENTATION.
  method constructor.
    super->constructor( iv_name ).
  endmethod.
  method add.
    append io_shape to li_shapes.
  ENDMETHOD.
  method remove.
    delete li_shapes where table_line eq io_shape.
  ENDMETHOD.
  method display.
    data: lo_shape type ref to lcl_shape.
    write: / ".
    do indent TIMES.
      write: (2) '-'.
    ENDDO.
    write: v_name.
    data: lv_indent type i.
    lv_indent = indent + 1.
*   Processing each primitive object within the composition
    loop at li_shapes into lo_shape.
      lo_shape->display( lv_indent ).
    endloop.
  endmethod.
ENDCLASS.
*
class lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: run.
ENDCLASS.
*
class lcl_main IMPLEMENTATION.
  METHOD run.
 
    data: lo_pic   type ref to lcl_shape.
    data: lo_shape type ref to lcl_shape.
 
    create object lo_pic type lcl_picture EXPORTING iv_name = 'Picture'.
    create object lo_shape type lcl_line EXPORTING iv_name = 'Left Line'.
    lo_pic->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Top Line'.
    lo_pic->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Right Line'.
    lo_pic->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Bottom Line'.
    lo_pic->add( lo_shape ).
 
    data: lo_pic2 type ref to lcl_shape.
    create OBJECT lo_pic2 type lcl_picture EXPORTING iv_name = 'Picture 2'.
    create object lo_shape type lcl_line EXPORTING iv_name = 'Left Line'.
    lo_pic2->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Top Line'.
    lo_pic2->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Right Line'.
    lo_pic2->add( lo_shape ).
    create object lo_shape type lcl_line EXPORTING iv_name = 'Bottom Line'.
    lo_pic2->add( lo_shape ).
    lo_pic->add( lo_pic2 ).
 
    create object lo_shape type lcl_line EXPORTING iv_name = 'text'.
    lo_pic->add( lo_shape ).
 
*   Uniform way to access the composition - it could be a primitive object
*   or composition itself.
    lo_pic->display( 1 ).
 
  endmethod.
ENDCLASS.
*
START-OF-SELECTION.
  lcl_main=>run( ).
 
 

Design Time Consideration:

We should follow these steps to achieve Composite Design Pattern.

  • Create an Abstract Class
  • Inherit an individual class with certain behavior
  • Inherit a composite class which would store the Kids objects. Operation of Composite objects should actually perform the operations of each child.
  • Client should use Component Abstract class to interact with composite objects.

Let me know, if you have used this pattern …

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{272 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 272 articles.

Load comments

1 Comment

  • hatim

    thx a lot for your effort,

    the programm doesnt compile, but it´s just a “.” at the end of the first line in the methode “display”:

    write : / ".

    sorry for my bad english.

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.