ABAP类使用单例模式

What is the concept of the Singleton design pattern?

The concept of restricting the instantiation of the class toonly and only to one object is called Singleton. As name suggests, it willrestrict to create only one instance of a class. The class will have logic inplace which will deny if the application will ask for more than one instance.

Let's try to understand with following examples:

  • We have an application which will bring the pay stub of an employee for current month. In a standard system, there will be only one active salary account forthe employee with the company. Because of this fact, we should only create oneobject of the employee's salary account. In program, we are creating this object in a loop. So, what happens if we don't have a design pattern which willrestrict it for creating more than one instance of that object? Application will create; rather overwrite an instance of the class.
  • In WebDynpro ABAP when you want to enhance SAP standard components wherein a child component wants to share data with the parent component. You can't add an interface node, not any interface method ina WebDynpro Enhancement component controller. Check the official announcementfrom SAP: SAP note 1476699.

So only way to share data between standard WebDynpro components is a singleton class.

This is how you would create a singleton class:

1. Create a class in SE24 which has instantiation property as private, this would create a constructor which is private.

ABAP类使用单例模式

2. Since constructor is private, to instantiate the class, we will need to create a Public Static method. In this public static method we will instantiate the class. To do this we will create an attribute of the type singleton class and store the reference in this attribute.

ABAP类使用单例模式
3. Code snippet for the public static method is

ABAP类使用单例模式




IF gref_obj IS not BOUND.

CREATE OBJECT gref_obj TYPE zash_singleton_test.

ENDIF.

ref_obj = gref_obj.

 

4. Now create an attribute at instance level of type Public which will be used to store the value to be shared.

ABAP类使用单例模式


5. Now create an instance of the class in Child component and set the values to beshared in WDDOINIT of a child Componentcontroller

6. Read this value from parent component's WDDOINIT of Componentcontroller

DATA lo_cmp_usage TYPE REF TO if_wd_component_usage.

lo_cmp_usage = wd_this->wd_cpuse_z_sub_comp( ).

IF lo_cmp_usage->has_active_component( ) IS INITIAL.

lo_cmp_usage->create_component( ).

ENDIF.

DATA: lo_obj1 TYPE REF TO zash_singleton_test.

DATA z_retrieved_value TYPE string.

 

 

lo_obj1 ?= zash_singleton_test=>get_instance( ).

IF lo_obj1 IS BOUND.

z_retrieved_value = lo_obj1->a_test_string.

ENDIF.

Now this pattern can be used in Custom developed WebDynproABAP Components as well to share data between parent and child components this is light weight data sharing approach.

 

This approach also better allows UIBBs (User InterfaceBuilding Blocks) implemented via feeder classes to be integrated with WDComponents within the Floorplan Manager.


转载自SAP部落格:http://sapblog.org/archives/380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ABAP (Advanced Business Application Programming) OO (Object-Oriented)环境中,单例模式和工厂模式都是常用的软件设计模式。 **1. 单例模式(Singleton Pattern)**: ABAP中的单例模式用于确保一个只有一个实例存在,并提供全局访问点。这种模式常用于需要共享资源或创建复杂初始化过程的对象。在ABAP中,你可以通过设置私有构造函数并提供一个静态方法返回单例对象来实现。例如: ```abap CLASS lcl_my_singleton DEFINITION. PUBLIC SECTION. TYPE s_SINGLETON = VALUE #( 'INSTANCE' => REF TO cl_my_singleton ). PRIVATE SECTION. DATA: sy_instance TYPE s_SINGLETON. ENDCLASS. CLASS-METHODS get_instance IMPORTING none. ... CLASS-METHODS new RETURNING VALUE(of_type) like sy_instance. ``` 当你通过`get_instance()`获取实例时,如果没有创建过,它会新创建;如果有,就直接返回已有的实例。 **2. 工厂模式(Factory Pattern)**: 这种模式用于隐藏对象的创建细节,并由工厂方法负责生成对象。用户只需要关心如何使用对象,而不需要知道具体如何创建。在ABAP中,可以定义一个抽象工厂或接口,然后提供具体的工厂来创建不同型的产品: ```abap INTERFACE i_factory. METHODS create_product TYPEREF TO cl_my_product. ENDINTERFACE. CLASSES cl_abstract_factory IMPLEMENTATION OF i_factory. ... CLASS lcl_my_product_factory IMPLEMENTATION OF i_factory. METHOD create_product. ... ENDMETHOD. ENDCLASS. ``` 使用时,开发者可以通过`i_factory`接口创建各种产品: ```abap DATA factory TYPE REF TO i_factory. factory->create_product(). " 返回具体产品的实例 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值