Python design pattern Daquan

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.