{"id":8014,"date":"2024-11-29T05:00:40","date_gmt":"2024-11-29T05:00:40","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=8014"},"modified":"2024-11-29T05:16:02","modified_gmt":"2024-11-29T05:16:02","slug":"explain-observer-pattern-in-angularjs","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/","title":{"rendered":"Explain Observer Pattern in AngularJS"},"content":{"rendered":"<p>The Observer Pattern is a behavioral design pattern widely used in <a href=\"https:\/\/blog.webnersolutions.com\/role-of-software-developer-in-testing\/\">software development<\/a> to establish a one-to-many dependency between objects. In the context of <a href=\"https:\/\/studysection.com\/blog\/viewencapsulation-in-angular\/\">Angular<\/a> and AngularJS, this pattern plays a crucial role in facilitating communication between components, allowing them to react to changes in state or data.<\/p>\n<p><strong>Overview of the Observer Pattern:<\/strong> In the Observer Pattern, there are two main roles: the Subject and the Observer. The Subject is the object that holds the state or data of interest, and the Observer is the object that wants to be notified of any changes in the Subject.<\/p>\n<p><strong>Key Components:<\/strong><\/p>\n<ol>\n<li><strong>Subject:<\/strong> The entity that maintains a list of observers, provides methods to attach and detach observers, and notifies them of state changes.<\/li>\n<li><strong>Observer:<\/strong> The object that is interested in the state changes of the Subject. It registers itself with the Subject, and when a change occurs, it is notified through a specific method.<\/li>\n<\/ol>\n<p><strong>Example in Angular\/AngularJS:<\/strong><br \/>\nLet&#8217;s consider a simple scenario where we have a counter component, and we want another component to be notified whenever the counter value changes.<\/p>\n<p><code>Subject - CounterService:<br \/>\nimport { Injectable } from '@angular\/core';<br \/>\nimport { Subject } from 'rxjs';<br \/>\n@Injectable({<br \/>\nprovidedIn: 'root',<br \/>\n})<br \/>\nexport class CounterService {<br \/>\nprivate counterSubject = new Subject();<br \/>\ngetCounterObservable() {<br \/>\nreturn this.counterSubject.asObservable();<br \/>\n}<br \/>\nupdateCounter(value: number) {<br \/>\nthis.counterSubject.next(value);<br \/>\n}<br \/>\n}<\/code><\/p>\n<p><strong>Observer &#8211; DisplayComponent:<\/strong><\/p>\n<p><code>import { Component, OnInit } from '@angular\/core';<br \/>\nimport { CounterService } from '..\/services\/counter.service';<br \/>\n@Component({<br \/>\nselector: 'app-display',<br \/>\ntemplate: '<br \/>\nCurrent Counter Value: {{ counterValue }}<br \/>\n',<br \/>\n})<br \/>\nexport class DisplayComponent implements OnInit {<br \/>\ncounterValue: number = 0;<br \/>\nconstructor(private counterService: CounterService) {}<br \/>\nngOnInit() {<br \/>\nthis.counterService.getCounterObservable().subscribe((value) =&gt; {<br \/>\nthis.counterValue = value;<br \/>\n});<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\n<strong>Usage in Another Component:<\/strong><\/p>\n<p><code>import { Component } from '@angular\/core';<br \/>\nimport { CounterService } from '..\/services\/counter.service';<br \/>\n@Component({<br \/>\nselector: 'app-counter',<br \/>\ntemplate: `<br \/>\n<button>Increment Counter<\/button><br \/>\n<button>Decrement Counter<\/button><br \/>\n`,<br \/>\n})<br \/>\nexport class CounterComponent {<br \/>\nconstructor(private counterService: CounterService) {}<br \/>\nincrementCounter() {<br \/>\n\/\/ Logic to increment the counter<br \/>\nthis.counterService.updateCounter(\/* updated counter value *\/);<br \/>\n}<br \/>\ndecrementCounter() {<br \/>\n\/\/ Logic to decrement the counter<br \/>\nthis.counterService.updateCounter(\/* updated counter value *\/);<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>Conclusion: In this post, we explored the Observer Pattern in the context of Angular\/AngularJS using a simple example. The CounterService acts as the Subject, holding the counter value, while the DisplayComponent acts as the Observer, reacting to changes in the counter value. This pattern promotes loose coupling and enhances the maintainability and scalability of Angular applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Observer Pattern is a behavioral design pattern widely used in software development to establish a one-to-many dependency between objects.<\/p>\n","protected":false},"author":1,"featured_media":8016,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Explain Observer Pattern in AngularJS<\/title>\n<meta name=\"description\" content=\"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explain Observer Pattern in AngularJS\" \/>\n<meta property=\"og:description\" content=\"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-29T05:00:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-29T05:16:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2024\/11\/Add-a-subheading-20.png\" \/>\n\t<meta property=\"og:image:width\" content=\"940\" \/>\n\t<meta property=\"og:image:height\" content=\"788\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Explain Observer Pattern in AngularJS\",\"datePublished\":\"2024-11-29T05:00:40+00:00\",\"dateModified\":\"2024-11-29T05:16:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\"},\"wordCount\":249,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\",\"url\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\",\"name\":\"Explain Observer Pattern in AngularJS\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2024-11-29T05:00:40+00:00\",\"dateModified\":\"2024-11-29T05:16:02+00:00\",\"description\":\"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explain Observer Pattern in AngularJS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Explain Observer Pattern in AngularJS","description":"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/","og_locale":"en_US","og_type":"article","og_title":"Explain Observer Pattern in AngularJS","og_description":"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.","og_url":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2024-11-29T05:00:40+00:00","article_modified_time":"2024-11-29T05:16:02+00:00","og_image":[{"width":940,"height":788,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2024\/11\/Add-a-subheading-20.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Explain Observer Pattern in AngularJS","datePublished":"2024-11-29T05:00:40+00:00","dateModified":"2024-11-29T05:16:02+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/"},"wordCount":249,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/","url":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/","name":"Explain Observer Pattern in AngularJS","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2024-11-29T05:00:40+00:00","dateModified":"2024-11-29T05:16:02+00:00","description":"The Observer Pattern is a behavioral design pattern widely used to establish a one-to-many dependency between objects.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/explain-observer-pattern-in-angularjs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Explain Observer Pattern in AngularJS"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":133,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8014"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=8014"}],"version-history":[{"count":5,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8014\/revisions"}],"predecessor-version":[{"id":8020,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8014\/revisions\/8020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/8016"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=8014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=8014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=8014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}