python:装飾モードの実装

7708 ワード


#
!/usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append("..") import common class Component: def __init__(self,data): self.url = data["url"] self.noteId = data["noteId"] self.userId = data["userId"] self.folderId = data["folderId"] def execute(self): pass class NoteComponent(Component): def __init__(self,data,component=None): self.component = component Component.__init__(self, data) def setComponent(self,component): self.component = component class HtmlCatcher(NoteComponent): def execute(self): if self.component != None: self.component.execute() print self.__class__.__name__ class ImageCatcher(NoteComponent): def execute(self): if self.component != None: self.component.execute() print self.__class__.__name__ class shareNoteComponet(NoteComponent): def execute(self): if self.component != None: self.component.execute() print self.__class__.__name__ def process(message): component = None if message['TYPE'] & common.CATCH_PAGE: component = HtmlCatcher(message["DATA"]) if message['TYPE'] & common.CATCH_IMAGE: component1 = ImageCatcher(message["DATA"]) component1.setComponent(component) component = component1 if message['TYPE'] & common.SAVE_SHARED: component1 = shareNoteComponet(message["DATA"]) component1.setComponent(component) component = component1 component.execute() if __name__ == "__main__": message = { #"TYPE":common.CATCH_PAGE, #"TYPE":common.CATCH_IMAGE | common.SAVE_SHARED, "TYPE":common.CATCH_PAGE | common.SAVE_SHARED | common.CATCH_IMAGE, "DATA": { "url":"http://www.baidu.com", "noteId":100, "userId":100, "folderId":100, } } process(message)