jeudi 14 avril 2022

Restrict the instantiation of any concrete products only to its factory

The situation may be well known.

We do not want to enable certain instantiations of objects to be made by everyone, we want to delegate it to the factory, and ONLY the one and ONLY factory.

The factory's instantiation is allowed by everyone. And also its ONE and ONLY, single method, e.g. "produce".

It is like BOPF or handlerclasses in a custo table.... no one else shall be able to instantiate those products.

I would like to do that with the nice formular driven design in ABAP SE24, and use as less code as pussyble.

I managed to find a way.

Prosa-Pseudocode:

We have a factory class, which the "usual" user can instantiate.

Then the usual user ( client) can call lr_factory->produce( "the_class_name_of_the_concrete_product" ).

INFO: This classname maybe hardcoded or determined by "you know what I mean".

Then "produce" creates a "mediator" object...

Then the mediator calls its method "produce_authorized("passing the class name");

In the concrete product "ZCL_PRODUCT_A" the class of the mediator object is assigned as friend.

Furthermore, the "ZCL_PRODUCT_A" is flagged as create private, and also its CTOR.

This works, and the mediator object can call "ro_instance = new ("passed_concrete_class_name").

However...

What I want it to look like, is:

The mediator is ( actually it already is) a superclass of the concrete product, to make it easier for the user to just "inherit its new concrete product" from the mediator superclass, instead of needing to copy any already delivered concrete product classes.

And what I do not get is:

CREATE PROTECTED.

My superclass could create its own children ( if this was enabled ), BUT the way is exactly 180 degrees the opposite.

So, btw, arguing about, why any subclass should be able to instantiate its parent, wether the opposite way, I would like to know:

Did I maybe miss something in the SE24 designer?

INFO: interface designs not that important right now, UNLESS You blast some real cool "overengineered" idea. I'm fine with that.

Aucun commentaire:

Enregistrer un commentaire