-
Notifications
You must be signed in to change notification settings - Fork 4
/
strategypattern.py
34 lines (26 loc) · 924 Bytes
/
strategypattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from __future__ import annotations
from abc import ABC, abstractmethod
class DriveStrategy(ABC):
@abstractmethod
def drive(self) -> None:
pass
class NormalDriveStrategy(DriveStrategy):
def drive(self) -> None:
print('Normal Drive Strategy')
class SportsDriveStrategy(DriveStrategy):
def drive(self) -> None:
print('Sports Drive Strategy')
class Vehicle:
def __init__(self, strategy: DriveStrategy) -> None:
self.strategy = strategy
def drive(self) -> None:
self.strategy.drive()
class OffRoadVehicle(Vehicle):
def __init__(self) -> None:
super().__init__(SportsDriveStrategy())
class PassengerVehicle(Vehicle):
def __init__(self) -> None:
super().__init__(NormalDriveStrategy())
# Testing the implementation
PassengerVehicle().drive() # Output: Normal Drive Strategy
OffRoadVehicle().drive() # Output: Sports Drive Strategy