{"id":5904,"date":"2024-06-27T17:18:50","date_gmt":"2024-06-27T15:18:50","guid":{"rendered":"https:\/\/oopm.org\/?page_id=5904"},"modified":"2024-11-26T14:36:30","modified_gmt":"2024-11-26T13:36:30","slug":"printable-an-interface-like-aspect","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=5904","title":{"rendered":"18.2 Interface-like aspects"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages5904&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=wpv2pages5904&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\">The addressable aspect in the previous section was an example of a general aspect with both data-items and methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following example illustrates aspects that only have methods. In other languages this kind of aspects are defined by a special <em>interface<\/em> definition, but we will define also these kinds of aspects by means of classes. Usually interface only have method signatures, i.e. name, parameters and return type, while aspects used for interface may have methods with more than signatures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We start out by the simple aspect of being printable:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> printable:\n   print:&lt;  \n      inner(print)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Different classes of objects may have a <code>printable<\/code> aspect. Here we define that <code>Person<\/code> objects are printable:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> Person(name: <strong>var<\/strong> String):\n   asPrintable: <strong>obj<\/strong> Printable\n      print::\n         name.print<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the description of <code>asPrintable<\/code> is nested within&nbsp;<code>Person<\/code>, the data item&nbsp;<code>name<\/code>&nbsp;of <code>Person<\/code> is visible, and it is therefore possible to extend print to print the&nbsp;<code>name<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>Person<\/code> object are printed like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>JohnSmith: <strong>ref<\/strong> Person\nJohnSmith := Person(\"John Smith\")\n...\nJohnSmith.asPrintable.print<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will not surprisingly print &#8220;John Smith&#8221;. A more interesting example is the case where we have a set of <code>Person<\/code> objects and apply <code>asPrintable.print<\/code> to all of objects in this set:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>persons: <strong>obj<\/strong> Set(Person)\n-- insert Person objects in this set, e.g.:\npersons.insert(Person(\"John Smith\"))\npersons.insert(Person(\"Liza Jones\"))\npersons.insert(Person(\"Mary Pole\"))\n...\npersons.scan\n   current.asPrintable.print<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The same effect <em>could<\/em> have been obtained by defining <code>Person<\/code> as a subclass of <code>Printable<\/code>, but that would classify <code>Person<\/code> to be <code>Printable<\/code>, and that is <em>not<\/em> what we want to express. Subclassing works well for classification, but for aspects we typically would like to have that a class like <code>Person<\/code> can have more than one aspect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we also have the notion of being movable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Movable: \n   move(newAddr: <strong>ref<\/strong> Address):\n      inner(move)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">then <code>Person<\/code> could also be movable:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> Address:\n   -\"-\n   print:&lt;\n      -\"-\n\n<strong>class<\/strong> Person(name: <strong>var<\/strong> String): \n   addr: <strong>obj<\/strong> Address\n\n   asPrintable: <strong>obj<\/strong> Printable\n      print::\n         name.print\n         address.print\n\n   asMovable: <strong>obj<\/strong> Movable\n      move::\n         addr.change(newAddr)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above class definitions, the properties of&nbsp;<code>Printable<\/code>&nbsp;(in this case just the method <code>print<\/code>) is also a property of&nbsp;<code>Person<\/code>, although via the name&nbsp;<code>asPrintable<\/code>. Correspondingly for&nbsp;<code>move<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark><\/code>of&nbsp;<code>Movable<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>Joe: <strong>ref<\/strong> Person\n...\nJoe := Person(\"Joe\")\nJoe.asPrintable.print\nJoe.asMovable.move(Address(\"Norway\",\"Oslo\",\"R\u00f8ahagan\", 33)) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, the above aspects, <code>Printable<\/code> and <code>Movable<\/code>, are example of interfaces in other languages, with only methods. As the methods <code>print<\/code> and <code>move<\/code> are defined independently of <code>Person<\/code> (or other classes that should be <code>Printable<\/code> and <code>Movable<\/code>, interfaces usually do not have any statements describing their actions. This is not the case for aspects that work as interfaces.<\/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=5904\" 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>The addressable aspect in the previous section was an example of a general aspect with both data-items and methods. The following example illustrates aspects that only have methods. In other languages this kind of aspects are defined by a special interface definition, but we will define also these kinds of aspects by means of classes. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3511,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5904","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\/5904","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5904"}],"version-history":[{"count":24,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5904\/revisions"}],"predecessor-version":[{"id":9881,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/5904\/revisions\/9881"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3511"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}