Advertisement
Guest User

GiovanniSeiBello+FW.py

a guest
Dec 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. class FW():
  2.     """
  3.    FW memorizza la parte comune dello stato in SharedState
  4.    
  5.    """
  6.  
  7.     def __init__(self, sharedState: list):
  8.         """op aggiunge un oggetto oggetto al file  prendendo tutta la parte condivisa dell'auto da sharedState e il resto dal
  9.        parametro itsOwnState"""
  10.         self.shared_state = sharedState
  11.    
  12.     def op(self, itsOwnState: list, tipo: type, file):
  13.         x = tipo(self.shared_state + itsOwnState)
  14.         with open(file, "a+") as file:
  15.             file.write(f"Nuova macchina con stato condiviso ({self.shared_state}) e stato unico ({itsOwnState})\n")
  16.         print(f"Il nuovo oggetto di tipo {tipo} e' {str(x.state())}")
  17.  
  18. class FWFactory():
  19.     """
  20.    Questa classe crea oggetti FW: ne crea uno nuovo se non esiste, altrimenti resituisce uno preesistente
  21.    
  22.    """
  23.     __created = []
  24.  
  25.     def __init__(self, args):
  26.         for arg in args:
  27.             fw = FW(arg)
  28.             self.__created.append(fw)
  29.  
  30.     def get_FW(self, shared_state: list) -> FW:
  31.         """
  32.        restituisce un FW con un certo stato o ne crea uno nuovo
  33.        """
  34.         for created in self.__created:
  35.             if created.shared_state == shared_state:
  36.                 print("FWFactory:  uso un FW esistente.")
  37.  
  38.                 return created
  39.  
  40.         print("FWFactory: non trovo un FW, ne creo uno nuovo.")
  41.         new_object = FW(shared_state)
  42.         self.__created.append(new_object)
  43.  
  44.         return new_object
  45.  
  46.  
  47.     def list_FWs(self):
  48.         """ stampa numero oggetti FW's e gli stati degli FW's"""
  49.         print(f"FWFactory ho {len(self.__created)} oggetti FW:")
  50.         for created in self.__created:
  51.             print("_".join(sorted(created.shared_state)))
  52.  
  53.  
  54. class automobile:
  55.     def __init__(self,state:list):
  56.         self._state=state
  57.     def state(self): return self._state
  58.        
  59. def add_car(factory: FWFactory, targa: str, proprietario: str,marca: str, modello: str, colore: str) :
  60.     print("\n\nClient: Aggiungo un automobile.")
  61.     fw = factory.get_FW([marca, modello, colore])
  62.    
  63.     fw.op([targa, proprietario],automobile,"automobili.txt")
  64.  
  65.  
  66. if __name__ == "__main__":
  67.     """
  68.    The client code usually creates a bunch of pre-populated flyweights in the
  69.    initialization stage of the application.
  70.    """
  71.  
  72.     factory = FWFactory([
  73.         ["Chevrolet", "Camaro2018", "rosa"],
  74.         ["Mercedes Benz", "C300", "nera"],
  75.         ["Mercedes Benz", "C500", "rossa"],
  76.         ["BMW", "M5", "rossa"],
  77.         ["BMW", "X6", "bianca"],
  78.     ])
  79.  
  80.     factory.list_FWs()
  81.  
  82.     add_car(
  83.         factory, "DE123AT", "Bob Bab", "BMW", "M5", "rossa")
  84.  
  85.     add_car(
  86.         factory, "AR324SD", "Mike Smith", "BMW", "X1", "rossa")
  87.  
  88.     print("\n")
  89.  
  90.     factory.list_FWs()
  91.  
  92. """Il programma stampa :
  93.  
  94. FWFactory: ho  5 oggetti FW:
  95. Camaro2018_Chevrolet_rosa
  96. C300_Mercedes Benz_nera
  97. C500_Mercedes Benz_rossa
  98. BMW_M5_rossa
  99. BMW_X6_bianca
  100.  
  101.  
  102. Client: Aggiungo un automobile.
  103. FWFactory:  uso un FW esistente.
  104. Il nuovo oggetto di tipo <class '__main__.automobile'> e` ['BMW', 'M5', 'rossa', 'DE123AT', 'Bob Bab']:
  105.  
  106.  
  107. Client: Aggiungo un automobile.
  108. FWFactory: non trovo un FW, ne creo uno nuovo.
  109. Il nuovo oggetto di tipo <class '__main__.automobile'> e` ['BMW', 'X1', 'rossa', 'AR324SD', 'Mike Smith']:
  110.  
  111.  
  112. FWFactory: ho  6 oggetti FW:
  113. Camaro2018_Chevrolet_rosa
  114. C300_Mercedes Benz_nera
  115. C500_Mercedes Benz_rossa
  116. BMW_M5_rossa
  117. BMW_X6_bianca
  118. BMW_X1_rossa
  119.  
  120. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement