observers, that is, dependent objects, each time data changes, these 2 view will changeclassHexviewer (object):defUpdate (self, subject):Print 'hexviewer:subject%s has data 0x%x'%(Subject.name, Subject.data)classDecimalviewer (object):defUpdate (self, subject):Print 'decimalviewer:subject%s has data%d'%(Subject.name, Subject.data)if __name__=='__main__': Data1= Data ('Data 1') Data2= Data ('Data 2') View1=decimalviewer () view2=hexviewer () Data1.attach (view1) Data1.attach (view2) Data2.attach
):def __init__(self): Self.name='Processserver'self.state=state.newdefboot (self):Print('booting the {}'. Format ( self) self.state=state.runningdefKill (Self, restart=True):Print('killing {}'. Format ( self) self.state= Stae.restartifRestartElseState.zobmiedefcreate_process (self, user, name):Print("trying to create process ' {} ' for user ' {} '". Format (name, user)classWindowsServer:PassclassNetworkserver:PassclassOperatingSystem:def __init__(self): Self.fs=fileserver () self.ps=Processserve
The appearance pattern of design patterns in Python advocates less coupling with multiple modules for code management, and is illustrated with examples below.
Application Features:
When many complex and small functions need to invoke requirements, and these calls often have a certain relevance, that is, a call is a series of.
Structural characteristics:
The orig
simple point design, the state into the door state, stop state and operating state, the operation is divided into open door, closed, run, stop, the process is also very complex. First of all, the open door state can not open, run, stop, stop the state can not be closed, stop, operation status can not open, close, run. To use a if...else ... Implementation, first code confusion, difficult to maintain, and the second is not easy to expand. As for the v
#-*-Coding:utf-8-*-__author__ = ' Administrator '#python高级编程: A useful design pattern#设计械是可复用的, some of the most popular books in a program that provide language-related solutions to the problems of software design awareness:"""Gamma, Heim, Johson and Vlissides a.k.a "Foursome (GOF)" written elements of reusable object-oriented software (Chinese:It is considered
Said to write a summary, the result because find an internship, moving dragged more than half a month, but it doesn't matter, is that they write their own look, to later want to see when turned out to look at, is a kind of miss.
Let's talk about Python, people say that C + + programmers are relatively familiar with python, my feeling is also, feel if there is a C basis, plus C + + object-oriented Understan
disadvantage of the singleton mode?While the singleton mode works fine but there are still some problems because the singleton has global access, the following issues may occur:
Multiple references to the same object may be created 。 Because a singleton creates only one object, a reference is created on an object in this case.
So far, we have introduced the singleton model of the relevant content, follow-up may write about other
_remove_stop_words (self,words):Raise Notimplementederrordef _stem_words (self,words):Raise Notimplementederrordef _frequency (self,setemmed_words):counts={}For word in Setemmed_words:Counts[word]=counts.get (word,0) +1#BasicIndex实现如下:From Itertools import GroupByClass Basicindex (Indexer):_stop_words= (' He ', ' she ', ' is ', ' and ', ' or ')def _remove_stop_words (self,words):return (word for word in words if Word is not in self._stop_words)def _stem_words (self,words):Return ((Len (word) >2
First Lecture: 1. Object-oriented design patterns and principles Introduction to Design Patterns: Each pattern describes a problem that recurs around us, and the core of the solution to the problem.--christopher alexander{Architect} Software designers ' understanding of the concept of
. Intermediary mode (mediator pattern): Use the mediator mode to centralize the complex communication and control between related objects.6. Memo Mode (Memento pattern): When you need to return an object to its previous state (for example, your user requests "undo"), you use Memo mode.7. Observer pattern (Observer pattern): Defines a one-to-many dependency between objects, so that when an object changes state, objects that depend on it are notified and updated automatically.8. State pattern: All
1. due to the different characteristics of the language, the design mode and implementation of the difficulty will be different2. Some patterns have been built into the language, such as the iterator pattern. 3. The singleton mode can be directly implemented with module-level variables .4. Normal Factory mode can be implemented directly by passing in "class name" as parameterSingleton Mode Example:12_eg.pyC
(width)] ' for ' in range (1, width-1): rows[0][x] = HO Rizontal Rows[height-1][x] = Horizontal for y in range (1, height-1): rows[y][0] = VERTICAL row S[Y][WIDTH-1] = VERTICAL for y, X in ((0, 0), (0, Width-1), (height-1, 0), (height-1, width-1)): ROWS[Y][X] = CORNER return rowsclass rectangle:def __init__ (self, x, y, width, height, fill, stroke): s elf.x = x self.y = y Self.rows = _create_rectangle (width, height, BLANK if fill = = "White" El Se "%") class Text:def __init__ (self, x,
):If OBJS in Cls._obj:Cls._obj.remove (OBJS)@classmethoddef not1 (cls,sub):Event=cls (sub)For O in Cls._obj:O (Event)#思路是观察者使用event类方法注册自己, and is obtained by carrying the event that triggers these events, as follows:Class Writevent (object):def __repr__ (self):Return ' writevent (self) 'Def log (event):print '%s:was writees '%event._objWritevent.reg (log)Class Anot (object):def __call__ (self, E):print ' Yean%s told me! ' %eWritevent.reg (Anot)WRITEVENT.NOT1 (' A given file ')#对这个实现, the follow
It seems that the following is the most elegant implementation.Other, either node redundancy, or initialize ugly ...#!/usr/bin/env python#-*-coding:utf-8-*-classNode:def __init__(self, initdata): Self.__data=InitData self.__next=NonedefGetData (self):returnSelf.__data defGetNext (self):returnSelf.__next defSetData (Self, newdata): Self.__data=NewDatadefSetnext (Self, newnext): Self.__next=Newnextclasssincyclinkedlist:def __init__(self): Self.head=No
fraiche sauce') defadd_topping (self):Print('adding the topping (Mozzaralla, bacon, ham, mushrooms, red onion, oregano) to you creamy bacon') self.pizza.topping.append ([t forTinch(Pizzatopping.mozzaralla, Pizzatopping.bacon, Pi Zzatopping.ham, Pizzatopping.mushrooms, Pizzatopping.red_onion, Pizzatopping.oregano)]) Time.sleep (Step_delay)Print('Done with the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano)') defBake (self): self.progress=pizzaprogress.bakingPrint('B
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.