REPORT ZADN_SAMPLE.
*----------------------------------------------------------------------*
* -> Abstract - CLASS cl_car_base DEFINITION
*----------------------------------------------------------------------*
CLASS cl_car_base DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS : set_attributes IMPORTING p_color TYPE string
p_year TYPE c,
get_cartype RETURNING value(p_cartype) TYPE string.
PRIVATE SECTION.
DATA : color TYPE string,
year TYPE c LENGTH 4.
ENDCLASS. "cl_car_base DEFINITION
*----------------------------------------------------------------------*
* -> Abstract - CLASS cl_car_base IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_car_base IMPLEMENTATION.
METHOD set_attributes.
me->color = p_color.
me->year = p_year.
ENDMETHOD. "set_attributes
METHOD get_cartype.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_car_base IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_vectra DEFINITION
*----------------------------------------------------------------------*
CLASS cl_vectra DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_vectra DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_vectra IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_vectra IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'VECTRA'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_vectra IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_agile DEFINITION
*----------------------------------------------------------------------*
CLASS cl_agile DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_agile DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_agile IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_agile IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'AGILE'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_agile IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_prisma DEFINITION
*----------------------------------------------------------------------*
CLASS cl_prisma DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_prisma DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_prisma IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_prisma IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'PRISMA'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_prisma IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_fiesta DEFINITION
*----------------------------------------------------------------------*
CLASS cl_fiesta DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_fiesta DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_fiesta IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_fiesta IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'FIESTA'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_fiesta IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_focus DEFINITION
*----------------------------------------------------------------------*
CLASS cl_focus DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_focus DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_focus IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_focus IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'FOCUS'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_focus IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_ka DEFINITION
*----------------------------------------------------------------------*
CLASS cl_ka DEFINITION INHERITING FROM cl_car_base.
PUBLIC SECTION.
METHODS get_cartype REDEFINITION.
ENDCLASS. "cl_ka DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_ka IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_ka IMPLEMENTATION.
METHOD get_cartype.
p_cartype = 'KA'.
ENDMETHOD. "get_cartype
ENDCLASS. "cl_ka IMPLEMENTATION
************************************************************************
************************************************************************
* *
* FACTORY CLASSES DEFINITION *
* *
************************************************************************
*----------------------------------------------------------------------*
* -> Abstract - CLASS cl_car_factory DEFINITION
*----------------------------------------------------------------------*
CLASS cl_car_factory DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS : create_car IMPORTING p_cartype TYPE n
EXPORTING p_carinstance TYPE REF TO cl_car_base.
ENDCLASS. "cl_car_factory DEFINITION
*----------------------------------------------------------------------*
* Abstract - CLASS cl_car_factory IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_car_factory IMPLEMENTATION.
METHOD create_car.
ENDMETHOD. "create_car
ENDCLASS. "cl_car_factory IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_chevrolet_factory DEFINITION
*----------------------------------------------------------------------*
CLASS cl_chevrolet_factory DEFINITION INHERITING FROM cl_car_factory.
PUBLIC SECTION.
METHODS : create_car REDEFINITION.
ENDCLASS. "cl_chevrolet_factory DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_chevrolet_factory IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_chevrolet_factory IMPLEMENTATION.
METHOD create_car.
CASE p_cartype.
WHEN '100'.
CREATE OBJECT p_carinstance TYPE cl_vectra.
WHEN '200'.
CREATE OBJECT p_carinstance TYPE cl_agile.
WHEN '300'.
CREATE OBJECT p_carinstance TYPE cl_prisma.
ENDCASE.
ENDMETHOD. "create_car
ENDCLASS. "cl_chevrolet_factory IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_ford_factory DEFINITION
*----------------------------------------------------------------------*
CLASS cl_ford_factory DEFINITION INHERITING FROM cl_car_factory.
PUBLIC SECTION.
METHODS : create_car REDEFINITION.
ENDCLASS. "cl_ford_factory DEFINITION
*----------------------------------------------------------------------*
* -> Concrete - CLASS cl_ford_factory IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_ford_factory IMPLEMENTATION.
METHOD create_car.
CASE p_cartype.
WHEN '100'.
CREATE OBJECT p_carinstance TYPE cl_fiesta.
WHEN '200'.
CREATE OBJECT p_carinstance TYPE cl_focus.
WHEN '300'.
CREATE OBJECT p_carinstance TYPE cl_ka.
ENDCASE.
ENDMETHOD. "create_car
ENDCLASS. "cl_ford_factory IMPLEMENTATION
************************************************************************
************************************************************************
* *
* PROGRAM FLOW LOGIC *
* *
************************************************************************
START-OF-SELECTION.
PARAMETERS : p_type1 TYPE n LENGTH 3 OBLIGATORY DEFAULT '100',
p_type2 TYPE n LENGTH 3 OBLIGATORY DEFAULT '100'.
DATA : o1_chevrolet_factory TYPE REF TO cl_car_factory,
o2_ford_factory TYPE REF TO cl_car_factory.
DATA : o1_car TYPE REF TO cl_car_base,
o2_car TYPE REF TO cl_car_base.
DATA : v_type1 TYPE string,
v_type2 TYPE string.
CREATE OBJECT : o1_chevrolet_factory TYPE cl_chevrolet_factory,
o2_ford_factory TYPE cl_ford_factory.
o1_chevrolet_factory->create_car( EXPORTING p_cartype = p_type1
IMPORTING p_carinstance = o1_car ).
o2_ford_factory->create_car( EXPORTING p_cartype = p_type2
IMPORTING p_carinstance = o2_car ).
IF o1_car IS BOUND.
o1_car->set_attributes( EXPORTING p_color = 'BLACK'
p_year = '2010' ).
v_type1 = o1_car->get_cartype( ).
WRITE : / 'Car Type : ', v_type1.
ULINE.
ENDIF.
IF o2_car IS BOUND.
o2_car->set_attributes( EXPORTING p_color = 'BLACK'
p_year = '2010' ).
v_type2 = o2_car->get_cartype( ).
WRITE : / 'Car Type : ', v_type2.
ENDIF.
22/09/2010
Factory Method Design Pattern
The factory method pattern is a design pattern that allows for the creation of objects without specifying the type of object that is to be created in code. A factory class contains a method that allows determination of the created type at run-time.
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário