skip to Main Content

Welcome

Say hello to the toggle bar. This is an optional section you can use to display any content you'd like. Simply select a page from the theme panel and the content of the page will display here. You can even use the drag and drop builder to create this! This is a perfect place for your company mission statement, alerts, notices or anything else.

Get In Touch

Email: support@total.com
Phone: 1-800-Total-Theme
Address: Las Vegas, Nevada

Our Location

togglebar-map

Observer Pattern

Post Series: Behavioral Design Pattern

This pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.

Observer Design Pattern C#

  • Subject: This is a class that contains a private collection of the observers that are subscribed to a subject for notification by using Notify operation.
  • ConcreteSubject: This is a class that maintains its own state. When a change is made to its state, the object calls the base class’s Notify operation to indicate this to all of its observers.
  • Observer: This is an interface which defines an operation Update, which is to be called when the subject’s state changes.
  • ConcreteObserver: This is a class that implements Observer interface and examines the subject to determine which information has changed.

Implement

Flow

  1. ConcreteSubject (Account) notify to observers(Logger và Mailer, Security) when as there is an user Login.
  2. After receiving the notification and call Update, ConcreteObserver (Logger và Mailer, Security ) use data from ConcreteSubject to get state to execute.

When to use it?

  1. Changes in state of an object need to be notified to a set of dependent objects, not all of them.
  2. Notification capability is required.
  3. The object sending the notification does not need to know about the receivers objects.

Resource

http://www.dotnettricks.com/learn/designpatterns/observer-design-pattern-c-sharp

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top