Created
March 8, 2019 15:52
-
-
Save vlad-bezden/f44ff09afc673eab59a91e518660d448 to your computer and use it in GitHub Desktop.
Factory Method Design Pattern in Python and it's Implementations
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Factory Design Patterns and it's Implementation in Python" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Example is based on article from https://realpython.com/factory-method-python/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# pip install pyyaml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"import xml.etree.ElementTree as et\n", | |
"import yaml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Song:\n", | |
" def __init__(self, song_id, title, artist):\n", | |
" self.song_id = song_id\n", | |
" self.title = title\n", | |
" self.artist = artist" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Song:\n", | |
" def __init__(self, song_id, title, artist):\n", | |
" self.song_id = song_id\n", | |
" self.title = title\n", | |
" self.artist = artist\n", | |
"\n", | |
" def serialize(self, serializer):\n", | |
" serializer.start_object('song', self.song_id)\n", | |
" serializer.add_property('title', self.title)\n", | |
" serializer.add_property('artist', self.artist)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class ObjectSerializer:\n", | |
" def serialize(self, serializable, format):\n", | |
" serializer = factory.get_serializer(format)\n", | |
" serializable.serialize(serializer)\n", | |
" return serializer.to_str()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class JsonSerializer:\n", | |
" def __init__(self):\n", | |
" self._current_object = None\n", | |
"\n", | |
" def start_object(self, object_name, object_id):\n", | |
" self._current_object = {\n", | |
" 'id': object_id\n", | |
" }\n", | |
"\n", | |
" def add_property(self, name, value):\n", | |
" self._current_object[name] = value\n", | |
"\n", | |
" def to_str(self):\n", | |
" return json.dumps(self._current_object)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class XmlSerializer:\n", | |
" def __init__(self):\n", | |
" self._element = None\n", | |
"\n", | |
" def start_object(self, object_name, object_id):\n", | |
" self._element = et.Element(object_name, attrib={'id': object_id})\n", | |
"\n", | |
" def add_property(self, name, value):\n", | |
" prop = et.SubElement(self._element, name)\n", | |
" prop.text = value\n", | |
"\n", | |
" def to_str(self):\n", | |
" return et.tostring(self._element, encoding='unicode')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class YamlSerializer(JsonSerializer):\n", | |
" def to_str(self):\n", | |
" return yaml.dump(self._current_object)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class SerializerFactory:\n", | |
"\n", | |
" def __init__(self):\n", | |
" self._creators = {}\n", | |
"\n", | |
" def register_format(self, format, creator):\n", | |
" self._creators[format] = creator\n", | |
"\n", | |
" def get_serializer(self, format):\n", | |
" creator = self._creators.get(format)\n", | |
" if not creator:\n", | |
" raise ValueError(format)\n", | |
" return creator()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"factory = SerializerFactory()\n", | |
"\n", | |
"factory.register_format('JSON', JsonSerializer)\n", | |
"factory.register_format('XML', XmlSerializer)\n", | |
"factory.register_format('YAML', YamlSerializer)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"song = Song('1', 'Water of Love', 'Dire Straits')\n", | |
"serializer = ObjectSerializer()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'{\"id\": \"1\", \"title\": \"Water of Love\", \"artist\": \"Dire Straits\"}'" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"serializer.serialize(song, 'JSON')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'<song id=\"1\"><title>Water of Love</title><artist>Dire Straits</artist></song>'" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"serializer.serialize(song, 'XML')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"{artist: Dire Straits, id: '1', title: Water of Love}\\n\"" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"serializer.serialize(song, 'YAML')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome! Thank you for making this!