<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Bi-Directional Object Reference in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236696#M1912669</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am developing a set of classes and I'm having trouble getting a bi-directional object reference to work...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an example, let's say I have a class called ZCL_EMPLOYEE and another class called ZCL_POSITION.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_EMPLOYEE has an attribute called "position" that is TYPE REF TO &lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_POSITION. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;In the constructor this object reference is instantiated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_POSITION has an attribute called "holder" that is TYPE REF TO &lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_EMPLOYEE. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;In the constructor this object reference is instantiated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;The problem when I do this is that it goes into an endless loop - I test the ZCL_EMPLOYEE object constructor, but then it creates the POSITION object which in turn creates the EMPLOYEE object, and the loop continues.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;I know I could rather use a method instead of an attribute, but I wanted to know if there is a way of achieving this using an attribute?&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;Thanks&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Aug 2015 14:44:43 GMT</pubDate>
    <dc:creator>David-Bann</dc:creator>
    <dc:date>2015-08-17T14:44:43Z</dc:date>
    <item>
      <title>Bi-Directional Object Reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236696#M1912669</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am developing a set of classes and I'm having trouble getting a bi-directional object reference to work...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an example, let's say I have a class called ZCL_EMPLOYEE and another class called ZCL_POSITION.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_EMPLOYEE has an attribute called "position" that is TYPE REF TO &lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_POSITION. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;In the constructor this object reference is instantiated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_POSITION has an attribute called "holder" that is TYPE REF TO &lt;SPAN style="font-size: 13.3333330154419px;"&gt;ZCL_EMPLOYEE. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;In the constructor this object reference is instantiated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;The problem when I do this is that it goes into an endless loop - I test the ZCL_EMPLOYEE object constructor, but then it creates the POSITION object which in turn creates the EMPLOYEE object, and the loop continues.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;I know I could rather use a method instead of an attribute, but I wanted to know if there is a way of achieving this using an attribute?&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;Thanks&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Aug 2015 14:44:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236696#M1912669</guid>
      <dc:creator>David-Bann</dc:creator>
      <dc:date>2015-08-17T14:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: Bi-Directional Object Reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236697#M1912670</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi David,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't you your exact intention but this maybe a possible way.&lt;/P&gt;&lt;P&gt;Make your constructors private and use public class-methods "create" instead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In this case you have full control over your creation-process.&lt;/P&gt;&lt;P&gt;To check, if your object has been created a time before, m&lt;SPAN style="font-size: 10pt;"&gt;ake a private static (class-data) internal table from type ref to "your own class" and store a reference of each created object from that class in this table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If someone calls your create-method check if your object still exist by comparing the values of constructor-parameters. (You can do this in a method called "compare"). &lt;SPAN style="font-size: 10pt;"&gt;If you find the object in your internal table you will use this object instead of creating a new one. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;Using a optional constructor-parameter from type of "other object" instead of the internal static table could be a solution, too. &lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt;"&gt;In case of creating the second object inside the static method "create" use this parameter for handshaking your first object. Then there is no need for creating the first object again inside the second object. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;A good way is to study &lt;A href="http://www.oodesign.com/"&gt;OO-Design-Patterns&lt;/A&gt;. In your case I used a modification of Singleton in my first try. Instead of a real Singleton from a class I used an internal table to store a Singleton from an object having the same constructor-parameter values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Greetings&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Andreas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Aug 2015 20:50:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236697#M1912670</guid>
      <dc:creator>AndyS</dc:creator>
      <dc:date>2015-08-17T20:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Bi-Directional Object Reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236698#M1912671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the detailed response Andreas!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry for the delayed response. Yes my first thought was to try exactly what you explained. I gave it a go but it still went into an infinite loop. Perhaps I didn't cover all the bases, but as I think about it I realise that the problem is in fact related to WHEN the objects are created. It seems that in ABAP-OO, all objects are created up-front and not left for later when the object is accessed.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do you know if there is a way to defer reference object construction (i.e. attributes of an object that are of another object type) until it is accessed by the calling program? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The reason I ask is because if in my above example, I were to have an attribute of "Superior Position", and then the superior position also links to the holder, or even the "subordinate positions", then the entire organisation structure would have to be built immediately when only one instance of an employee is created... That would be very bad use of memory management, especially if the calling program doesn't actually use any of the objects subsequently created. Not to mention possible performance issues...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For now I have just created methods to get the required object references so that my calling program can determine when the sub-objects should be instantiated.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 07:39:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236698#M1912671</guid>
      <dc:creator>David-Bann</dc:creator>
      <dc:date>2015-08-24T07:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: Bi-Directional Object Reference</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236699#M1912672</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi David,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;is it possible to show the important parts of your coding or an UML-diagram of the used classes? Maybe &lt;A href="http://martinfowler.com/bliki/LazyInitialization.html"&gt;Lazy-Initialization&lt;/A&gt; is helpful for you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 21:38:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/bi-directional-object-reference/m-p/11236699#M1912672</guid>
      <dc:creator>AndyS</dc:creator>
      <dc:date>2015-08-24T21:38:32Z</dc:date>
    </item>
  </channel>
</rss>

