Reference: skysky
First, according to the Convention, the class diagram of the previous Subprogram
The test procedure is as follows:
Report zbo_dp_003_re.
* The class and interface for this program
Include zbo_dp_003_if_cl.
* Reference Data for drink Definition
Data: dr_ref type ref to drink.
* Temp reference for decorator
Data: TDR type ref to drink.
Start-of-selection.
* ----------------- Start decorate ---------------*
* Narrowing cast
* Create darkroast object
Create object TDR type darkroast.
* Make dr_ref point to the object darkroast
* And this is the need to be decorated material
Dr_ref = TDR.
* Use mocha to decorate object darkroast
Create object TDR type mocha
Exporting DR = dr_ref. "This dr_ref is darkroast
Dr_ref = TDR.
* Use soy to decorate object mocha & darkroast
Create object TDR type soy
Exporting DR = dr_ref. "This dr_ref is mocha
Dr_ref = TDR.
* Use whip to decorate object soy & mocha & darkroast
Create object TDR type whip
Exporting DR = dr_ref. "This dr_ref is soy
Dr_ref = TDR.
* ------------------- End decorate ---------------*
* Define data which used to display data
Data: ls type string, LF Type F.
* Get description
Ls = dr_ref-> getdesc ().
* Get cost
LF = dr_ref-> Cost ().
* Display result
Write:/LS, ': $', lf decimals 2 exponent 0.
Define component. In our example, it is a super class drink. Note that it is an abstract class.
*----------------------------------------------------------------------*
* Include zbo_dp_003_if_cl *
*----------------------------------------------------------------------*
* For the decorator pattern, normally using an abstract super class
* And the decorator class inherite from the super class also as
* Abstract class
* Super abstract class with drink
Class drink definition abstract.
Public section.
Data: DESC type string.
Methods:
* Get the drink's description
Getdesc Returning Value (de) type string,
* Because the cost must be calculate from every concrete material
* It shoshould be an abstract Method
Cost abstract Returning Value (CO) type f.
Endclass.
* Implement the drink class
Class drink implementation.
Method getdesc.
* Return the description
De = DESC.
Endmethod.
Endclass.
Define concrete component. In our example, it is a subclass of the beverage class, darkroast.
* An concrete class for drink, as one need to be decorated
Class darkroast definition inheriting from drink.
Public section.
Methods:
* Initialization
Constructor,
* The subclass shocould implement abstract method from super
Cost redefinition.
Endclass.
* Implement darkroast
Class darkroast implementation.
Method constructor.
Call method super-> constructor.
* Give a new description
Desc = 'darkroast '.
Endmethod.
Method Cost.
* Get the raw material cost
CO = '1. 99 '.
Endmethod.
Endclass.
It defines the modifier abstract class. Note that it only inherits the drink class and does not do anything. What we need is only an interface, an interactive interface between the decorator and the decorator.
* Decorator definition, which will decorate the raw material
* The decorator shocould be as abstract class
* It is just for supply an interface, it won't implement
* Any method of super class
* Or you coshould define new method here so that the subclass
* Of decorator shoshould have new method in it
Class decorator definition Abstract
Inheriting from drink.
Endclass.
Class decorator implementation.
Endclass.
Define specific decorator
* Define the concrete decorator which will used to decorate
* The concrete drink object, for Exp: darkroast
Class mocha definition inheriting from decorator.
Public section.
* Define the interface which will point to super class drink
Data:
Drink type ref to drink.
Methods:
* Ininitialization
Constructor
Importing Dr type ref to drink,
* Redifine the getdesc method so that we can get the right name
* Of the decorated drink
Getdesc redefinition,
* Redifine the cost method so that we can get the right price
* Of the decorated drink
Cost redefinition.
Endclass.
Class mocha implementation.
Method constructor.
Call method super-> constructor.
* Make drink instance variant point to decorator
* For example, if darkroast decorated with mocha
* The reference drink shoul be pointed to darkroast
Drink = dr.
Endmethod.
Method getdesc.
* This method will show how your Decorate material we used
Data: ls_mocha type string.
Ls_mocha = drink-> getdesc ().
Concatenate ls_mocha ', mocha 'into de.
Endmethod.
Method Cost.
* Calculate the total price of the new drink which be decorated
* By the decorator
Data: lf_mocha Type F.
Lf_mocha = drink-> Cost ().
CO = lf_mocha + '0. 20 '.
Endmethod.
Endclass.
Define other decorators.
* The below part is mostly the same as mocha, because all of them
* Are decorators for the drink raw material
Class soy definition inheriting from decorator.
Public section.
Data:
Drink type ref to drink.
Methods:
Constructor
Importing Dr type ref to drink,
Getdesc redefinition,
Cost redefinition.
Endclass.
Class soy implementation.
Method constructor.
Call method super-> constructor.
Drink = dr.
Endmethod.
Method getdesc.
Data: lssoy type string.
Lssoy = drink-> getdesc ().
Concatenate lssoy ', soy 'into de.
Endmethod.
Method Cost.
Data: lfsoy Type F.
Lfsoy = drink-> Cost ().
CO = lfsoy + '0. 40 '.
Endmethod.
Endclass.
Class whip definition inheriting from decorator.
Public section.
Data:
Drink type ref to drink.
Methods:
Constructor
Importing Dr type ref to drink,
Getdesc redefinition,
Cost redefinition.
Endclass.
Class whip implementation.
Method constructor.
Call method super-> constructor.
Drink = dr.
Endmethod.
Method getdesc.
Data: lswhip type string.
Lswhip = drink-> getdesc ().
Concatenate lswhip, whip 'into de.
Endmethod.
Method Cost.
Data: lfwhip Type F.
Lfwhip = drink-> Cost ().
CO = lfwhip + '0. 60 '.
Endmethod.
Endclass.
This program is hard to understand, a little like the recursion we usually write. Below I will draw out the order of the getdesc method to make it easier to understand, as described below:
If the displayed image is not clear, you can right-click the image and save it as a local image.