# 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 class to produce different car objects according to specific orders class CarFactory(object): def creatCar(self, 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 __init__(self): # Set the specified car factory self.carFactory = CarFactory() def order(self, typeName): # Let the factory produce a car based on the type car = self.carFactory.creatCar(typeName) return car pinpai_store = CarStore() my_car = pinpai_store.order('Benz') my_car.move() my_car.stop() # This solution is called simple factory pattern # Factory functions and factory classes encapsulate the specific generation process, which is conducive to the subsequent expansion of the code # That is, the function is divided into more specific, 4s stores are only responsible for sales, and car factories are only responsible for manufacturing