Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Is it a non sens to use SingleTon on Factory Design pattern ?

FredericGirod
Active Contributor
0 Kudos
416

Hi team,

I am currently working on a big project, for this project I have created several class (maybe more than 10). All these class have their own responsibilities.

To avoid dependencies between all these class, I have used Interfaces (obviously) and 2 Factories (because I have two main separated block of logic).

Two of these class are SingleTon because I absolutly need to have the same memory area. For example the log. During all the process I have to populate the same log.

Now the process: My process occurs in several part of user-exit & Enhancement of the VA02 transaction. There is no BADi, so no simple way to share memory.

My main problem: During the usage of these tool, there are a lot of Instance creation (yep a lot), these as a possible effect on the performance and memory. I wonder how to reduce these multiple instance creation of the same object.

And I wonder if in the Clean Code & Design Pattern word, it is a non sens to create the Factory class as SingleTon & to keep in these instance the instance previously created ?

If a part of the code need anothere instance of an already created object, it is needed because the instance as a specificity, so the Factory could manage these.

What is your feeling about that?

thanks

Fred

1 ACCEPTED SOLUTION

AndyBoolean
Participant
245

Hi frdric.girod,

From my point of view it's normal to combine several design patterns with eacht other or even necessar to solve problems.

There is even an ABAP Blog about ABAP Objects Design Patterns - Singleton Factory

I hope this helps him.

Best regards,
Andy

10 REPLIES 10

MateuszAdamus
Active Contributor
245

Hello frdric.girod

First thing that comes to my mind is, that if you have a single implementation of your log class, then you could do that as a Singleton. This will simplify your code (no factory needed) and allow you to have the same instance in all placed.

As to the factory, I don't think it's nonsense.

I usually create factory methods as static methods, in this case Singleton pattern for factory is not needed, but you can certainly store already generated instances and return these if are requested again. In my case I do that as a static attribute of the class with factory method.

Kind regards,

Mateusz

0 Kudos
245

Hi mateuszadamus,

So you keep in your factory the already instance created in attribute ?

Fred

0 Kudos
245

Yeah, something like the example shown below.

CLASS lcl_bukrs_factory DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      create_instance_by_bukrs 
        IMPORTING iv_bukrs TYPE bukrs
        RETURNING VALUE(ro_instance) TYPE REF TO lif_bukrs.

  PRIVATE SECTION.
    TYPES:
      BEGIN OF ygs_instance,
        bukrs TYPE bukrs,
        instance TYPE REF TO lif_bukrs,
      END OF ygs_instance,
      ygt_instances TYPE SORTED TABLE ygs_instance WITH UNIQUE KEY bukrs.

    CLASS-DATA:
      at_instances TYPE ygt_instances.
ENDCLASS.

CLASS lcl_bukrs_factory IMPLEMENTATION.
  METHOD create_instance_by_bukrs.
    READ TABLE at_instances REFERENCE DATA(ld_instance)
      WITH TABLE KEY bukrs = iv_bukrs.
    IF sy-subrc <> 0.
      CREATE DATA ld_instance.
      ld_instance->bukrs = iv_bukrs.
      CREATE OBJECT ld_instance->instance TYPE (|lcl_bukrs_{ iv_bukrs }|).
      INSERT ld_instance->* INTO TABLE at_instances.
    ENDIF.

    ro_instance = ld_instance->instance.
  ENDMETHOD.
ENDCLASS.

Kind regards,
Mateusz

0 Kudos
245

Ahh yes! It is a MultiTon-Factory

more or less my question 🙂

Thanks for sharing !

Fred

245

For instance this code?

PUBLIC SECTION.
  CLASS-METHODS factory RETURNING VALUE(result) TYPE REF TO zif...
PRIVATE SECTION.
  DATA singleton TYPE REF TO zif...
...
METHOD factory.
  IF singleton IS NOT BOUND.
    singleton = NEW zcl...( ).
  ENDIF.
  result = singleton.
ENDMETHOD.

0 Kudos
245

Sorry Sandra, I don't understand. Where do you put this code? in the factory class or in the other class ?

AndyBoolean
Participant
246

Hi frdric.girod,

From my point of view it's normal to combine several design patterns with eacht other or even necessar to solve problems.

There is even an ABAP Blog about ABAP Objects Design Patterns - Singleton Factory

I hope this helps him.

Best regards,
Andy

0 Kudos
245

Thanks skieyes for replying,

I didn't know Namesh publish a specific blog about this. (I know he has published a lot of blog about Design Pattern)

I will do it this week my super-factory-multitone, it will be a complex job 🙂

I think the worst part was the SingleTon-destructor

regards

Fred

matt
Active Contributor
0 Kudos
245

Having found a need to create a destructor for a singleton class, I've changed it to multi-instance - it has entailed modification to the use. Fortunately, none of the code has made available for testing yet!

Still, it could be useful in contexts where you can't pass the instance reference by parameter. E.g. between different user exits. Unless someone has another solution?

0 Kudos
245

I personally prefer the Monostate Pattern.

CLASS zcl_monostate DEFINITION
  PUBLIC
  CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS set_x
      IMPORTING i_x TYPE i.
    METHODS get_x
      RETURNING VALUE(r_x) TYPE i.

  PRIVATE SECTION.
    CLASS-DATA its_x TYPE i.
ENDCLASS.
CLASS zcl_monostate IMPLEMENTATION.
  METHOD set_x.
    its_x = i_x.
  ENDMETHOD.

  METHOD get_x.
    r_x = its_x.
  ENDMETHOD.
ENDCLASS.

  METHOD test_instances_behave_as_one.
    DATA(m1) = NEW zcl_monostate( ).
    DATA(m2) = NEW zcl_monostate( ).

    DO 10 TIMES.
      m1->set_x( sy-index ).
      cl_abap_unit_assert=>assert_equals( exp = sy-index act = m2->get_x( ) ).
    ENDDO.
  ENDMETHOD.

The benefit of Singleton is that there only one instance of cross-application. This doesn't work in ABAP.