Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FW():
- """
- FW memorizza la parte comune dello stato in SharedState
- """
- def __init__(self, sharedState: list):
- """op aggiunge un oggetto oggetto al file prendendo tutta la parte condivisa dell'auto da sharedState e il resto dal
- parametro itsOwnState"""
- self.shared_state = sharedState
- def op(self, itsOwnState: list, tipo: type, file):
- x = tipo(self.shared_state + itsOwnState)
- with open(file, "a+") as file:
- file.write(f"Nuova macchina con stato condiviso ({self.shared_state}) e stato unico ({itsOwnState})\n")
- print(f"Il nuovo oggetto di tipo {tipo} e' {str(x.state())}")
- class FWFactory():
- """
- Questa classe crea oggetti FW: ne crea uno nuovo se non esiste, altrimenti resituisce uno preesistente
- """
- __created = []
- def __init__(self, args):
- for arg in args:
- fw = FW(arg)
- self.__created.append(fw)
- def get_FW(self, shared_state: list) -> FW:
- """
- restituisce un FW con un certo stato o ne crea uno nuovo
- """
- for created in self.__created:
- if created.shared_state == shared_state:
- print("FWFactory: uso un FW esistente.")
- return created
- print("FWFactory: non trovo un FW, ne creo uno nuovo.")
- new_object = FW(shared_state)
- self.__created.append(new_object)
- return new_object
- def list_FWs(self):
- """ stampa numero oggetti FW's e gli stati degli FW's"""
- print(f"FWFactory ho {len(self.__created)} oggetti FW:")
- for created in self.__created:
- print("_".join(sorted(created.shared_state)))
- class automobile:
- def __init__(self,state:list):
- self._state=state
- def state(self): return self._state
- def add_car(factory: FWFactory, targa: str, proprietario: str,marca: str, modello: str, colore: str) :
- print("\n\nClient: Aggiungo un automobile.")
- fw = factory.get_FW([marca, modello, colore])
- fw.op([targa, proprietario],automobile,"automobili.txt")
- if __name__ == "__main__":
- """
- The client code usually creates a bunch of pre-populated flyweights in the
- initialization stage of the application.
- """
- factory = FWFactory([
- ["Chevrolet", "Camaro2018", "rosa"],
- ["Mercedes Benz", "C300", "nera"],
- ["Mercedes Benz", "C500", "rossa"],
- ["BMW", "M5", "rossa"],
- ["BMW", "X6", "bianca"],
- ])
- factory.list_FWs()
- add_car(
- factory, "DE123AT", "Bob Bab", "BMW", "M5", "rossa")
- add_car(
- factory, "AR324SD", "Mike Smith", "BMW", "X1", "rossa")
- print("\n")
- factory.list_FWs()
- """Il programma stampa :
- FWFactory: ho 5 oggetti FW:
- Camaro2018_Chevrolet_rosa
- C300_Mercedes Benz_nera
- C500_Mercedes Benz_rossa
- BMW_M5_rossa
- BMW_X6_bianca
- Client: Aggiungo un automobile.
- FWFactory: uso un FW esistente.
- Il nuovo oggetto di tipo <class '__main__.automobile'> e` ['BMW', 'M5', 'rossa', 'DE123AT', 'Bob Bab']:
- Client: Aggiungo un automobile.
- FWFactory: non trovo un FW, ne creo uno nuovo.
- Il nuovo oggetto di tipo <class '__main__.automobile'> e` ['BMW', 'X1', 'rossa', 'AR324SD', 'Mike Smith']:
- FWFactory: ho 6 oggetti FW:
- Camaro2018_Chevrolet_rosa
- C300_Mercedes Benz_nera
- C500_Mercedes Benz_rossa
- BMW_M5_rossa
- BMW_X6_bianca
- BMW_X1_rossa
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement