前言
大家可以使用开源项目ABAPplantUML生成UML类图
代码仅是个人测试记录
实例源代码
单例模式
report ypwk_singleton.
class lcl_basic_singleton definition create private.
public section.
class-methods:get_instance
returning value(ro_instance) type ref to lcl_basic_singleton.
class-methods class_constructor.
methods:set_name importing i_name type string,
get_name returning value(r_name) type string.
private section.
class-data:mo_intance type ref to lcl_basic_singleton.
data:mv_name type string.
endclass.
class lcl_basic_singleton implementation.
method class_constructor.
create object mo_intance."饿汉模式,取消改方法实施即为懒汉模式
endmethod.
method get_instance.
if mo_intance is not bound.
create object mo_intance.
endif.
ro_instance = mo_intance.
endmethod.
method set_name.
me->mv_name = i_name.
endmethod.
method get_name.
r_name = me->mv_name.
endmethod.
endclass.
start-of-selection.
data(lo_instance1) = lcl_basic_singleton=>get_instance( ).
lo_instance1->set_name( 'name1').
data(lo_instance2) = lcl_basic_singleton=>get_instance( ).
data(lv_name2) = lo_instance2->get_name( ).
write lv_name2.
工厂模式
interface if_cellphone.
methods:ring.
endinterface.
class huawei definition.
public section.
interfaces:if_cellphone.
methods:constructor.
endclass.
class huawei implementation.
method constructor.
write :/ '华为手机制造成功'.
endmethod.
method if_cellphone~ring.
write:/ '华为手机响起铃声...'.
endmethod.
endclass.
class xiaomi definition.
public section.
interfaces:if_cellphone.
methods:constructor.
endclass.
class xiaomi implementation.
method constructor.
write :/ '小米手机制造成功'.
endmethod.
method if_cellphone~ring.
write :/ '小米手机响起铃声...'.
endmethod.
endclass.
class cellphone_factory definition.
public section.
methods: constructor,
make_cellphone importing iv_type type string
returning value(ro_cellphone) type ref to if_cellphone..
endclass.
class cellphone_factory implementation.
METHOD constructor.
WRITE :/ '手机工厂新建成功!!!'.
ENDMETHOD.
method make_cellphone.
case iv_type.
when 'MI'.
create object ro_cellphone type xiaomi.
when 'HW'.
create object ro_cellphone type huawei.
when others.
endcase.
endmethod.
endclass.
start-of-selection.
data(lo_factory) = new cellphone_factory( ).
data(lo_huawei) = lo_factory->make_cellphone( iv_type = 'HW').
lo_huawei->ring( ).
data(lo_xiaomi) = lo_factory->make_cellphone( iv_type = 'MI').
lo_xiaomi->ring( ).
抽象工厂模式
report ypwk_abstract_factory.
class animals definition abstract.
public section.
methods:sayhello importing i_name type string.
endclass.
class animals implementation.
method sayhello.
write: / 'animals say hello to all people'.
endmethod.
endclass.
class pig definition inheriting from animals create public.
public section.
methods:sayhello redefinition.
endclass.
class pig implementation.
method sayhello.
write: 'pig say hello to ', i_name.
endmethod.
endclass.
class cow definition inheriting from animals create public.
public section.
methods: sayhello redefinition.
endclass.
class cow implementation.
method sayhello.
write:/ 'cow say hello to ',i_name.
endmethod.
endclass.
class duck definition inheriting from animals create public.
public section.
methods: sayhello redefinition.
endclass.
class duck implementation.
method sayhello.
write:/ 'duck say hello to ',i_name.
endmethod.
endclass.
interface if_plant.
methods:sayhello importing i_name type string.
data:m_test type string.
endinterface.
class tree definition create public.
public section.
interfaces:if_plant.
endclass.
class tree implementation.
method if_plant~sayhello.
write:/ 'tree sy hello to ', i_name.
endmethod.
endclass.
class flower definition create public.
public section.
interfaces:if_plant.
endclass.
class flower implementation.
method if_plant~sayhello.
write:/ 'flower sy hello to ', i_name.
endmethod.
endclass.
class grass definition create public.
public section.
interfaces:if_plant.
endclass.
class grass implementation.
method if_plant~sayhello.
write:/ 'grass sy hello to ', i_name.
endmethod.
endclass.
class abstract_factory definition abstract.
public section.
methods:get_animals
importing i_type type string
returning value(ro_animal) type ref to animals,
get_plant
importing i_type type string
returning value(ro_plant) type ref to if_plant.
endclass.
class abstract_factory implementation.
method get_animals.
endmethod.
method get_plant.
endmethod.
endclass.
class factory_sz definition inheriting from abstract_factory create public.
public section.
methods:get_animals redefinition,
get_plant redefinition.
endclass.
class factory_sz implementation.
method get_animals.
case i_type.
when 'PIG'.
ro_animal = new pig( ).
when 'COW'.
ro_animal = new cow( ).
when others.
endcase.
endmethod.
method get_plant.
case i_type.
when 'T'.
ro_plant = new tree( ).
when 'F'.
ro_plant = new flower( ).
when 'G'.
ro_plant = new grass( ).
when others.
endcase.
endmethod.
endclass.
class factory_gz definition inheriting from abstract_factory create public.
public section.
methods:get_animals redefinition,
get_plant redefinition.
endclass.
class factory_gz implementation.
method get_animals.
case i_type.
when 'PIG'.
ro_animal = new pig( ).
when 'COW'.
ro_animal = new cow( ).
when 'DUCK'.
ro_animal = new duck( ).
when others.
endcase.
endmethod.
method get_plant.
case i_type.
when 'T'.
ro_plant = new tree( ).
when 'F'.
ro_plant = new flower( ).
when others.
endcase.
endmethod.
endclass.
class factory_builder definition create public.
public section.
class-methods: get_factory importing i_name type string
returning value(ro_factory) type ref to abstract_factory.
endclass.
class factory_builder implementation.
method get_factory.
case i_name.
when 'SZ'.
ro_factory = new factory_sz( ).
when 'GZ'.
ro_factory = new factory_gz( ).
when others.
endcase.
endmethod.
endclass.
start-of-selection.
data(lo_factory_sz) = factory_builder=>get_factory( 'SZ').
data(lv_animal_sz) = lo_factory_sz->get_animals( 'COW').
lv_animal_sz->sayhello( 'wibur').
data(lo_factory_gz) = factory_builder=>get_factory( 'GZ').
data(lv_plant_gz) = lo_factory_gz->get_plant( 'T').
lv_plant_gz->sayhello( 'wibur').
建造者模式
report ypwk_builder.
"包装方法接口
interface if_pack.
methods: packing returning value(r_pack) type string.
endinterface.
"包装方法-碗
class bowl definition create public.
public section.
interfaces:if_pack.
endclass.
class bowl implementation.
method if_pack~packing.
r_pack = '一碗'.
endmethod.
endclass.
"包装方法-盘子
class plate definition create public.
public section.
interfaces:if_pack.
endclass.
class plate implementation.
method if_pack~packing.
r_pack = '一盘'.
endmethod.
endclass.
"包装方法-杯子
class cup definition create public.
public section.
interfaces:if_pack.
endclass.
class cup implementation.
method if_pack~packing.
r_pack = '一杯'.
endmethod.
endclass.
"食物接口
interface if_food.
methods:get_name returning value(r_name) type string,
get_price returning value(r_price) type i,
get_pack returning value(ro_pack) type ref to if_pack.
endinterface.
"食物-米饭
class rice definition create public.
public section.
interfaces:if_food.
endclass.
class rice implementation.
method if_food~get_name.
r_name = '米饭'.
endmethod.
method if_food~get_price.
r_price = 1.
endmethod.
method if_food~get_pack.
ro_pack = new bowl( ).
endmethod.
endclass.
"食物-面条
class noodle definition create public.
public section.
interfaces:if_food.
endclass.
class noodle implementation.
method if_food~get_name.
r_name = '面条'.
endmethod.
method if_food~get_price.
r_price = 3.
endmethod.
method if_food~get_pack.
ro_pack = new bowl( ).
endmethod.
endclass.
"食物-肉-抽象类
class meat definition abstract create public.
public section.
interfaces:if_food.
endclass.
class meat implementation.
method if_food~get_name.
r_name = '炒肉'.
endmethod.
method if_food~get_price.
r_price = 10.
endmethod.
method if_food~get_pack.
ro_pack = new plate( ).
endmethod.
endclass.
"食物-肉-牛肉
class beef definition inheriting from meat create public.
public section.
methods: if_food~get_name redefinition,
if_food~get_price redefinition.
endclass.
class beef implementation.
method if_food~get_name.
r_name = '孜然炒牛肉'.
endmethod.
method if_food~get_price.
r_price = 40.
endmethod.
endclass.
"食物-肉-猪肉
class pork definition inheriting from meat create public.
public section.
methods: if_food~get_name redefinition,
if_food~get_price redefinition.
endclass.
class pork implementation.
method if_food~get_name.
r_name = '回锅肉'.
endmethod.
method if_food~get_price.
r_price = 30.
endmethod.
endclass.
"食物-蔬菜
class vegetable definition create public.
public section.
interfaces:if_food.
endclass.
class vegetable implementation.
method if_food~get_name.
r_name = '蒜蓉生菜'.
endmethod.
method if_food~get_price.
r_price = 2.
endmethod.
method if_food~get_pack.
ro_pack = new plate( ).
endmethod.
endclass.
"饮料-牛奶
class milk definition create public.
public section.
interfaces:if_food.
endclass.
class milk implementation.
method if_food~get_name.
r_name = '牛奶'.
endmethod.
method if_food~get_price.
r_price = 3.
endmethod.
method if_food~get_pack.
ro_pack = new cup( ).
endmethod.
endclass.
"基础实现类
class meal definition create public.
public section.
methods:add_food importing i_food type ref to if_food,
display_list,
get_bill RETURNING VALUE(r_cost) type i,
constructor importing i_name type string.
private section.
types:begin of ty_list,
food type ref to if_food,
end of ty_list.
data:mt_list type table of ty_list.
endclass.
class meal implementation.
method constructor.
write :/ '====', i_name,'===='.
endmethod.
method add_food.
append initial line to mt_list assigning field-symbol(<fs_list>).
<fs_list>-food = i_food.
endmethod.
method display_list.
loop at mt_list into data(ls_list).
write:/ ls_list-food->get_name( ),
sy-vline ,
ls_list-food->get_pack( )->packing( ),
sy-vline ,
ls_list-food->get_price( ).
endloop.
endmethod.
method get_bill.
clear r_cost.
loop at mt_list into data(ls_list).
r_cost = r_cost + ls_list-food->get_price( ).
endloop.
endmethod.
endclass.
start-of-selection.
data(lo_lunch) = new meal( '午餐').
lo_lunch->add_food( new rice( ) ).
lo_lunch->add_food( new pork( ) ).
lo_lunch->add_food( new vegetable( ) ).
lo_lunch->display_list( ).
data(lv_total_price) = lo_lunch->get_bill( ).
write:/ '总价:',lv_total_price.
原型模式
report ypwk_prototype.
class abs_prototype definition abstract create public.
public section.
methods:clone returning value(ro_clone) type ref to abs_prototype.
data:mv_name type string.
endclass.
class abs_prototype implementation.
method clone.
" ro_clone = new abs_prototype( ).
endmethod.
endclass.
class prototype definition inheriting from abs_prototype create public.
public section.
methods:clone redefinition.
private section.
data:mo_clone type ref to prototype.
endclass.
class prototype implementation.
method clone.
data:lo_clone type ref to prototype.
create object lo_clone.
lo_clone->mv_name = me->mv_name.
ro_clone = lo_clone.
endmethod.
endclass.
start-of-selection.
data(lo_instance) = new prototype( ).
lo_instance->mv_name = 'name1'.
data(lo_instance2) = lo_instance->clone( ).
write lo_instance2->mv_name.