{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Factory Pattern\n", "1. A Creational pattern\n", "1. One Api to create different objects\n", "1. Also known as Virtual Constructor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Car Factory" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from abc import ABCMeta, abstractmethod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interfaces" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class IAuto(metaclass=ABCMeta):\n", " @abstractmethod\n", " def start(self):\n", " pass\n", "\n", " @abstractmethod\n", " def stop(self):\n", " pass" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class IFactory(metaclass=ABCMeta):\n", " @abstractmethod\n", " def create_auto(self):\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tesla Factory" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Tesla(IAuto):\n", " def __init__(self, car_name):\n", " self._carname = car_name\n", "\n", " def start(self):\n", " print(f\"{self._carname} barely makes any noise.\")\n", "\n", " def stop(self):\n", " print(f\"{self._carname} stops within moments\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class TeslaFactory(IFactory):\n", " def create_auto(self):\n", " self.tesla = Tesla('Tesla Model S')\n", " return self.tesla" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dodge Factory" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class DodgeSRT(IAuto):\n", " def __init__(self, car_name):\n", " self._carname = car_name\n", "\n", " def start(self):\n", " print(f\"{self._carname} Growls\")\n", "\n", " def stop(self):\n", " print(f\"{self._carname} Screeches to a stop!\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class DodgeFactory(IFactory):\n", " def create_auto(self):\n", " self.dodge_srt = DodgeSRT('Dodge SRT')\n", " return self.dodge_srt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Null Factory" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class NullCar(IAuto):\n", " def __init__(self, car_name):\n", " self._carname = car_name\n", "\n", " def start(self):\n", " print(f'Unknown Vehichle {self._carname}')\n", "\n", " def stop(self):\n", " print(f'Vehicle Stopped')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class NullFactory(IFactory):\n", " def create_auto(self):\n", " self.null_car = NullCar('')\n", " return self.null_car" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Auto Factory" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class AutoFactory():\n", " autos = {}\n", "\n", " def __init__(self):\n", " self.load_autos()\n", "\n", " def load_autos(self):\n", " self.autos.update([[Tesla.__name__, Tesla],\n", " [DodgeSRT.__name__, DodgeSRT],\n", " [NullCar.__name__, NullCar]]);\n", "\n", " def create_instance(self, car_name):\n", " if car_name in self.autos:\n", " return self.autos[car_name](car_name)\n", " else:\n", " return self.autos['NullCar'](car_name)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DodgeSRT Growls\n", "DodgeSRT Screeches to a stop!\n", "----------------\n", "Tesla barely makes any noise.\n", "Tesla stops within moments\n", "----------------\n", "Unknown Vehichle Chevrolet\n", "Vehicle Stopped\n", "----------------\n" ] } ], "source": [ "factory = AutoFactory()\n", "\n", "for car_name in 'DodgeSRT', 'Tesla', 'Chevrolet':\n", " car = factory.create_instance(car_name)\n", "\n", " car.start()\n", " car.stop()\n", " print('----------------')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sophisticated Auto Factory" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FactoryManager():\n", " factories = {}\n", "\n", " def __init__(self):\n", " self.load_factories()\n", "\n", " def load_factories(self):\n", " self.factories.update([[TeslaFactory.__name__, TeslaFactory],\n", " [DodgeFactory.__name__, DodgeFactory],\n", " [NullFactory.__name__, NullFactory]]);\n", "\n", " def create_factory(self, factory_name):\n", " if factory_name in self.factories:\n", " return self.factories[factory_name]()\n", " else:\n", " return self.factories['NullFactory']()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dodge SRT Growls\n", "Dodge SRT Screeches to a stop!\n", "----------------\n", "Tesla Model S barely makes any noise.\n", "Tesla Model S stops within moments\n", "----------------\n", "Unknown Vehichle \n", "Vehicle Stopped\n", "----------------\n" ] } ], "source": [ "factory_manager = FactoryManager()\n", "\n", "for factory_name in 'DodgeFactory', 'TeslaFactory', 'ChevroletFactory':\n", " factory = factory_manager.create_factory(factory_name)\n", " car = factory.create_auto()\n", "\n", " car.start()\n", " car.stop()\n", " print('----------------')" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }