-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor chapter_06 pattern to pass mypy and flake8 checks
- Loading branch information
Showing
7 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Init file for chapter_06_command package.""" |
22 changes: 22 additions & 0 deletions
22
06_command/commands.py → patterns/chapter_06_command/commands.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,61 @@ | ||
"""Abstract base command class and two implementations.""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
from .receivers import Receiver | ||
|
||
|
||
class Command(ABC): | ||
"""Abstract base command class.""" | ||
|
||
def __init__(self, receiver: Receiver) -> None: | ||
"""Initialize with a receiver instance.""" | ||
self._receiver = receiver | ||
|
||
@abstractmethod | ||
def execute(self) -> None: | ||
"""Execute the command.""" | ||
... | ||
|
||
@abstractmethod | ||
def undo(self) -> None: | ||
"""Undo the command.""" | ||
... | ||
|
||
def __repr__(self) -> str: | ||
"""Return the command representation.""" | ||
return self.__class__.__name__ | ||
|
||
|
||
class TurnDeviceOnCommand(Command): | ||
"""Abstract command class implementation. | ||
Command is reponsible for turning device on. | ||
""" | ||
|
||
def execute(self) -> None: | ||
"""Execute the command.""" | ||
print(f'Executing "{self._receiver}" turning on.') | ||
self._receiver.on() | ||
|
||
def undo(self) -> None: | ||
"""Undo the command.""" | ||
print(f'Undoing "{self._receiver}" turning on.') | ||
self._receiver.off() | ||
|
||
|
||
class TurnDeviceOffCommand(Command): | ||
"""Abstract command class implementation. | ||
Command is reponsible for turning device off. | ||
""" | ||
|
||
def execute(self) -> None: | ||
"""Execute the command.""" | ||
print(f'Executing "{self._receiver}" turning off.') | ||
self._receiver.off() | ||
|
||
def undo(self) -> None: | ||
"""Undo the command.""" | ||
print(f'Undoing "{self._receiver}" turning off.') | ||
self._receiver.on() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
06_command/receivers.py → patterns/chapter_06_command/receivers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,57 @@ | ||
"""Abstract receiver class and 3 its implementations.""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Receiver(ABC): | ||
"""Abstract receiver class.""" | ||
|
||
@abstractmethod | ||
def on(self) -> None: | ||
"""Abstract method for turning the receiver on.""" | ||
... | ||
|
||
@abstractmethod | ||
def off(self) -> None: | ||
"""Abstract method for turning the receiver off.""" | ||
... | ||
|
||
def __repr__(self) -> str: | ||
"""Return the receiver representation.""" | ||
return self.__class__.__name__ | ||
|
||
|
||
class KitchenLight(Receiver): | ||
"""Kitchen light as receiver class implementation.""" | ||
|
||
def on(self) -> None: | ||
"""Turn the kitchen light on.""" | ||
print('Kitchen light is turned on.') | ||
|
||
def off(self) -> None: | ||
"""Turn the kitchen light off.""" | ||
print('Kitchen light is turned off.') | ||
|
||
|
||
class BedroomLight(Receiver): | ||
"""Bedroom light as receiver class implementation.""" | ||
|
||
def on(self) -> None: | ||
"""Turn the bedroom light on.""" | ||
print('Bedroom light is turned on.') | ||
|
||
def off(self) -> None: | ||
"""Turn the bedroom light off.""" | ||
print('Bedroom light is turned off.') | ||
|
||
|
||
class TV(Receiver): | ||
"""TV as receiver class implementation.""" | ||
|
||
def on(self) -> None: | ||
"""Turn the TV on.""" | ||
print('TV is turned on.') | ||
|
||
def off(self) -> None: | ||
"""Turn the TV off.""" | ||
print('TV is turned off.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ exclude = | |
.git, | ||
__pycache__, | ||
.venv, | ||
06_command/*, | ||
07_adapter/*, | ||
08_facade/*, | ||
09_template_method/* |