Observer Pattern

import abc
class Observer(abc.ABC):
@abc.abstractmethod
def update(self, lon, lat):
pass

class MobileApp(Observer):
def update(self, lon=0.0, lat=0.0):
print(f"{lon} : {lat}")

class MobilePhone:
lon, lat = 0.0, 0.0
observers = []

def move(cls, lon, lat):
if cls.lon == lon and cls.lat == lat:
return
cls.lon, cls.lat = lon, lat
cls.notify()

def register(cls, o):
cls.observers.append(o)

def notify(cls):
for observer in cls.observers:
observer.update(cls.lon, cls.lat)

phone = MobilePhone()

phone.register(MobileApp())
phone.move(1.0, 1.2)
phone.move(1.1, 21.2)
phone.move(1.2, 93)
phone.move(333.0, 144.2)
class Sub:
def __init__(self, name):
self.name = name
def update(self, message):
print(f"Notification for {self.name} : {message}")

class Pub:
def __init__(self):
self.subs = set()

def register(self,sub):
self.subs.add(sub)

def unregister(self, sub):
self.subs.discard(sub)

def notify(self, message):
for sub in self.subs:
sub.update(message)

sub_list = [Sub("changpil@gmail.com"), Sub("clee@suse.com"),Sub("chalee@microsoft.com")]
pub = Pub()
for sub in sub_list:
pub.register(sub)

pub.notify("greeting")
import abc
class Observer(abc.ABC):
@abc.abstractmethod
def update(self, floor):
pass


class Button:
def __init__(self, name):
self.name = name
self.on = None
self.observers = []

def request(self):
if not self.on:
self.notify()
self.on = True
print("Button activated")

def off(self):
self.on = False
print("Button deactivated")

def register(self, observer):
self.observers.append(observer)

def notify(self):
for observer in self.observers:
observer.update(self.name)


class Door():
def open(self):
self.status = "door opened"

def close(self):
self.status = "door close"




class Floor(Observer):
def __init__(self, floor):
self.floor = floor
self.door = Door()
self.upButton = Button("up")
self.downButton = Button("down")
self.upButton.register(self)
self.downButton.register(self)
self.uprequest = False
self.downrequest = False

def update(self, name):
if name == "up":
self.uprequest = True
elif name == "down":
self.downrequest = True

def requestUp(self):
self.upButton.request()

def requestDown(self):
self.downButton.request()

def opendoor(self):
self.door.open()

def closedoor(self):
self.door.close()

groundfloor = Floor(0)
groundfloor.requestUp()
print(groundfloor.uprequest)