24/09/2010

Builder Design Pattern

The builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create.


REPORT ZADN_SAMPLE.

*----------------------------------------------------------------------*
* -> CLASS cl_Meal DEFINITION
*----------------------------------------------------------------------*
CLASS cl_meal DEFINITION.

PUBLIC SECTION.

DATA : sandwich TYPE string,
sideorder TYPE string,
drink TYPE string,
offer TYPE string,
price TYPE p LENGTH 4 DECIMALS 2.

METHODS : tostring.

ENDCLASS. "cl_Meal DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_meal IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_meal IMPLEMENTATION.

METHOD tostring.

DATA : v_msg TYPE string,
v_price TYPE c LENGTH 15.

WRITE me->price TO v_price.

CONCATENATE : `Sandwich: `
me->sandwich
` - SideOrder: `
me->sideorder
` - Drink: `
me->drink
` - Offer: `
me->offer
` - Price: R$ `
v_price INTO v_msg.

WRITE / v_msg.

ENDMETHOD. "ToString

ENDCLASS. "cl_meal IMPLEMENTATION

*----------------------------------------------------------------------*
* -> CLASS cl_MealBuilder DEFINITION
*----------------------------------------------------------------------*
CLASS cl_mealbuilder DEFINITION ABSTRACT.

PUBLIC SECTION.

METHODS : addsandwich ABSTRACT,
addsideorder ABSTRACT,
adddrink ABSTRACT,
addofferitem ABSTRACT,
setprice ABSTRACT,
getmeal ABSTRACT EXPORTING p_meal TYPE REF TO cl_meal.

ENDCLASS. "cl_MealBuilder DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_MealDirector DEFINITION
*----------------------------------------------------------------------*
CLASS cl_mealdirector DEFINITION.

PUBLIC SECTION.

METHODS : makemeal IMPORTING p_mealbuilder TYPE REF TO cl_mealbuilder.

ENDCLASS. "cl_MealDirector DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_mealdirector IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_mealdirector IMPLEMENTATION.

METHOD makemeal.

p_mealbuilder->addsandwich( ).
p_mealbuilder->addsideorder( ).
p_mealbuilder->adddrink( ).
p_mealbuilder->addofferitem( ).
p_mealbuilder->setprice( ).

ENDMETHOD. "MakeMeal

ENDCLASS. "cl_mealdirector IMPLEMENTATION

*----------------------------------------------------------------------*
* -> CLASS cl_JollyVegetarianMealBuilder DEFINITION
*----------------------------------------------------------------------*
CLASS cl_jollyvegetarianmealbuilder DEFINITION INHERITING FROM cl_mealbuilder.

PUBLIC SECTION.

METHODS : constructor,
addsandwich REDEFINITION,
addsideorder REDEFINITION,
adddrink REDEFINITION,
addofferitem REDEFINITION,
setprice REDEFINITION,
getmeal REDEFINITION.

PRIVATE SECTION.

DATA : o_meal TYPE REF TO cl_meal.

ENDCLASS. "cl_JollyVegetarianMealBuilder DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_jollyvegetarianmealbuilder IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_jollyvegetarianmealbuilder IMPLEMENTATION.

METHOD constructor.

super->constructor( ).

CREATE OBJECT o_meal.

ENDMETHOD. "constructor

METHOD addsandwich.

o_meal->sandwich = `Vegeburger`.

ENDMETHOD. "addsandwich

METHOD addsideorder.

o_meal->sideorder = `Fries`.

ENDMETHOD. "addsideorder

METHOD adddrink.

o_meal->drink = `Orange Juice`.

ENDMETHOD. "adddrink

METHOD addofferitem.

o_meal->offer = `Donut Voucher`.

ENDMETHOD. "addofferitem

METHOD setprice.

o_meal->price = `4.99`.

ENDMETHOD. "setprice

METHOD getmeal.

p_meal = me->o_meal.

ENDMETHOD. "getmeal

ENDCLASS. "cl_jollyvegetarianmealbuilder IMPLEMENTATION


*----------------------------------------------------------------------*
* -> CLASS cl_mischievousbrazilianbuilder DEFINITION
*----------------------------------------------------------------------*
CLASS cl_mischievousbrazilianbuilder DEFINITION INHERITING FROM cl_mealbuilder.

PUBLIC SECTION.

METHODS : constructor,
addsandwich REDEFINITION,
addsideorder REDEFINITION,
adddrink REDEFINITION,
addofferitem REDEFINITION,
setprice REDEFINITION,
getmeal REDEFINITION.

PRIVATE SECTION.

DATA : o_meal TYPE REF TO cl_meal.

ENDCLASS. "cl_mischievousbrazilianbuilder DEFINITION


*----------------------------------------------------------------------*
* -> CLASS cl_mischievousbrazilianbuilder IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_mischievousbrazilianbuilder IMPLEMENTATION.

METHOD constructor.

super->constructor( ).

CREATE OBJECT o_meal.

ENDMETHOD. "constructor

METHOD addsandwich.

o_meal->sandwich = `X-TUDO Burger`.

ENDMETHOD. "addsandwich

METHOD addsideorder.

o_meal->sideorder = `Fries`.

ENDMETHOD. "addsideorder

METHOD adddrink.

o_meal->drink = `Caipirinha`.

ENDMETHOD. "adddrink

METHOD addofferitem.

o_meal->offer = `Weekend Happy`.

ENDMETHOD. "addofferitem

METHOD setprice.

o_meal->price = `15.49`.

ENDMETHOD. "setprice

METHOD getmeal.

p_meal = me->o_meal.

ENDMETHOD. "getmeal

ENDCLASS. "cl_mischievousbrazilianbuilder IMPLEMENTATION

************************************************************************
************************************************************************
* *
* PROGRAM FLOW LOGIC *
* *
************************************************************************

START-OF-SELECTION.

DATA : o_director TYPE REF TO cl_mealdirector,
o_jvmb TYPE REF TO cl_jollyvegetarianmealbuilder,
o_mvbb TYPE REF TO cl_mischievousbrazilianbuilder,
o_vegmeal TYPE REF TO cl_meal,
o_brameal TYPE REF TO cl_meal.

CREATE OBJECT : o_director,
o_jvmb,
o_mvbb.

o_director->makemeal( EXPORTING p_mealbuilder = o_jvmb ).

o_jvmb->getmeal( IMPORTING p_meal = o_vegmeal ).

o_director->makemeal( EXPORTING p_mealbuilder = o_mvbb ).

o_mvbb->getmeal( IMPORTING p_meal = o_brameal ).

o_vegmeal->tostring( ).

o_brameal->tostring( ).

Nenhum comentário:

Postar um comentário