python责任链模式

责任链模式是一种行为设计模式,它允许你将请求沿着处理者链进行传递,直到有一个处理者能够处理它为止。在Python中,你可以使用多线程来实现责任链模式的框架。

首先,你需要定义一个基础的处理者类,它包含处理请求的方法。然后,你可以创建多个继承自该基础类的具体处理者类,每个类都负责处理不同类型的请求。在处理请求时,每个处理者可以决定是自己处理请求还是将其传递给下一个处理者。

import threading
import queue

class Handler(threading.Thread):
    def __init__(self, name, successor=None):
        super().__init__(name=name)
        self.successor = successor
        self.queue  = queue.Queue()
        # self.flag = True
 
    def handle_request(self,request):
        pass

    def run(self):
        while self.flag:
            request = self.queue.get()
            self.handle_request(request)
            self.queue.task_done()
        print('finished')

class ConcreteHandlerA(Handler):
    def __init__(self, name, successor=None):
        super().__init__(name=name)
        self.flag = True
    def handle_request(self, request):
        if request == 'A':
            print("ConcreteHandlerA handles the request: {}".format(request))
        elif self.successor is not None:
            self.successor.handle_request(request)
    def run(self):
        while self.flag:
            try:
                request = self.queue.get(timeout=0.1)
            except:
                continue
            self.handle_request(request)
            self.queue.task_done()
        print('A finished')

class ConcreteHandlerB(Handler):
    def __init__(self, name, successor=None):
        super().__init__(name=name)
        self.flag = True
    def handle_request(self, request):
        if request == 'B':
            print("ConcreteHandlerB handles the request: {}".format(request))
        elif self.successor is not None:
            self.successor.handle_request(request)
    def run(self):
        while self.flag:
            try:
                request = self.queue.get(timeout=0.1)
            except:
                continue
            if request:
                self.handle_request(request)
            self.queue.task_done()
        print('B finished')

class ConcreteHandlerC(Handler):
    def __init__(self, name, successor=None):
        super().__init__(name=name)
        self.flag = True
    def handle_request(self, request):
        if request == 'C':
            print("ConcreteHandlerC handles the request: {}".format(request))
        elif self.successor is not None:
            self.successor.handle_request(request)
    def run(self):
        while self.flag:
            try:
                request = self.queue.get(timeout=0.1)
            except:
                continue
            if request:
                self.handle_request(request)
            self.queue.task_done()
        print('C finished')

# 创建处理者实例并构建处理者链
handlerA = ConcreteHandlerA(name='HandlerA')
handlerB = ConcreteHandlerB(name='HandlerB', successor=handlerA)
handlerC = ConcreteHandlerC(name='HandlerC', successor=handlerB)

# 启动处理者线程
handlerA.start()
handlerB.start()
handlerC.start()

# 向处理者链发送请求
handlerC.queue.put('A')
handlerC.queue.put('B')
handlerC.queue.put('C')

# 等待所有请求处理完成
handlerC.flag = False
handlerB.flag = False
handlerA.flag = False

你可能感兴趣的:(python,责任链模式,开发语言)