Observer Pattern

Author Topic: Observer Pattern  (Read 562 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 139
  • Karma: 0
    • No Addiction
Observer Pattern
« on: 05-03-2016, 05:21:34 »
PHP applications, usually manipulate data. In many cases, changes to one piece of data can affect many different parts of your application’s code. For example, the price of each product item displayed on an e-commerce site in the customer’s local currency is affected by the current exchange rate. Now, assume that each product item is represented by a PHP object that most likely originates from a database; the exchange rate itself is most probably being taken from a different source and is not part of the item’s database entry. Let’s also assume that each such object has a display() method that outputs the HTML relevant to this product.

The observer pattern allows for objects to register on certain events and/or data, and when such an event or change in data occurs, it is automatically notified. In this way, you could develop the product item to be an observer on the currency exchange rate, and before printing out the list of items, you could trigger an event that updates all the registered objects with the correct rate. Doing so gives the objects a chance to update themselves and take the new data into account in their display() method

Offline TomClarke

  • Trade Count: (0)
  • Hero Member
  • *****
  • Thank You 15
  • Posts: 896
  • Karma: 0
Re: Observer Pattern
« Reply #1 on: 05-11-2016, 01:10:26 »
The observer pattern is a design pattern that defines a link between objects so that when one object's state changes, all dependent objects are updated automatically. This pattern allows communication between objects in a loosely coupled manner.


Related Topics

  Subject / Started by Replies Last post
1 Replies
Last post 09-29-2014, 05:44:41
by kiranshahweb