0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wikiのFacadePatternをpython3.xで実装(python3.9)

Posted at
facade_wiki.py

"""UML
    -------------->   ModuleB
   |                /    |
Facade --> ModuleA       |
   |                \    |
    -------------->   ModuleC
"""

# Module B
class Car:

    def __init__(self):
        self.__speed = 0
        self.__distance = 0

    @property
    def speed(self) -> int:
        return self.__speed

    @speed.setter
    def speed(self, speed: int):
        self.__speed = speed

    @property
    def distance(self) -> int:
        return self.__distance

    def run(self, minutes: int):
        self.__distance += self.__speed * minutes


# Module A
class Driver:

    def __init__(self, car: Car):
        self.__car = car

    def push_pedal(self, speed: int):
        self.__car.speed = speed

    def drive(self, minutes: int):
        self.__car.run(minutes)


# Facade
class DrivingSimulator:
    """Point
    Facadeクラスはあくまでサブシステム内部に仕事を投げるだけで複雑な実装は持たない
    """
    def simulate(self):
        car = Car()
        driver = Driver(car)
        driver.push_pedal(700)
        driver.drive(30)
        driver.push_pedal(750)
        driver.drive(20)
        print(f'The travel distance is {car.distance}m')

# Client
if __name__ == '__main__':
    driving_simulator = DrivingSimulator()
    driving_simulator.simulate()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?