# Define the Mercedes-Benz class class BenchiCar(object): # How to define the car def move(self): print('---The Mercedes-Benz is moving---') def stop(self): print('---The Mercedes-Benz stopped---') # define BMW class class BMWCar(object): # How to define the car def move(self): print('---BMW is moving---') def stop(self): print('---BMW stopped---') # Define a function to simulate a car factory, the purpose is to create a specific car object def creatCar(typeName): if typeName == 'Benz': car = BenchiCar() # find a car elif typeName == 'BMW': car = BMWCar() # find a car return car # Define a store class that sells Beijing Hyundai cars class CarStore(object): def order(self, typeName): # Let the factory produce a car based on the type car = creatCar(typeName) return car pinpai_store = CarStore() my_car = pinpai_store.order('BMW') my_car.move() my_car.stop()