Observer Design Pattern :)

It is a kind of design pattern which defines one to many relationships between objects and when state of the one object changes, all the dependent objects will be notified and updated automatically.

This is also used to handle distributed event handling system, allowing multiple objects to react to changes to another object without tightly coupling them together.

class observer:
    def update(self,message):
        pass


class Subject:
    def __init__(self):
        self._observer = []

    def attach(self,observer):
        self._observer.append(observer)

    def dettach(self,observer):
        self._observer.remove(observer)

    def notify(self,message):
        for observer in self._observer:
            observer.update(message)

    
class ConcreteObserver(observer):
    def __init__(self,name):
        self._name = name

    def update(self,message):
        print(f"{self._name} recieved message {message}")


class ConcreteSubject(Subject):
    def __init__(self,state):
        super().__init__()
        self._state = state

    def get_state(self):
        return self._state
    
    def set_state(self,state):
        self._state = state
        self.notify(f"changed state to {self._state}")


sub = ConcreteSubject("initial state")

obs1 = ConcreteObserver("observer1")
obs2 = ConcreteObserver("observer2")

sub.attach(obs1)
sub.attach(obs2)

sub.set_state("new state")        

In the above code snippets:

The observer class defines an interface for observers with an update method. the subject class represents the subject being observed. it maintains list of observers and provides method to attach, detach and notify them. The concreteobserver class implements observer interface and provides update method to react changes. The concretesubject class extends subject class and contains the state of the observers are interested in. It also notifies observers when the state changes.

When you run the code, you will see both the observers are notified and updated when the subject's state changes.

#oops #designPatterns #ObserverDP

要查看或添加评论,请登录

swetansu mohanty的更多文章

  • Facade Design Pattern

    Facade Design Pattern

    Facade Design Pattern is a kind of structural Design Pattern which provides simplified interface for complex class…

  • Adapter Design Pattern

    Adapter Design Pattern

    Adapter Design Pattern is a structural design pattern that allow two incompatible interfaces work together. It acts as…

  • Decorator Design Pattern

    Decorator Design Pattern

    Decorator Design Pattern is a structural design pattern used to add behavior to the objects dynamically. It often used…

  • Relationships between Objects

    Relationships between Objects

    The term "relation between objects" refers to how different objects interact or are connected with each other within a…

  • Factory Design Patterns

    Factory Design Patterns

    The Factory Design Pattern is a kind of pattern that provides interface for creating objects in a super class and allow…

  • Singleton Design Patterns

    Singleton Design Patterns

    Singleton design patterns ensures that a class have only one instance and it provides global point of access to that…

  • why should we use DESIGN PATTERNS in programming language?

    why should we use DESIGN PATTERNS in programming language?

    Using Design Patterns offers several benefits such as contribute to better code organization, maintainability and…

  • Exploring Class in Python

    Exploring Class in Python

    Hii #linkedin connections :) I am just exploring python and found something interesting. In python there are different…

社区洞察

其他会员也浏览了