Factory method mode of ABAP and Design Mode

Source: Internet
Author: User

Reference: skysky

Content: defines an interface for creating objects, but the subclass determines which class to instantiate. The factory method delays the class Instantiation to the subclass.

In simple terms, the factory method mode can encapsulate the instantiation of specific types.

Structure:

Product: All products must implement this common interface, so that the class of these products can reference this interface, rather than the specific class.

Concretecreator: creates one or more products. Only the concrete creator class knows how to create these products. At the same time, it implements the factory method to actually manufacture the product.

Creator: an abstract class that implements methods for all operations on products, but does not implement factory methods. All its subclasses must implement this abstract factorymethod () method.

The instance program first provides the class diagram:

Note: The creation class is parallel to the product class because they are all abstract classes, and the abstract class has many concrete subclasses, each of which has its own specific implementation.

The test procedure is as follows:

Report zbo_dp_004_re.

* Factory method pattern used interface and class

Include zbo_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'

Processing ing PZ = pz_ref.

* Get the pizza's name

Data: ls type string.

Call method pz_ref-> getname

Processing ing NA = ls.

Write:/'ethan ordered a: ', ls.

Skip.

* Book Chi store's pizza

Call method chi_ref-> orderpizza

Exporting pz_name1 = 'cheese'

Processing ing PZ = pz_ref.

* Get the pizza's name

Call method pz_ref-> getname

Processing ing NA = ls.

Write:/'joel ordered a: ', ls.

Abstract product category:

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

* Include zbo_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 100 '.

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.

Factory type:

* 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

Processing ing 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.

Specific product categories:

* 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.

Specific creation class:

* 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 'chees '.

Create object PZ type nystylepizza1.

When 'vegie '.

When 'clam '.

When 'pepperons '.

Endcase.

Endmethod.

Endclass.

Class Chi definition inheriting from pizzastore.

Protected section.

Methods:

Createpizza redefinition.

Endclass.

Class Chi implementation.

Method createpizza.

Case pz_name2.

When 'chees '.

Create object PZ type chistylepizza1.

When 'vegie '.

When 'clam '.

When 'pepperons '.

Endcase.

Endmethod.

Endclass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.