Created
January 28, 2018 23:19
-
-
Save ferdef/4dd2bf4eba88e2d0c705d81f66aeca5b to your computer and use it in GitHub Desktop.
Factory Method
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
class Computer: | |
@staticmethod | |
def factory(type): | |
computers = { | |
'Macbook': Macbook, | |
'Surface': Surface, | |
'Desktop': Desktop | |
} | |
try: | |
return computers[type]() | |
except KeyError: | |
raise NotImplementedError('Computer version not implemented') | |
class Macbook: | |
def os_version(self): | |
return 'OSX Sierra' | |
class Surface: | |
def os_version(self): | |
return 'Windows 10' | |
class Desktop: | |
def os_version(self): | |
return 'Windows 7' | |
macbook = Computer.factory('Macbook') | |
print(macbook.os_version()) | |
surface = Computer.factory('Surface') | |
print(surface.os_version()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment