cmdb project, Singleton,

1.cmdb assets after the acquisition Why not just put inside the database? (Api functions as an interface)

I would like to ask the question more when it functions as an interface api 

First of all, the role of my program inside the api interface to do record storage and asset changes of assets acquired 
Why do all that by api? 
The first point: 
    Because I want to do after the operation and maintenance of automation, so unavoidable there will be many systems with asset information, come back here for me, although I can give him the account number and password for the database, but very insecure, I just need it is an interface to write on it, and then return the data to other systems, 
the second point: 
    when we acquired asset, if not use api, then you have to keep the account and password databases across all servers above, will be insecurity.

 

2. singleton

Literally means: only one instance, and only the strength of one. ( To ensure that only one instance of a class, and it provides access to a global access point. )
 
This type of design pattern belongs created schema, which provides the best way to create the object. 
This model involves a single class that is responsible for creating your own objects, while ensuring that only a single object is created. This class provides the only way to access the object can directly access, no instance of the object class. 

Note:
  1. singleton class intelligent one instance
  2. singleton class must create their own unique instance
  3. singleton class must provide this example to all other objects
mainly to solve:
  a global frequent use of the class to create and destroy
how use:
  when you want to control the number of instances, to save system resources
on how to solve:
  to determine whether or not already have deemed this single case, if there is returned, no it is created (constructor must be private)
application scenarios:
  
1, a class only one teacher.
  2, Windows is a multi-process multi-threaded, when a file operation, it inevitably multiple processes or threads simultaneously operating a file of the phenomenon, the processing of all files must be done by a unique instance.
  3, some devices are often designed as a single Manager embodiment mode, such as a computer printer has two, when both the output will not process the same document printer.
Pros:
  The only instance in memory 1. only one class, reducing memory overhead, especially frequent create and destroy instances
  2. Avoid multiple occupancy (such as write file operation) resource
disadvantages:
  1. There is no interface can not inherit
  2. conflict with the single responsibility principle
  3. It should be a relationship between the internal logic class, rather than how to come out of the relationship between the strength of the
Notes:
  getInstance () method requires the use of synchronization lock synchronized (Singleton.class) prevent multiple threads into the cause instance is instantiated multiple times.

 

 

Multi-pattern

class Foo (Object):
     DEF  the __init__ (Self, name, Age): 
        the self.name = name 
        self.age = Age 

    DEF FUNC (Self): 
        MSG = " % S-% S " % (the self.name, self.age )
         Print (MSG)
         Print (Self)   # print out two different instances of the memory address 

OBJ1 = foo ( " Kobe " ,. 19) # an object foo / example 
obj1.func () 

OBJ1 = foo ( " ADMIN " ,. 19 ) # an object foo / example
obj1.func ()

 

Error singleton

class Foo(object):
    instance = None
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __new__(cls, *args, **kwargs):
        if not cls.instance:
            cls.instance = object.__new__(cls)
        return cls.instance

    def func(self):
        msg = "%s-%s" % (self.name, self.age)
        print(self)  #Examples of printing two memory addresses 
        return MSG 


OBJ1 = Foo ( " Kobe " ,. 19) # an object foo / Examples 
RES1 = obj1.func ()
 Print (RES1) 

obj2 = Foo ( " ADMIN " , 18 is) # foo an object / instance 
RES2 = obj2.func ()
 Print (RES2) 

# Note that when using single-mode embodiment, generally do not write __init__, as an example of the data before __init__ will be overwritten.

 

Based __new__ correct singleton

#-Based multithreaded singleton wrong version
 Import Time
 class Singleton (Object): 
    instance = None
     DEF  __init__ (Self): 
        self.name = None
     DEF  __new__ (CLS, * args, ** kwargs):
         IF  not cls.instance : 
            the time.sleep ( . 1 ) 
            cls.instance = Object. __new__ is (CLS)
         return cls.instance 

DEF Task (): 
    obj = Singleton ()
     Print (obj) 

fori in the Range (10 ): 
    t = threading.Thread (target = Task) 
    t.start () 

# -based multithreading singleton pattern does not hold 
# because all of the threads to execute __new__ way to go, and in the case of to time.sleepde, will live ram 
# it will be carried out after the strength of the ram continued to live, but 10 threads have all passed if not cls.instance: judge sentence 
# so will the strength of the new instance. Therefore, the instantiation singletons multithreading lock to add 

# singleton pattern based multithreaded correct version

 Import Time
 Import Threading 

class Singleton (Object): 
    instance = None 
    Lock = threading.RLock ()
     DEF  __new__ is (CLS, args *, ** kwargs): 

        IFcls.instance:   # When the last is to be another example of the time, does not need to operate the lock, and save resources 
            return cls.instance 

        with cls.lock:   # automatic locking and unlocking 
            IF  not cls.instance: 
                the time.sleep ( . 1 ) 
                cls.instance = Object. __new__ is (CLS)
             return cls.instance 

DEF Task (): 
    obj = Singleton ()
     Print (obj) 

for I in Range (10 ): 
    T = of the threading.Thread (target = Task) 
    T. start ()

 

 

Example files into single-mode (based on the application in the source code )

# xx.py
class Singleton(object):

    def __init__(self):
        self._registry = []

    def register(self,model_class):
        self._registry.append(model_class)
site = Singleton()

# xx2.py
import xx
xx.site

 

 

 

Singleton application scenario
  1.django profile, as long as a load, a later use the same values are used.

class Singleton(object):
    instance = None

    def __init__(self):
        self.k0 = 0
        self.k1 = 1
        self.k2 = 2
        ...

    def __new__(cls, *arg, **kawrgs):
        if not cls.instance:
            cls.instance = object.__new__(cls)
        return cls.instance
    
obj1 = Singleton()
obj2 = Singleton()
obj3 = Singleton()

 

   2.django of admin, registered in models with the hope that all the model class is registered to the same list.

 pass

Guess you like

Origin www.cnblogs.com/p0st/p/11859251.html