Skip to content

Commit

Permalink
Refactor chapter_07 facade pattern to pass mypy and flake8 checks
Browse files Browse the repository at this point in the history
  • Loading branch information
balancy committed Oct 3, 2023
1 parent eedb1de commit 552e407
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
11 changes: 0 additions & 11 deletions 08_facade/main.py

This file was deleted.

1 change: 1 addition & 0 deletions patterns/chapter_07_facade/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init file for chapter_07_facade package."""
35 changes: 35 additions & 0 deletions 08_facade/devices.py → patterns/chapter_07_facade/devices.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,105 @@
"""Devices module."""


class Popper:
"""Popper device class."""

def turn_on(self) -> None:
"""Turn on popper."""
print('Pop-corn: popping')

def pop(self) -> None:
"""Pop pop-corn."""
print('Pop-corn: poped')

def turn_off(self) -> None:
"""Turn off popper."""
print('Pop-corn: off')


class Light:
"""Light device class."""

def turn_on(self) -> None:
"""Turn on light."""
print('Light: turned on')

def dim(self, value: int) -> None:
"""Dim light."""
print(f'Light: dimmed to {value}')

def turn_off(self) -> None:
"""Turn off light."""
print('Light: turned off')


class Screen:
"""Screen device class."""

def down(self) -> None:
"""Move screen down."""
print('Screen: is down')

def up(self) -> None:
"""Move screen up."""
print('Screen: is up')


class Projector:
"""Projector device class."""

def turn_on(self) -> None:
"""Turn on projector."""
print('Projector: turned on')

def set_input(self, input_value: str) -> None:
"""Set input device for projector."""
print(f'Projector: input set to {input_value}')

def set_wide_screen_mode(self) -> None:
"""Set wide screen mode for projector."""
print('Projector: switched to wide screen mode')

def turn_off(self) -> None:
"""Turn off projector."""
print('Projector: turned off')


class Amplifier:
"""Amplifier device class."""

def turn_on(self) -> None:
"""Turn on amplifier."""
print('Amplifier: turned on')

def set_dvd(self) -> None:
"""Set DVD for amplifier."""
print('Amplifier: DVD is set')

def set_surround_sound(self) -> None:
"""Set surround sound for amplifier."""
print('Amplifier: surround sound is set')

def set_volume(self, value: int) -> None:
"""Set volume for amplifier."""
print(f'Amplifier: volume is set to {value}')

def turn_off(self) -> None:
"""Turn off amplifier."""
print('Amplifier: turned off')


class DvdPlayer:
"""DVD player device class."""

def turn_on(self) -> None:
"""Turn on DVD player."""
print('DVD: turned on')

def play(self, movie: str) -> None:
"""Play movie on DVD player."""
print(f'DVD: playing {movie}')

def turn_off(self) -> None:
"""Turn off DVD player."""
print('DVD: turned off')
13 changes: 13 additions & 0 deletions 08_facade/facades.py → patterns/chapter_07_facade/facades.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Home theater (facade to handle all devices) module."""

from .devices import Amplifier, DvdPlayer, Light, Popper, Projector, Screen


class HomeTheaterFacade:
"""Home theater facade class."""

def __init__(self) -> None:
"""Initialize home theater facade."""
self._amp: Amplifier = Amplifier()
self._player: DvdPlayer = DvdPlayer()
self._light: Light = Light()
Expand All @@ -11,6 +16,10 @@ def __init__(self) -> None:
self._screen: Screen = Screen()

def watch_movie(self) -> None:
"""Watch movie method.
Incapsulate commands for all devices to watch a movie.
"""
print('Get ready to watch a movie...')
print()

Expand All @@ -37,6 +46,10 @@ def watch_movie(self) -> None:
print('Enjoy the movie!')

def end_movie(self) -> None:
"""End movie method.
Incapsulate commands for all devices to end watching a movie.
"""
print()
print('Shutting movie theater down...')
print()
Expand Down
18 changes: 18 additions & 0 deletions patterns/chapter_07_facade/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Main entrypoint for the Facade pattern example."""


from .facades import HomeTheaterFacade


def run_pattern_example() -> None:
"""Test facade pattern example.
Run only one facade method to make all devices work toghether.
"""
home_theater: HomeTheaterFacade = HomeTheaterFacade()
home_theater.watch_movie()
home_theater.end_movie()


if __name__ == '__main__':
run_pattern_example()
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ addopts = [
[tool.coverage.run]
omit = [
"*/main.py",
"*/chapter_07_facade/*"
]
parallel = true

Expand All @@ -42,7 +43,5 @@ show_missing = true

[tool.mypy]
exclude = [
"07_adapter/*",
"08_facade/*",
"09_template_method/*",
]

0 comments on commit 552e407

Please sign in to comment.