All posts by chang

State Pattern

# Allowing an object to alter behavior
# when its internal state changes so that it appears to change its class

class Phone:
def __init__(self):
self.ring_state = SoundState()
def volumeUp(self):
self.ring_state = self.ring_state.nextVolumeUp()
def volumeDown(self):
self.ring_state = self.ring_state.nextVolumeDown()
def ring(self):
self.ring_state.ring()

import abc

class RingState(abc.ABC):
@abc.abstractmethod
def ring(self):
pass
@abc.abstractmethod
def nextVolumeUp(self):
pass
@abc.abstractmethod
def nextVolumeDown(self):
pass

class SoundState(RingState):
def ring(self):
print("Phone is ringing")
def nextVolumeUp(self):
return self
def nextVolumeDown(self):
return VibrateState()

class VibrateState(RingState):
def ring(self):
print("Phone is vibrating")
def nextVolumeUp(self):
return SoundState()
def nextVolumeDown(self):
return SilentState()

class SilentState(RingState):
def ring(self):
print("Phone is silent")
def nextVolumeUp(self):
return VibrateState()
def nextVolumeDown(self):
return self

phone = Phone()
phone.ring()
phone.volumeDown()
phone.ring()
phone.volumeDown()
phone.ring()
phone.volumeDown()
phone.ring()
phone.volumeUp()
phone.ring()
phone.volumeUp()
phone.ring()

Strategy Pattern

class File:
def __init__(self):
self.strategy = None

def compress(self):
self.strategy.compress()

import abc
class CompressionStrategy(abc.ABC):
@abc.abstractmethod
def compress(cls):
pass

class ZipCompression(CompressionStrategy):
def compress(cls):
print("zip Compression")

class RarCompression(CompressionStrategy):
def compress(cls):
print("rar Compression")

f = File()
f.strategy = ZipCompression()
f.compress()

f.strategy = RarCompression()
f.compress()

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)