A simple implementation of Factory Pattern in language python
#!/usr/bin/env python | |
class UIFactory: | |
def __init__(self, **builders): | |
self.builders = builders | |
def createMenu(self): | |
return self.builders['menu']() | |
def createText(self): | |
return self.builders['text']() | |
class DayUIFactory(UIFactory): | |
def __init__(self): | |
super().__init__( | |
menu=DayMenuComponent, | |
text=DayTextComponent | |
) | |
class NightUIFactory(UIFactory): | |
def __init__(self): | |
super().__init__( | |
menu=NightMenuComponent, | |
text=NightTextComponent | |
) | |
class Component: | |
def __init__(self, category, theme): | |
self.category = category | |
self.theme = theme | |
def __repr__(self): | |
return '<{}{}Component {}>'.format( | |
self.theme.title(), | |
self.category.title(), | |
id(self) | |
) | |
class MenuCompnent(Component): | |
def __init__(self, theme): | |
super().__init__('menu', theme) | |
class DayMenuComponent(MenuCompnent): | |
def __init__(self): | |
super().__init__('day') | |
class NightMenuComponent(MenuCompnent): | |
def __init__(self): | |
super().__init__('night') | |
class TextComponent(Component): | |
def __init__(self, theme): | |
super().__init__('text', theme) | |
class DayTextComponent(TextComponent): | |
def __init__(self): | |
super().__init__('day') | |
class NightTextComponent(TextComponent): | |
def __init__(self): | |
super().__init__('night') | |
if __name__ == '__main__': | |
factory = DayUIFactory() | |
print(factory.createMenu()) # <DayMenuComponent 139692210593072> | |
print(factory.createText()) # <DayTextComponent 139692210593072> | |
factory = NightUIFactory() | |
print(factory.createMenu()) # <NightMenuComponent 139692210593016> | |
print(factory.createText()) # <NightTextComponent 139692210593016> |
#!/usr/bin/env python | |
class CloudFactory: | |
def __init__(self, config:dict): | |
self.config = config | |
class AWSCloudFactory(CloudFactory): | |
def authenticate(self, app_key, secret_key): | |
if app_key != 'aws-app-key': | |
raise ValueError(app_key) | |
elif secret_key != 'aws-secret-key': | |
raise ValueError(secret_key) | |
return AWSCloud() | |
def create(self): | |
conn = self.authenticate( | |
self.config['aws-app-key'], | |
self.config['aws-secret-key'] | |
) | |
return conn | |
class GoogleCloudFactory(CloudFactory): | |
def authenticate(self, secret_key): | |
if secret_key != 'google-secret-key': | |
raise ValueError(secret_key) | |
return GoogleCloud() | |
def create(self): | |
conn = self.authenticate(self.config['google-secret-key']) | |
return conn | |
class Cloud: | |
pass | |
class AWSCloud(Cloud): | |
def __repr__(self): | |
return f'<AWSCloud {id(self)}>' | |
class GoogleCloud(Cloud): | |
def __repr__(self): | |
return f'<GoogleCloud {id(self)}>' | |
if __name__ == '__main__': | |
config = { | |
'aws-app-key': 'aws-app-key', | |
'aws-secret-key': 'aws-secret-key', | |
'google-secret-key': 'google-secret-key' | |
} | |
print(AWSCloudFactory(config).create()) | |
print(GoogleCloudFactory(config).create()) |
#!/usr/bin/env python | |
class Factory: | |
@staticmethod | |
def get(name): | |
'''return specific product by name''' | |
if name == 'A': | |
return ConcreteProductA() | |
elif name == 'B': | |
return ConcreteProductB() | |
else: | |
raise ValueError(name) | |
class Product: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return f'Product <{self.name}>' | |
class ConcreteProductA(Product): | |
def __init__(self): | |
super().__init__('A') | |
class ConcreteProductB(Product): | |
def __init__(self): | |
super().__init__('B') | |
if __name__ == '__main__': | |
print(Factory.get('A')) # Product <A> | |
print(Factory.get('B')) # Product <B> | |
print(Factory.get('C')) # ValueError: C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment