SHARE
TWEET

Untitled

a guest Oct 10th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # this is template pattern
  2. class CarModel:
  3.     def __init__(self, sequence):
  4.         self.seq = sequence
  5.     def start(self):
  6.         pass
  7.     def alarm(self):
  8.         pass
  9.     def engineBoom(self):
  10.         pass
  11.     def run(self):
  12.         for i in self.seq:
  13.             i = i.lower()
  14.             if i == "start":
  15.                 self.start()
  16.             elif i == "stop":
  17.                 self.stop()
  18.             elif i == "alarm":
  19.                 self.alarm()
  20.             elif i == "engine boom":
  21.                 self.engineBoom()
  22.  
  23. class BenzModel(CarModel):
  24.     def start(self):
  25.         print("benz start...")
  26.     def alarm(self):
  27.         print("benz alarm...")
  28.     def engineBoom(self):
  29.         print("benz engine boom...")
  30.     def stop(self):
  31.         print("benz stop...")
  32.  
  33. class BmwModel(CarModel):
  34.     def start(self):
  35.         print("bmw start...")
  36.     def alarm(self):
  37.         print("bmw alarm...")
  38.     def engineBoom(self):
  39.         print("bmw engine boom...")
  40.     def stop(self):
  41.         print("bmw stop...")
  42.  
  43. # Builder is to builder a class that has specific construct order.
  44. class CarBuilder:
  45.     def setSequence(self, seq):
  46.         pass
  47.     def getCarModel(self):
  48.         pass
  49.  
  50. class BenzBuilder(CarBuilder):
  51.     def __init__(self):
  52.         self.benz = None
  53.     def getCarModel(self):
  54.         return self.benz
  55.     def setSequence(self, seq):
  56.         self.benz = BenzModel(seq)
  57.  
  58. class BmwBuilder(CarBuilder):
  59.     def __init__(self):
  60.         self.bmw = None
  61.     def getCarModel(self):
  62.         return self.bmw
  63.     def setSequence(self, seq):
  64.         self.bmw = BmwModel(seq)
  65.  
  66. class Director:
  67.     def __init__(self):
  68.         self.benz, self.bmw = BenzBuilder(), BmwBuilder()
  69.     def getBenzModel(self):
  70.         seq = ["start", "stop"]
  71.         self.benz.setSequence(seq)
  72.         return self.benz.getCarModel()
  73.     def getBmwModel(self):
  74.         seq = ["alarm", "start", "stop"]
  75.         self.bmw.setSequence(seq)
  76.         return self.bmw.getCarModel()
  77.  
  78. def main():
  79.     director = Director()
  80.     for i in range(10):
  81.         director.getBenzModel().run()
  82.     for i in range(2):
  83.         director.getBmwModel().run()
  84.  
  85. if __name__ == "__main__":
  86.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top