# Command Pattern is good for batch processes with behavioral class
class Document:
text = ""
def __str__(self):
return self.text
import abc
class DocumentCommand(abc.ABC):
doc = None
@abc.abstractmethod
def execute(self):
pass
@abc.abstractmethod
def undo(self):
pass
class AddStrCommand(DocumentCommand):
def __init__(self, doc, txt):
self.text = txt
self.doc = doc
def execute(self):
self.doc.text += self.text
return self.doc
def undo(self):
return DeleteStrCommand(self.doc, self.text).execute()
class DeleteStrCommand(DocumentCommand):
def __init__(self, doc, txt):
self.text = txt
self.doc = doc
def execute(self):
idx = self.doc.text.find(self.text)
if idx == -1:
return self.doc
self.doc.text = self.doc.text[:idx]
return self.doc
def undo(self):
return AddStrCommand(self.doc, self.text).execute()
doc = Document()
editingTexts = [AddStrCommand(doc,"hello\n"), AddStrCommand(doc,"My name is Chang\n"), AddStrCommand(doc,"I hope you have a good day")]
for c in editingTexts:
c.execute()
print(doc)
print(editingTexts[-1].undo())
print(editingTexts[-1].undo())
d = DeleteStrCommand(doc,"My name is Chang\n")
print(d.execute())
print(d.undo())
print(doc)
import abc
class Order(abc.ABC):
@abc.abstractmethod
def execute(self):
pass
class Stock:
def __init__(self, name):
self.name = name
def buy(self):
print(f"Stock {self.name} bought", end=" ")
def sell(self):
print(f"Stock {self.name} sold", end=" ")
class BuyStock(Order):
def __init__(self, stock, quantity):
self.stock = stock
self.quantity = quantity
def execute(self):
self.stock.buy()
print(f"{self.quantity}")
class SellStock(Order):
def __init__(self, stock, quantity):
self.stock = stock
self.quantity = quantity
def execute(self):
self.stock.sell()
print(f"{self.quantity}")
class Broker:
def __init__(self, name):
self.name = name
self.orderList = []
def addOrder(self, order):
self.orderList.append(order)
def placeOrders(self):
for o in self.orderList:
o.execute()
broker = Broker("Fidelity")
amazonStock = Stock("Amazon")
buy1 = BuyStock(amazonStock, 10)
sell1 =SellStock(amazonStock, 5)
broker.addOrder(buy1)
broker.addOrder(sell1)
pltrStock = Stock("PLTR")
buy2 = BuyStock(pltrStock, 100)
sell2 =SellStock(pltrStock, 9)
broker.addOrder(buy2)
broker.addOrder(sell2)
broker.placeOrders()
class Pet:
def __init__(self, kind, name):
self.kind = kind
self.name = name
class FeedPet:
def __init__(self, pet, food):
self.pet = pet
self.food = food
def execute(self):
print(f"{self.pet.kind}, {self.pet.name} eats {self.food}")
class WalkPet:
def __init__(self, pet, place):
self.pet = pet
self.place = place
def execute(self):
print(f"{self.pet.name} eats {self.place}")
class PlayPet:
def __init__(self, pet, min):
self.pet = pet
self.min = min
def execute(self):
print(f"{self.pet.name} plays for {self.min}")
class AnimalFarm:
def __init__(self, name):
self.name = name
self.todoList = []
def addTodo(self, deed):
self.todoList.append(deed)
def doTodoList(self):
for do in self.todoList:
do.execute()
farm = AnimalFarm("Seattle Animal Farm")
horse = Pet("Horse", "Thunder")
cow = Pet("Cow", "Moo")
cat = Pet("cat", "Muze")
dog = Pet("dog", "Win")
farm.addTodo(FeedPet(horse, "carrot"))
farm.addTodo(PlayPet(horse, "30"))
farm.addTodo(WalkPet(dog, "dog park"))
farm.addTodo(FeedPet(cat, "salmon"))
farm.addTodo(FeedPet(cow, "hay"))
farm.doTodoList()