{"id":3396,"date":"2024-02-08T13:23:55","date_gmt":"2024-02-08T12:23:55","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3396"},"modified":"2025-01-14T14:34:34","modified_gmt":"2025-01-14T13:34:34","slug":"5-4-composite-values","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3396","title":{"rendered":"5.4 Composite values"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3396&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=wpv2pages3396&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=\"wp-block-paragraph\">In the previous chapters, we have introduced objects and classes for representing physical<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>entities<span style=\"white-space: normal; font-family: var(--wp--preset--font-family--work-sans); font-size: var(--wp--preset--font-size--normal);\">&nbsp;like accounts and customers<\/span>; we have introduced references to objects, primitive values like float, integer, char, Boolean, and String<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>for representing properties of physical entities, like owner, balance, and interestRate &#8211; cf. section <script>mkRef(\"On representing phenomena\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In some cases primitive value types are not adequate for the types of data items. In a domain where e.g. date (in terms of year, month and day) and time of day (in terms of hour, minute, second) are required to represent<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>values of properties, we need composite values in order to represent these properties.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is important here to notice that objects and values are different entities &#8211; objects have state that may vary over time whereas values are timeless abstractions &#8211; we discuss this further in section <script>mkRef(\"Objects and values\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We do, however, need objects to <em>implement<\/em> values, but such objects differ from objects <em>representing<\/em> physical entities. Objects representing values are called <em>value objects<\/em>, and a class defining value objets is called a <em>value type<\/em> or just a <em>type<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the United States the notation for date and time differs from most of the rest of the world. In the US a date is written as month-day-year whereas the rest of the world use day-month-year. A time of day in the USA is written using a 12-hour clock and the suffixes a.m. and p.m. In addition, many other formats are used like year-mont-day. In the examples below, we just represent dates and time of days that may be in either US-notation og non-US notation.<\/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\" style=\"flex-basis:40%\">\n<p class=\"wp-block-paragraph\">In qBeta a value type is defined by means of a class with the <code>Value<\/code> as superclass. <code>Value<\/code> is actually the name of a class and we explain how this works in section <script>mkRef(\"Composite values\");<\/script>. For now, just think of <code>Value<\/code> as a keyword like <code><strong>class<\/strong><\/code>.<\/p>\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 fetchpriority=\"high\" decoding=\"async\" width=\"405\" height=\"343\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/DateAndTimeOfDayAsValue.jpg\" alt=\"\" class=\"wp-image-10366\" style=\"width:367px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/DateAndTimeOfDayAsValue.jpg 405w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/DateAndTimeOfDayAsValue-300x254.jpg 300w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><figcaption class=\"wp-element-caption\">Date and TimeOfDay as special Value types<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Date(year, month, day: <strong>var<\/strong> Integer): Value\n   setFormat(asUS: <strong>var<\/strong> Boolean):\n      useUSformat := US\n   asString -&gt; S: <strong>var<\/strong> String:\n      ...\n   useUSformat: <strong>var<\/strong> Boolean\n\n<strong>class<\/strong> TimeOfDay(hour, minute, second: <strong>var<\/strong> Integer): Value\n   setFormat(asUS: <strong>var<\/strong> Boolean):\n      useUSformat := US\n   asString -&gt; S: <strong>var<\/strong> String:\n      ...\n   useUSformat: <strong>var<\/strong> Boolean<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Date<\/code> has data items <code>year<\/code>, <code>month<\/code>, <code>day<\/code>. November 11, 1949 will therefore be represented by a <code>Date<\/code> value with <code>year = 1949<\/code>, <code>month = 11<\/code> and <code>day = 11<\/code>, i.e <code>Date(1949, 11, 11)<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, class <code>Date<\/code> has:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>a Boolean <code>useUSFormat<\/code>, representing whether or not to use US notation when displaying the <code>Date<\/code> &#8211; <code>true<\/code>, US-notation is used &#8211; and since a <code>Boolean<\/code> variable by default is <code>false<\/code>, non-US notation is the default;<\/li>\n\n\n\n<li>a method <code>setFormat<\/code> that may be used to set the format;<\/li>\n\n\n\n<li>a method <code>asString<\/code> that returns a <code>String<\/code> representing the <code>Date<\/code> in either US-notation or non-US-notation.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>TimeOfDay<\/code> has a similar structure and <code>TimeOfDay(1,2,3)<\/code> represents the time of day being 1 hour, 2 minutes and 3 seconds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following example illustrates the difference between references (to objects) and values. We have added a <code>Date<\/code> property <code>customerSince<\/code> to <code>Customer<\/code>:<\/p>\n\n\n\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\n   customerSince: <strong>var<\/strong> Date<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In a similar way, we have also added a data-item, <code>creationDate<\/code> of type <code>Date<\/code> to <code>Account<\/code>:<\/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   creationDate: <strong>var<\/strong> Date\n   -\"- <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The diagram below illustrates the difference between a reference data-item (<code>owner<\/code>) and value data-items (<code>creationDate<\/code>, <code>customerSince<\/code>).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"715\" height=\"311\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/creationDateObject-1.jpg\" alt=\"\" class=\"wp-image-8530\" style=\"width:607px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/creationDateObject-1.jpg 715w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/creationDateObject-1-300x130.jpg 300w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><figcaption class=\"wp-element-caption\">creationDateObject<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>owner<\/code> refers to a separate <code>Customer<\/code> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The primitive value data-items <code>balance<\/code>, and <code>interestRate<\/code> are shown embedded in the <code>Account<\/code> object. The same is the case for the <code>String<\/code> values in <code>Customer<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The composite value object <code>creationDate<\/code> is also embedded in the <code>Account<\/code> object since it is a sole property of the <code>Account<\/code> object. It is not possible for other objects to have a reference to such a value object. This is an important distinction between objects and values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Another example using composite values <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the following we have another example of using composite values.  In the bank domain, each account keeps track of transactions with information about date and time of the transaction, and what kind of transaction (like <code>deposit<\/code> and <code>withdraw<\/code>). This information is important for the bank, and for the customer in order to check what kind of transaction was done when. A transaction is represented by instances of class <code>Transaction<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Transaction(\n   what: <strong>var<\/strong> String, \n   whichDate: <strong>var<\/strong> Date,\n   whichTime: <strong>var<\/strong> TimeOfDay,\n   whichAmount: <strong>var<\/strong> float):\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The three dots <code>'...'<\/code> indicates possible additional attributes of class <code>Transaction<\/code>, but in the examples in this book, we only make use of the attributes defined as parameters.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We add a <code>Set<\/code>-object, <code>transactions<\/code>, to keep track of the transactions on an <code>Account<\/code>. The methods <code>addInterest<\/code>, <code>deposit<\/code>, and <code>withdraw<\/code> add transaction objects to this set:<\/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   transactions: <strong>obj<\/strong> Set(Transaction)\n   addInterest:\n      interest: <strong>var<\/strong> (balance * interestRate) \/ 100\n      balance := balance + interest\n      transactions.insert(\n         Transaction(\"interest\", clock.today, clock.now, interest))\n   deposit(amount: <strong>var<\/strong> float):\n      balance := balance + amount\n      transactions.insert(\n         Transaction(\"deposit\", clock.today, clock.now, amount))\n   withdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n      balance:= balance - amount\n      transactions.insert(\n         Transaction(\"withdraw\", clock.today, clock.now, amount))\n      newB := balance <\/code><\/pre>\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=3396\" 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>In the previous chapters, we have introduced objects and classes for representing physical entities&nbsp;like accounts and customers; we have introduced references to objects, primitive values like float, integer, char, Boolean, and String for representing properties of physical entities, like owner, balance, and interestRate &#8211; cf. section . In some cases primitive value types are not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2270,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3396","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\/3396","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=3396"}],"version-history":[{"count":91,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3396\/revisions"}],"predecessor-version":[{"id":11135,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3396\/revisions\/11135"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2270"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}