All code can be found in my GitHub repository: https://github.com/IlyaPrusakou/ABAPBuilderPattern
In this repository you can find implementation of Builder Pattern in ABAP. The main point is the realization of pattern made on special version of ABAP language - ABAP for Cloud Development. So that there is 100% cloud compatibility.
Just copy and use it.
The Builder pattern is one of the classic creational design pattern. The main idea of the one is to separate process of creation of Product class (Product, Car, Car Schema and etc.) A logic for creation of Product class is handled into Builder class.
Below you can find Pattern Schemas and Implementation Details, you can skip theoretical notes and go to these sections.
There are 3 types of builder pattern I've differentiated. Each of them can be found correspondingly in GitHub files: src/zpru_bld1, src/zpru_bld2, src/zpru_bld3.
All classes and interfaces can play roles of:
Regarding of Product role I would like to underline distinction of at least two type of specialization:
Factual specialization means that all instances of product has the same class or interface, but they differ by data preserved into. The example:
As you can see there are two instances of class Car, but they are varied with qualified field model, which can be read-only field, set inside constructor.
As you can guessed that type of specialization is based on usage of not only the same class with different values of qualified fields, but also different classes or interfaces. Moreover, there is room for amplification of interfaces raised. The same example:
Regarding Builder role I would elucidate method Build. The method creates an instance of Product and set all data, preserved in Builder instance, into product instance. We can set up builder once with setters methods. All the data will be plugged in Product instance. Each invocation of Build creates new instance of Product with data from Builder. The Builder is a source of data, along with that Product is a recipient of data. Moreover, there is space for implementation of specialization of product. For instance, Builder can have multiple Build methods (buildProductA, buildProductB and etc.), returning different instance setup or different class types.
Director role is just additional stage of Product creation. When Builder is a distinct layer for creation of Products, but Director is a layer to create or setup of Builders. Hence the question of specialization rises again there. For example, we can make multiple methods (setupBuilderA, setupBuilderB and etc.) for setup of different Builders.
" Create mercedes builder
" pass mandatory fields
DATA(lo_mercedes_builder) = NEW zpru_cl_bld1_car_builder( iv_id = '1'
iv_brand = 'MERCEDES'
iv_model = 'C 180 T-Model' ).
" pass optional fields into builder
lo_mercedes_builder->set_price('100000' )->set_multimedia( 'AVANTGARDE' )->set_equipment( 'Memory Package' )->set_assistance_system('Driving Assistance Package Plus' ).
" construct final object of mercedes
DATA(lo_mercedes_c_180) = lo_mercedes_builder->build( ).
DATA(lo_director) = NEW zpru_cl_bld2_car_director( ).
DATA(lo_mercedes_builder) = NEW zpru_cl_bld2_car_builder( iv_brand = 'MERCEDES'
iv_model = 'C 180 T-Model' ).
lo_director->build_mercedes( io_builder = lo_mercedes_builder ).
lo_mercedes_builder->set_color( 'BLUE' ).
lo_mercedes_builder->set_id( '1' ).
DATA(lo_mercedes_c180_1_blue) = lo_mercedes_builder->build( ).
DATA lo_mercedes_builder_car TYPE REF TO zpru_if_bld3_builder_car.
DATA lo_mercedes_builder_schema TYPE REF TO zpru_if_bld3_builder_schema.
DATA lo_director TYPE REF TO zpru_cl_bld3_car_director.
lo_director = NEW zpru_cl_bld3_car_director( ).
lo_mercedes_builder_car = NEW zpru_cl_bld3_car_builder( iv_brand = 'MERCEDES'
iv_model = 'C 180 T-Model' ).
lo_director->build_mercedes_car( io_builder = lo_mercedes_builder_car ).
lo_mercedes_builder_car->zpru_if_bld3_builder~set_id( '1' ).
DATA(lo_mercedes_c180_car) = lo_mercedes_builder_car->build( ).
lo_mercedes_builder_schema = NEW zpru_cl_bld3_schema_builder( iv_brand = 'MERCEDES'
iv_model = 'C 180 T-Model' ).
lo_director->build_mercedes_schema( io_builder = lo_mercedes_builder_schema ).
lo_mercedes_builder_schema->zpru_if_bld3_builder~set_id( '2' ).
DATA(lo_mercedes_c180_schema) = lo_mercedes_builder_schema->build( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 33 | |
| 28 | |
| 24 | |
| 14 | |
| 13 | |
| 12 | |
| 11 | |
| 11 | |
| 9 | |
| 8 |