Bus is one of the common communication solutions for transmitting data and controlling signals between various functional parts or devices of a computer. Now assume that the following scenario: a central processing unit (CPU) through a protocol bus and a light signal connected to a signal lamp has 64 colors can be set, the central processing unit running three threads, you can control the signal, and can independently set the color of the signal. Abstract the details of the protocol (expressed in print), how to implement the thread of the signal control logic.
The addition of the line lock is undoubtedly the first thought of the method, but the control of the lock on each thread, undoubtedly increased the coupling between the modules. Below, we will use the singleton pattern in design mode to solve this problem.
What is a singleton mode? A singleton pattern is a guarantee that a class has only one instance and provides a global access point to access it. Specifically, in this case, the bus object, which is a singleton, has only one instance, and each thread accesses the bus with only one global access point, that is, the only instance.
The Python code is as follows:
#encoding =utf8import Threadingimport time# use method new to implement the Singleton mode class Singleton (object): #抽象单例 def new (CLS, *args, **kw): If not hasattr (CLS, ' _instance '): Orig = Super (Singleton, C ls) cls._instance = orig.new (CLS, *args, **kw) return cls._instance# Bus class bus (Singleton): lock = th Reading. Rlock () def senddata (self,data): Self.lock.acquire () time.sleep (3) print "Sending Signal data ...", Data self.lock.release () #线程对象, in order to explain the meaning of the singleton, the bus object is instantiated in the Run class Visitentity (threading. Thread): my_bus= "" Name= "" Def GetName (self): return self.name def setName (self, name): Self.nam E=name def run (self): Self.my_bus=bus () self.my_bus.sendData (self.name) If name== ' main ': For I in rang E (3): print "Entity%d begin to run ..."%i my_entity=visitentity () my_entity.setname ("Entity_" +str (i)) My_entity.start ()
The results of the operation are as follows:
Entity 0 begin to run ...
Entity 1 begin to run ...
Entity 2 begin to run ...
Sending Signal Data ... Entity_0
Sending Signal Data ... Entity_1
Sending Signal Data ... Entity_2
While the program is running, three threads run at the same time (the first three rows of the results are printed quickly), and then the bus resources are used (the last three lines print one line every 3 seconds). Although it appears that bus buses have been instantiated three times, there is actually only one instance in memory.
Single-Case mode
The singleton pattern is a relatively simple category in all design patterns and is defined as follows: Ensure a class has only one instance, and provide a global point of access to it. (Guaranteed that one And there is only one access point globally)
Advantages and applications of the singleton model
Advantages of Singleton mode:
1, because the single-case mode requires only one instance in the global, so it can save more memory space;
2, the global only one access point, can better synchronize data control, to avoid multiple occupancy;
3, single case can be long-standing memory, reduce system overhead.
Examples of application of the singleton pattern:
1, generate the global unique serial number;
2, access to the global reuse of the only resources, such as disk, bus, etc.;
3, a single object occupies too much resources, such as databases;
4, the system globally unified management, such as the Task Manager under Windows;
5, the website counter.
Disadvantages of a singleton pattern
1, the expansion of the singleton mode is more difficult;
2, which is assigned to the single-instance etheric duty, to some extent contrary to the principle of single responsibility (the six principles will be discussed later);
3, the singleton mode is the concurrent Collaboration software module needs to be completed first, so it is not conducive to testing;
4, the singleton mode in some cases causes a "resource bottleneck."