{"id":7963,"date":"2024-10-03T12:27:33","date_gmt":"2024-10-03T10:27:33","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7963"},"modified":"2024-12-30T10:19:31","modified_gmt":"2024-12-30T09:19:31","slug":"17-3-2-representing-the-observer-pattern-as-aspects","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7963","title":{"rendered":"18.3.2 Representing the observer pattern as aspects"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7963&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=wpv2pages7963&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 example in the previous section is meant for illustrating the observer pattern, but from a modeling point-of-view, it does not make sense that <code>fireAlarm<\/code> is a subclass of <code>Subject<\/code> and that <code>FireDepartment<\/code>, etc. are subclasses of <code>Observer<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will show how the observer pattern may be represented as aspects. For this purpose, we will use the bank example. A scenario that calls for adding subject\/observer aspects to the bank system is the following: Every day the bank checks if there has been some suspicious transactions on accounts, and in case, both a special alarm part of the bank&nbsp;<em>and<\/em>&nbsp;the actual customer are notified of the event. This is done by giving each account a subject aspect and both alarm and each customer an observer aspect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Account<\/code>&nbsp;objects have a subject aspect represented by the intrinsic object&nbsp;<code>asSubject<\/code>, and&nbsp;<code>Customer<\/code>&nbsp;objects and the&nbsp;<code>alarm<\/code>&nbsp;object have an observer aspect represented by the intrinsic object&nbsp;<code>asObserver<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BankSysEx: <strong>obj<\/strong>\n   <strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):   \n      -\"-\n      asSubject: <strong>obj<\/strong> Subject\n\n   <strong>class<\/strong> Customer(id: <strong>var<\/strong> Integer):\n      -\"-\n      asObserver: <strong>obj<\/strong> Observer\n\n   alarm: <strong>obj<\/strong>\n      asObserver: <strong>obj<\/strong> Observer\n   ... <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The subject and observer aspects are now represented as properties of intrinsic objects of <code>Account<\/code> objects, of <code>Customer<\/code> objects, and of the <code>alarm<\/code> object. This is illustrated in the following figure, in the first round with the types of the intrinsic objects being <code>Subject<\/code> and <code>Observer<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"579\" height=\"581\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-9.jpg\" alt=\"\" class=\"wp-image-10604\" style=\"width:539px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-9.jpg 579w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-9-300x300.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-9-150x150.jpg 150w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As for the <code>Firealarm<\/code> example, we also have to extend <code>ObservedSubject<\/code> to include information about the accounts. The type of&nbsp;<code>asSubject<\/code>&nbsp;object cannot just be&nbsp;<code>Subject<\/code>, as it has to report events that has to do with&nbsp;<code>Account<\/code>-objects. Since we have two types of observers, <code>Customer<\/code> objects and the <code>alarm<\/code>, we introduce a class <code>AccountSubject<\/code> being a subclass of <code>Subject<\/code>, and we use the class as the type of <code>asSubject<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> AccountSubject(whichAccount: <strong>ref<\/strong> Account): Subject\n   issueWithTransactions:\n      \"There is an issue with the account of: \".print\n      whichAccount.owner.print\n   inner(AccountSubject)\n\n<strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):\n   -\"-\n   asSubject: <strong>obj<\/strong> AccountSubject(this(Account))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In a similar way, the&nbsp;<code>asObserver<\/code>&nbsp;objects of&nbsp;<code>Customer<\/code>&nbsp;and&nbsp;<code>alarm<\/code> are&nbsp;defined&nbsp;as singular objects derived from class&nbsp;<code>Observer<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Customer(name: <strong>var<\/strong> String):\n   asObserver: <strong>obj<\/strong> Observer\n      <strong>class<\/strong> ObservedSubject:: AccountSubject  \n      notify::\n         theSubject.issueWithTransactions   \n...\nalarm: <strong>obj<\/strong>\n   asObserver: <strong>obj<\/strong> Observer\n      <strong>class<\/strong> ObservedSubject:: AccountSubject \n      notify::\n         theSubject.issueWithTransactions<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"665\" height=\"688\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-8.jpg\" alt=\"\" class=\"wp-image-10602\" style=\"width:579px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-8.jpg 665w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/AccountAlarmCustomer-8-290x300.jpg 290w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next we show how <code>Customer<\/code> objects and <code>alarm<\/code> may subscribe to events in <code>Account<\/code> objects. This is done in a method <code>newAccount<\/code> in <code>BankSysEx<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>newAccount(owner: <strong>ref<\/strong> Customer):\n   acc: <strong>ref<\/strong> Account\n   acc := Account(owner)\n   theAccountsFile.insert(acc)\n   acc.asSubject.subscribe(owner.asObserver)\n   acc.asSubject.subscribe(alarm)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>newAccount<\/code> creates a new <code>Account<\/code> for the <code>Customer<\/code> referred to by <code>owner<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First a new <code>Account<\/code> object is generated and a reference to it is assigned to <code>acc<\/code>.<\/li>\n\n\n\n<li>Next this reference is inserted in the <code>theAccountsFile<\/code>, which is the set of accounts of <code>BankSysEx<\/code>.<\/li>\n\n\n\n<li>Then the <code>owner<\/code> aspect (<code>owner.asObserver<\/code>) of the new <code>Account<\/code> is subscribed to the subject aspect of <code>acc<\/code> (<code>acc.asSubject<\/code>).<\/li>\n\n\n\n<li>Finally the <code>alarm<\/code> subscribes to the subject aspect of <code>acc<\/code>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"445\" height=\"301\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/NewAccountOSD.jpg\" alt=\"\" class=\"wp-image-10184\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/NewAccountOSD.jpg 445w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/NewAccountOSD-300x203.jpg 300w\" sizes=\"(max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To sum up, the overall structure of <code>BankSysEx<\/code> is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BankSysEx: <strong>obj<\/strong>\n   <strong>class<\/strong> AccountSubject(whichAccount:<strong> ref<\/strong> Account): Subject\n      -\"-\n   <strong>class<\/strong> Account(owner: <strong>ref<\/strong> Customer):   \n      -\"-   \n      asSubject: <strong>obj<\/strong> AccountSubject\n\n   <strong>class<\/strong> Customer(id: <strong>var<\/strong> Integer):\n      -\"-\n      asObserver: <strong>obj<\/strong> Observer\n         ObservedSubject:: AccountSubject\n         -\"-  \n \n   alarm: <strong>obj<\/strong>\n      asObserver: <strong>obj<\/strong> Observer\n         ObservedSubject:: AccountSubject\n         -\"- \n   newAccount(owner:&nbsp;ref&nbsp;Customer):\n      -\"-\n   accountsFile: <strong>obj<\/strong> OrderedList(Account)\n   ... <\/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=7963\" 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 example in the previous section is meant for illustrating the observer pattern, but from a modeling point-of-view, it does not make sense that fireAlarm is a subclass of Subject and that FireDepartment, etc. are subclasses of Observer. In this section, we will show how the observer pattern may be represented as aspects. For this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7529,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7963","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\/7963","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=7963"}],"version-history":[{"count":33,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7963\/revisions"}],"predecessor-version":[{"id":10846,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7963\/revisions\/10846"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7529"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}