Monday 10 May 2021

Singleton pattern and factory pattern of common Python design patterns

 

1. Singleton mode

Singleton pattern (Singleton Pattern) is a commonly used software design pattern, the main purpose of this pattern is to ensure that only one instance of a class exists. When you want to have only one instance of a class in the entire system, singleton objects can come in handy.
For example, the configuration information of a server program is stored in a file, and the client reads the configuration file information through an AppConfig class. If the content of the configuration file needs to be used in many places during the running of the program, that is to say, many instances of AppConfig objects need to be created, which results in the existence of multiple AppConfig instance objects in the system, which will seriously waste memory Resources, especially if the configuration file has a lot of content. In fact, for a class like AppConfig, we want only one instance object to exist during the running of the program. Note: Importing modules in python is a singleton mode.

Exercise: Create a Sun class

class Sun(object):
    #Define a variable
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            #If __instance has no value, assign a value to the __instance variable
            cls.__instance = object.__new__(cls)
            return cls.__instance
        else:
            #If __instance has a value, return directly.
            return cls.__instance

sun = Sun()
print(id(sun))#2185130970304
sun1=Sun()
print(id(sun1))#2185130970304
#sun and sun1 have the same id value
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Second, the factory model

Factory pattern is a design pattern used to create objects in software development.
When a "type" is input when the program is run, the corresponding object needs to be created here. This uses the factory model. In such a situation, the implementation code is based on the factory pattern, which can achieve scalable and maintainable code. When adding a new type, it is no longer necessary to modify the existing class, only adding subclasses that can generate a new type.
Use factory mode application scenario: You can use factory mode when you don’t know what kind of object the user wants to create.
example:

class Car(object):

    def run(self):
        print('Running rubbing')

    def stop(self):
        print('Wooing parking')

class BMW(Car):

    def run(self):
        print('BMW--->>Running)

    def stop(self):
        print('BMW---Wooing parking')

class Benz(Car):
    def run(self):
        print('Mercedes--->>Running)

    def stop(self):
        print('Mercedes--->>Wooing parking')

class Skoda(Car):
    def run(self):
        print('Skoda--->>Running)

    def stop(self):
        print('Skoda --->>Wooing parking')

class CarFactory(object):

    def new_car(self,name):

        if name == 'BMW':
            bmw = BMW()
            return bmw

        if name == 'Benz':
            benz = Benz()
            return benz

        if name == 'skd':
            return Skoda()

class CarStore(object):
    def __init__(self,factory):
        self.factory=factory

    def order(self,name):
        new_car=self.factory.new_car(name)
        return new_car
        
car_factory=CarFactory()
car_store=CarStore(car_factory)
car= car_store.order('skd')
car.run()
car.stop()


from: https://www.programmersought.com/article/66934024946/