Created
August 4, 2018 07:54
-
-
Save ykon/bd17278fe6730d59c8aa90bdfb68157e to your computer and use it in GitHub Desktop.
FizzBuzz OOP
This file contains hidden or 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
# FizzBuzz OOP | |
from abc import ABC, abstractmethod | |
class BaseFizzBuzz(ABC): | |
def __init__(self, n: int) -> None: | |
self.number = n | |
@staticmethod | |
def isNumber(n: int) -> bool: | |
return False | |
@abstractmethod | |
def display(self) -> None: | |
pass | |
class FizzBuzz(BaseFizzBuzz): | |
def __init__(self, n: int) -> None: | |
super().__init__(n) | |
@staticmethod | |
def isNumber(n: int) -> bool: | |
return n % 15 == 0 | |
def display(self) -> None: | |
print('FizzBuzz') | |
class Fizz(BaseFizzBuzz): | |
def __init__(self, n: int) -> None: | |
super().__init__(n) | |
@staticmethod | |
def isNumber(n: int) -> bool: | |
return n % 3 == 0 | |
def display(self) -> None: | |
print('Fizz') | |
class Buzz(BaseFizzBuzz): | |
def __init__(self, n: int) -> None: | |
super().__init__(n) | |
@staticmethod | |
def isNumber(n: int) -> bool: | |
return n % 5 == 0 | |
def display(self) -> None: | |
print('Buzz') | |
class FZBZNumber(BaseFizzBuzz): | |
def __init__(self, n: int) -> None: | |
super().__init__(n) | |
@staticmethod | |
def isNumber(n: int) -> bool: | |
return True | |
def display(self) -> None: | |
print(self.number) | |
class FZBZFactory: | |
def __init__(self) -> None: | |
pass | |
def create(self, n: int) -> BaseFizzBuzz: | |
if FizzBuzz.isNumber(n): | |
return FizzBuzz(n) | |
elif Fizz.isNumber(n): | |
return Fizz(n) | |
elif Buzz.isNumber(n): | |
return Buzz(n) | |
else: | |
return FZBZNumber(n) | |
class FZBZPlayer: | |
def __init__(self, factory: FZBZFactory, n: int) -> None: | |
self.factory = factory | |
self.number = n | |
def play(self) -> None: | |
for i in range(1, self.number + 1): | |
fzbz = self.factory.create(i) | |
fzbz.display() | |
fzbz_player = FZBZPlayer(FZBZFactory(), 100) | |
fzbz_player.play() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment