Advertisement
agel122

add_haters/hated(factory_pattern)

Sep 1st, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2.  
  3. # abstract creator
  4. class CreateData(ABC):
  5.     @abstractmethod
  6.     def data(self):
  7.         pass
  8.  
  9.     def added_data(self):
  10.         result = self.data()
  11.         return result.add_items()
  12.  
  13.  
  14. # concrete creator (Haters)
  15. class CreateHatersManual(CreateData):
  16.     def data(self):
  17.         return ManualHaters()
  18.  
  19.  
  20. # concrete creator (Hated)
  21. class CreateHatedManual(CreateData):
  22.     def data(self):
  23.         return ManualHated()
  24.  
  25.  
  26. # abstract product
  27. class Data(ABC):
  28.     @abstractmethod
  29.     def add_items(self):
  30.         pass
  31.  
  32. # concrete product (Haters)
  33. class ManualHaters(Data):
  34.     def __init__(self):
  35.         print(f"let's input names of haters and after - persons, hated by haters!")
  36.  
  37.     def add_hated(self):
  38.         print('please, input names. if you have enough, press q\n')
  39.         inputdata = []
  40.         while True:
  41.             b = input('one of the names\n')
  42.             if b == 'q':
  43.                 break
  44.             else:
  45.                 inputdata.append(b)
  46.         return inputdata
  47.  
  48.     def add_items(self):
  49.         inputdata = dict.fromkeys(self.add_hated(), {})
  50.         for name in inputdata.keys():
  51.             print(f"items, covered by {name}")
  52.             inputdata[name] = set(self.add_hated())
  53.         return inputdata
  54.  
  55.  
  56. # concrete product (Hated)
  57. class ManualHated(Data):
  58.     def __init__(self):
  59.         print(f"let's input names of hated persons!")
  60.  
  61.     def add_items(self):
  62.         print('please, input names. if you have enough, press q\n')
  63.         inputdata = []
  64.         while True:
  65.             b = input('one of the names\n')
  66.             if b == 'q':
  67.                 break
  68.             else:
  69.                 inputdata.append(b)
  70.         return set(inputdata)
  71.  
  72.  
  73. def get_data(creator):
  74.     result = creator.added_data()
  75.     return result
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     hated = get_data(CreateHatedManual())
  80.     haters = get_data(CreateHatersManual())
  81.     print(haters)
  82.     print(hated)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement