Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93554
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-27 21:11
文章分类

全部博文(38)

我的朋友

分类: 信息化

2015-05-17 11:57:16

REPORT  zthomas_oo_pattern_singleton1.
"class definition

CLASS lcl_application DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    "static method whic will return us the object reference.
    CLASS-METHODS:
       get_apps_instance
                   RETURNING value(ro_apps) TYPE REF TO lcl_application.
    METHODS:
       set_v_name
         IMPORTING
            iv_name TYPE char30,
       get_v_name
         RETURNING value(rv_name) TYPE char30.
  PRIVATE SECTION.
    "static class reference to hold the  existing object reference
    CLASS-DATA: lo_apps TYPE REF TO lcl_application.


    DATA:v_name TYPE char30.
ENDCLASS.                    "lcl_application DEFINITION

"class implementation
*----------------------------------------------------------------------*
*       CLASS lcl_application IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.


*this method will return the object reference to the calling application


  METHOD get_apps_instance.
    IF lo_apps IS INITIAL.
      "creation of the objcet
      CREATE OBJECT lo_apps.
    ENDIF.
*assigning reference back to exporting parameter
    ro_apps  = lo_apps.
  ENDMETHOD.                    "get_apps_instance


  METHOD set_v_name.
    me->v_name = iv_name.
  ENDMETHOD.                    "set_v_name


  METHOD get_v_name.
    rv_name = me->v_name.
  ENDMETHOD.                    "get_v_name


ENDCLASS.                    "lcl_application IMPLEMENTATION


START-OF-SELECTION.
*reference 1


  DATA:lo_application TYPE REF TO lcl_application.
  DATA:lv_result TYPE char30.




  WRITE: 'LO_APPLICATION'.


* calling the method which gets us the instance
* Statement CREATE OBJECT LO_APPLICATION
* would not work as the class LCL_APPLICATION instantiation
* is set to PRIVATE
  lo_application = lcl_application=>get_apps_instance( ).
*set the variable and get it back
  lo_application->set_v_name( 'this is the first object' ).


  lv_result = lo_application->get_v_name( ).
  WRITE:/ lv_result.
  CLEAR lv_result.




*  *.Reference: 2............................................
  DATA: lo_2nd_apps TYPE REF TO lcl_application.
  SKIP 2.
  WRITE: / 'LO_2ND_APPS : '.
*   calling the method which gets us the instance
* By calling GET_APPS_INSTANCE method again to get the singleton
* object, it would give the same object back and assign it to
* LO_2ND_APPS object reference. This would be varified by
* getting the value of the instance attribute V_NAME
  lo_2nd_apps = lcl_application=>get_apps_instance( ).


*  *set the variable and get it back
*  lo_application->set_v_name( 'this is the second object' ).
  lv_result = lo_2nd_apps->get_v_name( ).
  WRITE: / lv_result.
  CLEAR lv_result.
阅读(790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~