標籤:ssm 如何 turn cdc new 互動 style nbsp 網路編程
單例模式:多次執行個體化的結果指向同一個執行個體
單例實現方式1
import settingsclass MySQL: __instance=None def __init__(self, ip, port): self.ip = ip self.port = port @classmethod def from_conf(cls): if cls.__instance is None: cls.__instance=cls(settings.IP, settings.PORT) return cls.__instanceobj1=MySQL.from_conf()obj2=MySQL.from_conf()obj3=MySQL.from_conf()# obj4=MySQL(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)# print(obj4)
View Code
單例實現方式2(裝飾器)
import settingsdef singleton(cls): _instance=cls(settings.IP,settings.PORT) def wrapper(*args,**kwargs): if len(args) !=0 or len(kwargs) !=0: obj=cls(*args,**kwargs) return obj return _instance return wrapper@singleton #MySQL=singleton(MySQL) #MySQL=wrapperclass MySQL: def __init__(self, ip, port): self.ip = ip self.port = port# obj=MySQL(‘1.1.1.1‘,3306) #obj=wrapper(‘1.1.1.1‘,3306)# print(obj.__dict__)obj1=MySQL() #wrapper()obj2=MySQL() #wrapper()obj3=MySQL() #wrapper()obj4=MySQL(‘1.1.1.3‘,3302) #wrapper(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)print(obj4)
View Code
單例實現方式3(元類)
import settingsclass Mymeta(type): def __init__(self,class_name,class_bases,class_dic): #self=MySQL這個類 self.__instance=self(settings.IP,settings.PORT) def __call__(self, *args, **kwargs): # self=MySQL這個類 if len(args) != 0 or len(kwargs) != 0: obj=self.__new__(self) self.__init__(obj,*args, **kwargs) return obj else: return self.__instanceclass MySQL(metaclass=Mymeta): #MySQL=Mymeta(...) def __init__(self, ip, port): self.ip = ip self.port = portobj1=MySQL()obj2=MySQL()obj3=MySQL()obj4=MySQL(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)print(obj4)
View Code
單例實現方式4(模組匯入)
def f1(): from singleton import instance print(instance)def f2(): from singleton import instance,My SQL print(instance) obj=MySQL(‘1.1.1.3‘,3302) print(obj)f1()f2()
View Code
import settingclass MySQL: print((‘run.....‘)) def __init__(self,ip,port): self.ip=ip self.port=portinstance=MySQL(setting.IP,setting.PORT)
View Code
網路編程
1. 目標:編寫一個C/S架構的軟體
C/S: Client--------基於網路----------Server
B/S: Browser-------基於網路----------Server
2. 服務端需要遵循的原則:
1. 服務端與用戶端都需要有唯一的地址,但是服務端的地址必須固定/綁定
2. 對外一直提供服務,穩定運行
3. 服務端應該支援並發
3. 網路
網路建立的目的是為資料互動(通訊)
如何?通訊:
1. 建立好底層的物理串連介質
2. 有一套統一的通訊標準,稱之為互連網協議
4. 互連網協議:就是電腦界的英語
OSI七層協議
應用程式層
展示層
會話層
傳輸層
網路層
資料連結層
物理層
ip+mac可以標識全世界範圍內獨一無二的一台電腦的位置
port可以標識一台電腦之上唯一的一個基於網路通訊的應用軟體
ip+mac+port:可以標識全世界範圍內獨一無二的一個應用軟體(基於網路通訊)
python學習----8.28---單例模式,網路編程