{"id":315,"date":"2023-05-11T09:40:08","date_gmt":"2023-05-11T07:40:08","guid":{"rendered":"https:\/\/oopm.org\/?page_id=315"},"modified":"2025-01-16T06:49:07","modified_gmt":"2025-01-16T05:49:07","slug":"3-2-references","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=315","title":{"rendered":"3.3 Introducing a Customer class"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages315&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages315&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"has-primary-color has-text-color wp-block-paragraph\">Customers of a bank and thereby potential owners of an account will usually have a profile with more than just the name (like &#8220;John Smith&#8221;). In addition, the same customer may have more than one account in the bank, and there may be other situations where  the profile of a customer is needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With classes of objects, the way to model this is to introduce the class <code>Customer<\/code> with attributes representing the properties of a customer profile.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String):\n   addr: <strong>var<\/strong> String         \n   email: <strong>var<\/strong> String<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"184\" height=\"139\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/01\/classCustomer.jpg\" alt=\"\" class=\"wp-image-3146\" style=\"width:168px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">We may declare a <code>Customer<\/code> object as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JohnSmithProfile: <strong>obj<\/strong> Customer(\"John Smith\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We may subsequently assign values to the data-items <code>addr<\/code> and <code>email<\/code> of <code>JohnSmith<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JohnSmithProfile.addr := \"1011 Eagle Road, Moon Town, Utopia\"\nJohnSmithProfile.email := \"john.smith@utopia.org\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We would like to be able to refer to the <code>JohnSmithProfile<\/code>-object from the <code>Account<\/code> object. In order to handle this, we introduce the mechanism of a <em>variable reference<\/em>, which may be declared as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aCustomer: <strong>ref<\/strong> Customer    <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The variable, <code>aCustomer<\/code> may refer to different <code>Customer<\/code>-objects during program execution. While the data-items we have seen so far (<code>balance<\/code> and <code>interestRate<\/code>) may hold different <code>float<\/code> values representing different values of <code>balance<\/code> and <code>interestRate<\/code>, the data-item <code>aCustomer<\/code> is a reference data-item that may refer to <code>Customer<\/code> objects.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">We may e.g. set <code>aCustomer<\/code> to refer to the <code>JohnSmithProfile<\/code>-object using an assignment statement:<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>aCustomer := JohnSmithProfile<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<style>\n#XXX2 {\n  width: 40%;\n  padding-left: 15px;\n  margin-left: 15px;\n  float: right;\n  background-color: lightgray;\n  font-size: 0.80em;\n}\n<\/style>\n<div id=\"XXX2\">The term <em>ghost object<\/em> is an object that is a placeholder for items used in an example &#8211; they do not make sense by themself.<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In order to illustrate the above, we place the code fragments in a <em>ghost<\/em> object, <code>aGhost<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aGhost: <strong>obj<\/strong>\n   aCustomer: <strong>ref<\/strong> Customer  \n   JohnSmithProfile: <strong>obj<\/strong> Customer(\"John Smith\")\n   JohnSmithProfile.addr := \"1011 Eagle Road, Moon Town, Utopia\"\n   JohnSmithProfile.email := \"john.smith@utopia.org\"\n   aCustomer := JohnSmithProfile\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The snapshot shows the situation after the last assignment as marked by the red arrow (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">&#8211;&gt;<\/mark>):<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"771\" height=\"169\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/JohnSmithProfile-1.jpg\" alt=\"\" class=\"wp-image-9535\" style=\"width:598px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/JohnSmithProfile-1.jpg 771w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/JohnSmithProfile-1-300x66.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/JohnSmithProfile-1-768x168.jpg 768w\" sizes=\"(max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The class <code>Account<\/code> is correspondingly changed so that the <code>owner<\/code> of<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-primary-color\">an<\/mark> <code>Account<\/code> is a reference to a <code>Customer<\/code> object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float\n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance:= balance - amount\n      newB :=<strong> <\/strong>balance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We now use a reference variable, <code>owner<\/code>, to represent the owner of class <code>Account<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>owner: <strong>ref<\/strong> Customer   <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>Account<\/code>-object now has a reference to a <code>Customer<\/code> object representing the owner of the account. We may create an <code>Account<\/code> object with the <code>Owner<\/code> object <code>JonhSmithsProfile<\/code> as the actual parameter as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>account_1010: <strong>obj<\/strong> Account(JohnSmithsProfile)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Again, we place the above code in a ghost object to illustrate the situation where <code>aCustomer<\/code> and <code>account_1010.owner<\/code> both refer to the same <code>Customer<\/code> object. The following snapshot shows the situation at the end of execution of <code>aClerk<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aGhost: <strong>obj<\/strong>\n   aCustomer: <strong>ref<\/strong> Customer  \n   JohnSmithProfile: <strong>obj<\/strong> Customer(\"John Smith\")\n   account_1010:&nbsp;<strong>obj<\/strong>&nbsp;Account(JohnSmithsProfile)\n   JohnSmithProfile.addr := \"1011 Eagle Road, Moon Town, Utopia\"\n   JohnSmithProfile.email := \"john.smith@utopia.org\"\n   aCustomer := JohnSmithProfile\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, both <code>aCustomer<\/code> and the <code>owner<\/code> attribute of the <code>Account<\/code> object refers to the same <code>Customer<\/code> object.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"822\" height=\"421\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/ReferenceFromAccountToCustomer.jpg\" alt=\"\" class=\"wp-image-11267\" style=\"width:599px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/ReferenceFromAccountToCustomer.jpg 822w, https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/ReferenceFromAccountToCustomer-300x154.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/ReferenceFromAccountToCustomer-768x393.jpg 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><figcaption class=\"wp-element-caption\">Reference owner from Account object to Customer object<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We create yet another <code>Account<\/code> for John Smith using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>account_1011: <strong>obj<\/strong> Account(JohnSmithsProfile)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assuming we add the above declaration to our ghost object, this situation may be illustrated by the following snapshot:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"477\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/twoReferencesFromAccountToCustomer-1.jpg\" alt=\"\" class=\"wp-image-11269\" style=\"width:537px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/twoReferencesFromAccountToCustomer-1.jpg 726w, https:\/\/oopm.org\/wp-content\/uploads\/2025\/01\/twoReferencesFromAccountToCustomer-1-300x197.jpg 300w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><figcaption class=\"wp-element-caption\">Two owner references from Account objects to Customer object<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In a paper based bank the fact that two or more accounts are owned by the same customer is represented by all these accounts having the same name on the owner field of the account sheet. In a computerised bank system this would also be possible, however, with objects we have seen that there is an alternative where the name (and other types of information) of a customer is represented by attributes of an object and thereby maintained in one place only.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The  details of reference assignment are described in section <script>mkRef(\"Reference data-items\")<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=315\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Customers of a bank and thereby potential owners of an account will usually have a profile with more than just the name (like &#8220;John Smith&#8221;). In addition, the same customer may have more than one account in the bank, and there may be other situations where the profile of a customer is needed. With classes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":231,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-315","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315"}],"version-history":[{"count":129,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/315\/revisions"}],"predecessor-version":[{"id":11270,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/315\/revisions\/11270"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/231"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}