Chain-of-responsibility pattern: Difference between revisions

Content deleted Content added
Python example: type hint
Line 374:
 
 
class Logger(object):
"""Abstract handler in chain of responsibility pattern."""
__metaclass__ = ABCMeta
Line 399:
return self.next
 
def message(self, msg: str, severity: LogLevel) -> None:
"""Message writer handler.
 
Message writer handler.
ArgsArguments:
msg (str): Message string.
severity (LogLevel): Severity of message as log level enum.
"""
Line 412 ⟶ 413:
 
@abstractmethod
def write_message(self, msg: str) -> None:
"""Abstract method to write a message.
 
Abstract method to write a message.
ArgsArguments:
msg (str): Message string.
Raises: NotImplementedError
"""
Line 422 ⟶ 424:
 
class ConsoleLogger(Logger):
def write_message(self, msg: str) -> None:
"""Overrides parent's abstract method to write to console.
"""
 
Overrides parent's abstract method to write to console.
ArgsArguments:
msg (str): Message string.
"""
print("Writing to console:", msg)
Line 431 ⟶ 434:
 
class EmailLogger(Logger):
"""Overrides parent's abstract method to send an email.
 
Arguments:
Args: msg (str): Message string.
"""
def write_message(self, msg: str) -> None:
Overrides parent's abstract method to send an email.
print(f"Sending via email:", {msg}")
Args: msg (str): Message string.
"""
def write_message(self, msg):
print("Sending via email:", msg)
 
 
class FileLogger(Logger):
"""Overrides parent's Abstractabstract method to write a messagefile.
 
Arguments:
Args: msg (str): Message string.
"""
def write_message(self, msg: str) -> None:
Overrides parent's abstract method to write a file.
print(f"Writing to log file:", {msg}")
Args: msg (str): Message string.
"""
def write_message(self, msg):
print("Writing to log file:", msg)
 
 
def main():
"""Building the chain of responsibility."""
"""
Building the chain of responsibility.
"""
logger = ConsoleLogger([LogLevel.ALL])
email_logger = logger.set_next(