The examples in this article describe Python's common design patterns. Share to everyone for your reference, as follows:
# #!/usr/bin/env python# #-*-coding:utf-8## class httpbase:# def get (self): # psss# class HTTP1 (Httpbase): # def G ET (self): # print ' HTTP1 ' # class HTTP2 (Httpbase): # def get (self): # print ' HTTP2 ' # # # class base:# def __init__ (s ELF): # self.httpobj = none# def http (self): # Self.httpobj.get () # def COMPUTE (self): # self.http () # Self . Show () # #虚函数 # def show (self): # pass# def notify (self, k): # print ' Notify ', k### #桥接模式, associating different HTTP1 and HTTP via a, b * Class Basea (Base): # def __init__ (self): # self.httpobj = HTTP1 () # def notify (self, k): # print ' A notify ', k# Def show (self): # print ' Show a ' # # class Baseb (Base): # def __init__ (self): # self.httpobj = HTTP2 () # def Noti FY (self, k): # print ' B notify ', k# def Show (self): # print ' Show B ' # # #观测者模式 # class observer:# def __init__ (sel f): # Self.listob = []# def register (self, obj): # self.listOB.append (obj) # def notify (self): # for obj in SE lf.listob:# obj.noTify (Len (Self.listob)) # # #适配器模式 # class b1:# def http (self): # BASEB (). http () # #工厂模式 # class factory:# def createa (SE LF): # return Basea () # def createb (self): # return Baseb () # # # # #单例模式 # class Logger (object): # log = none# @stati cmethod# def New (): # # import threading# #线程安全 # Mylock = Threading. Rlock () # Mylock.acquire () # if not logger.log:# Logger.log = Logger () # mylock.release () # # return Logg er.log# def write (self, v): # print ' Logger ', v## if __name__ = = "__main__": # a = Factory (). Createa () # b = Factory (). Createb () # # OBJS = Observer () # Objs.register (a) # objs.register (b) # # A.compute () # B.compute () # objs.notify () # # B1 = B1 () # b1.http () # # Logger.new (). Log.write (' V ')
More interested in Python related content readers can view this site topic: "Python Picture Operation skills Summary", "Python data structure and algorithm tutorial", "Python Socket Programming Skills Summary", "Python function Use Tips", " Python string manipulation Tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory operations tips
I hope this article is helpful for Python program design.