class PizzaShop: def __init__(self): self.server=Server('Pat') self.chef=PizzaRobot('bob') self.oven=Oven() def order(self,name): customer=Customer(name) customer.order(self.server) self.chef.work() self.oven.bake() customer.pay(self.server)
class PizzaShop: def __init__(self): self.server = Server('Pat') self.chef = PizzaRobot('Bob') self.oven = Oven() # oven bakes def order(self, name): customer = Customer(name) customer.order(self.server) self.chef.work() self.oven.bake() customer.pay(self.server)
class Shop: def __init__(self): self.server = Server("Pat") self.chef = PizzaRobot("John") self.oven = Oven() def order(self, name): customer = Customer(name) customer.order(self.server) self.chef.work() self.oven.bake() customer.pay(self.server)
class PizzShop: def __init__(self): # 构造函数将导入的类实例化并将其嵌入 self.server = Server('Pat') self.chef = PizzaRobot('Bob') self.oven = Oven() def order(self, name): customer = Customer(name) customer.order(self.server) self.chef.work() self.oven.bake() customer.pay(self.server)
class PizzaShop: """a container""" def __init__(self): self.server = Server("pat") self.chef = PizzaRobot("Bob") self.oven = Oven() def order(self, name): """组合:这是新的接口order,不过它的实现依赖了其中的对象以及它们的方法""" customer = Customer(name) customer.order(self.server) self.chef.work() self.oven.bake() customer.pay(self.server)
class PizzaShop: def __init__(self): self.server = [Server("Bob")] self.chef = PizzaRobot("Spam") def order(self, name, pizza_name, pizza_size): print "order:" customer = Customer(name) count_server = len(self.server) index = randint(0, count_server - 1) if count_server > 1 else 0 customer_server = self.server[index] customer.order(customer_server, pizza_name, pizza_size) self.chef.work() customer.pay(customer_server, pizza_name, pizza_size) def addServer(self, server): if isinstance(server, Server): self.server.append(server) else: raise ValueError("the argument need a instance of Server")
class PizzaShop: def __init__(self): self.server = [Server('Bob')] self.chef = PizzaRobot('Spam') def order(self, name, pizza_name, pizza_size): print "order:" customer = Customer(name) count_server = len(self.server) index = randint(0, count_server - 1) if count_server > 1 else 0 customer_server = self.server[index] customer.order(customer_server, pizza_name, pizza_size) self.chef.work() customer.pay(customer_server, pizza_name, pizza_size) def addServer(self, server): if isinstance(server, Server): self.server.append(server) else: raise ValueError('the argument need a instance of Server')
from employee import Employee, PizzaRobot, Chef, Server from shop import Shop if __name__ == "__main__": bob = PizzaRobot("bob") bob.work() print(bob) bob.give_raise(0.1) print(bob) for c in Employee, Chef, Server, PizzaRobot: obj = c(c.__name__) obj.work() print("======================") scene = Shop() scene.order("Anne") print("...") scene.order("Ellie")