forked from moreus/ABAPDesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zdp_state.abap
102 lines (86 loc) · 2.38 KB
/
zdp_state.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
*&---------------------------------------------------------------------*
*& Report ZDP_STATE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdp_state.
CLASS context DEFINITION DEFERRED.
CLASS cl_abap_typedescr DEFINITION LOAD.
DATA: moff TYPE i
, slen TYPE i
, mlen TYPE i.
DEFINE get_clazz_name.
&2 = cl_abap_classdescr=>get_class_name( &1 ).
FIND REGEX 'CLASS=' IN &2 MATCH OFFSET moff match LENGTH mlen.
slen = moff + mlen.
SHIFT &2 BY slen PLACES LEFT.
END-OF-DEFINITION.
CLASS state DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS:
handle ABSTRACT IMPORTING io_context TYPE REF TO context.
ENDCLASS.
CLASS context DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING io_state TYPE REF TO state,
request.
DATA: mo_state TYPE REF TO state.
ENDCLASS.
CLASS context IMPLEMENTATION.
METHOD constructor.
me->mo_state = io_state.
ENDMETHOD.
METHOD request.
me->mo_state->handle( io_context = me ).
ENDMETHOD.
ENDCLASS.
CLASS concrete_statea DEFINITION INHERITING FROM state.
PUBLIC SECTION.
METHODS:
handle REDEFINITION.
ENDCLASS.
CLASS concrete_stateb DEFINITION INHERITING FROM state.
PUBLIC SECTION.
METHODS:
handle REDEFINITION.
ENDCLASS.
CLASS concrete_statea IMPLEMENTATION.
METHOD handle.
DATA: stateb TYPE REF TO state.
DATA: class_name TYPE abap_abstypename.
CREATE OBJECT stateb TYPE concrete_stateb.
io_context->mo_state = stateb.
get_clazz_name stateb class_name.
WRITE: / 'State:', class_name.
ENDMETHOD.
ENDCLASS.
CLASS concrete_stateb IMPLEMENTATION.
METHOD handle.
DATA: statea TYPE REF TO state.
DATA: class_name TYPE abap_abstypename.
CREATE OBJECT statea TYPE concrete_statea.
io_context->mo_state = statea.
get_clazz_name statea class_name.
WRITE: / 'State:', class_name.
ENDMETHOD.
ENDCLASS.
CLASS mainapp DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: main.
ENDCLASS.
CLASS mainapp IMPLEMENTATION.
METHOD: main.
DATA: context TYPE REF TO context.
DATA: state TYPE REF TO concrete_statea.
CREATE OBJECT state.
CREATE OBJECT context EXPORTING io_state = state.
context->request( ).
context->request( ).
context->request( ).
context->request( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
mainapp=>main( ).