Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PyFoodBevOCP
#python 3.6.9 #PyFoodBevOCP: example of SOLID principle where Open-Closed principle is highlighted; two classes Food and Beverage are created; class Detail is abstract class where abstract method fdet() prints details about the instances of Food and Beverage classes;depending on the type of food and beverage corresponding subclasses of Detail is called and respective fdet(Food/Beverage) method is invoked; note that fdet() takes as argument Food or Beverage class to print the details; here subclasses of Details are extended to invoke the appropriate fdet() to fit the instances instead of modifying Details; #this code is created by Rezaul Hoque on April 13,2022; #contact: jewelmrh@yahoo.com; Dhaka, Bangladesh,https://rezaulhoque.wordpress.com;https:hoquestake.blogspot.com #note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; from abc import ABC, abstractmethod class Food: def __init__(self, food,type,price,qty): self.food= food self.type=type self.price=price self.qty=qty def __repr__(self): return f'Food(name={self.food},type={self.type},price={self.price}, qty={self.qty})' class Beverage: def __init__(self, beverage,type,price,qty): self.beverage= beverage self.type=type self.price=price self.qty=qty def __repr__(self): return f'Beverage(name={self.beverage},type={self.type},price={self.price}, qty={self.qty})' class Details(ABC): @abstractmethod def fdet(self): pass class BakeFood(Details): def fdet(self,Food): print(f'{Food}') class FryFood(Details): def fdet(self, Food): print(f'{Food}') class Drinks(Details): def fdet(self, Beverage): print(f'{Beverage}') class TraDrinks(Details): def fdet(self, Beverage): print(f' {Beverage}') if __name__ == '__main__': a = Food('Fried Hilsa','Fried Food',80,1) b=Food('Naan','Baked Food',12,1) c = Beverage('Coke','Carbonated Drink',20,1) d=Beverage('Lassi','Traditional Drink',40,1) f1=FryFood() f1.fdet(a) f2=BakeFood() f3=Drinks() f4=TraDrinks() f2.fdet(b) f3.fdet(c) f4.fdet(d)
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Multinomial Naive Bayes
Game2
Restaurant 1
Ok
Calc1
abc1
function and doc string
Contact Class 2.0
PyRegLookBehind
PyGuessTheNum
Please log in to post a comment.