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

引用:翱翔云天

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

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

结构:

2009062916380892.gif

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

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

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

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

2009062916382426.gif

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

测试程序如下:

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.

转载于:https://www.cnblogs.com/wequst/archive/2009/06/29/1513351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值