技术开发 频道

ABAP与设计模式之工厂方法模式

【IT168 技术文章】

    内容:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。


    简单来说,工厂方法模式能够封装具体的类型的实例化。

    结构:


    Product:所有产品都必须实现这个共同的接口,这样一来,使用这些产品的类就可以引用这个接口,而不是具体类。

    ConcreteCreator:负责创建一个或者多个具体产品,只有concrete creator类知道如何创建这些产品。同时,它实现了factory method,以实际制造出产品。

    Creator: 是一个抽象类,它实现了所有操作产品的方法,但不实现工厂方法。其所有的子类都必须实现这个抽象的factorymethod()方法。

    实例程序,首先给出类图:

    注意:创建类和产品类是平行的,因为他们都是抽象类,而抽象类都有许多具体的子类,每个子类都有自己特定的实现。

    测试程序如下:

    REPORT ZBOBO_DP_004_RE .

    
*Factory method pattern used interface and class

    include zbobo_dp_004_if_cl.

    
*This data used to create two type of concrete creator

    data:

    ny_ref type ref to pizzastore,

    chi_ref type ref to pizzastore.

    
*The different pizza have the same interface

    data:

    pz_ref type ref to pizza.

    start
-of-selection.

    
* Create two different pizzastore

    create object ny_ref type ny.
"For NY

    create object chi_ref type chi.
"For Chi

    
* Book NY store's pizza

    call method ny_ref
->orderpizza

    exporting pz_name1
= 'cheese'

    receiving pz
= pz_ref.

    
* Get the pizza's name

    data: ls type string.

    call method pz_ref
->getname

    receiving na
= ls.

    write:
/ 'Ethan ordered a:',ls.

    skip.

    
* Book Chi store's pizza

    call method chi_ref
->orderpizza

    exporting pz_name1
= 'cheese'

    receiving pz
= pz_ref.

    
* Get the pizza's name

    call method pz_ref
->getname

    receiving na
= ls.

    write:
/ 'Joel ordered a:',ls. 

    抽象产品类:

    *----------------------------------------------------------------------*

    
* INCLUDE ZBOBO_DP_004_IF_CL *

    
*----------------------------------------------------------------------*

    
*Declare product class

    
class pizza definition abstract.

    
public section.

    
* Define instance variants

    data:

    name type string,

    dough type string,

    sauce type string.

    data:

    begin of rtab,

    str type string,

    end of rtab,

    itab like table of rtab.

    
* Methods which will be inherited by subclass

    methods:

    
* Prepare pizza

    prepare,

    
* Bake pizza

    bake,

    
* Cut pizza

    cut,

    
* Boxing pizza

    box,

    
* Getter method to get pizza name

    getname

    returning value(na) type string.

    endclass.

    
*Implement the pizza class

    
class pizza implementation.

    method prepare.

    write:

    
/ 'Preparing:', name,

    
/ 'Tossing dough....',

    
/ 'Adding sauce....',

    
/ 'Adding toppings:'.

    loop at itab into rtab.

    write:
/ rtab-str.

    endloop.

    endmethod.

    method bake.

    write:
/ 'Bake for 25 minutes at 350'.

    endmethod.

    method cut.

    write:
/ 'Cutting the pizza into diagonal slices'.

    endmethod.

    method box.

    write:
/ 'place pizza in official PizzaStore box'.

    endmethod.

    method getname.

    na
= name.

    endmethod.

    endclass.

    工厂类:

    *Delare plant method class

    
*We can now define plant class,note that it is in the same leve as produ

    
class pizzastore definition abstract.

    
public section.

    methods:

    
* This method is used to order the concrete product

    orderpizza

    importing pz_name1 type string

    returning value(pz) type ref to pizza.

    
protected section.

    methods:

    
* This method seems like a factory

    
* Because this method is abstract, the subclass must instantiate thi

    
* This method must have a return value, which is the concrete produc

    createpizza
abstract

    importing pz_name2 type string

    returning value(pz) type ref to pizza.

    endclass.

    
class pizzastore implementation.

    method orderpizza.

    data: pz_ref type ref to pizza.

    
* This is the key part of factory method pattern

    
* We use factory method to create concrete product

    call method createpizza

    exporting pz_name2
= pz_name1

    receiving pz
= pz_ref.

    
* Other methods

    call method:

    pz_ref
->prepare,

    pz_ref
->bake,

    pz_ref
->cut,

    pz_ref
->box.

    
* Return the concrete product which have been created

    pz
= pz_ref.

    endmethod.

    endclass.

     具体产品类:

    *OK, now we can concrete product

    
class nystylepizza1 definition

    inheriting from pizza.

    
public section.

    methods:

    constructor.

    endclass.

    
class nystylepizza1 implementation.

    method constructor.

    call method
super->constructor.

    name
= 'NY style Sauce and Cheese Pizza'.

    dough
= 'Thin Crust Dough'.

    sauce
= 'Marinara Sauce'.

    rtab
-str = 'Grated Reggiano Cheese'.

    append rtab to itab.

    endmethod.

    endclass.

    
class chistylepizza1 definition

    inheriting from pizza.

    
public section.

    methods:

    constructor,

    cut redefinition.

    endclass.

    
class chistylepizza1 implementation.

    method constructor.

    call method
super->constructor.

    name
= 'Chicago Style Deep Dish Cheese Pizza'.

    dough
= 'Extra Thick Crust Dough'.

    sauce
= 'Plum Tomato Sauce'.

    rtab
-str = 'Shredded Mozzarella Cheese'.

    append rtab to itab.

    endmethod.

    method cut.

    write:
/ 'Cutting the pizza into square slices'.

    endmethod.

    endclass.

    具体创建类:

    *Now we can define the concrete creator

    
class ny definition inheriting from pizzastore.

    
protected section.

    methods:

    createpizza redefinition.

    endclass.

    
class ny implementation.

    method createpizza.

    
case pz_name2.

    when
'cheese'.

    create object pz type nystylepizza1.

    when
'vegie'.

    when
'clam'.

    when
'pepperoni'.

    endcase.

    endmethod.

    endclass.

    
class chi definition inheriting from pizzastore.

    
protected section.

    methods:

    createpizza redefinition.

    endclass.

    
class chi implementation.

    method createpizza.

    
case pz_name2.

    when
'cheese'.

    create object pz type chistylepizza1.

    when
'vegie'.

    when
'clam'.

    when
'pepperoni'.

    endcase.

    endmethod.

    endclass.
0
相关文章