{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Strategy Pattern\n", "1. It belongs to Behavioral Pattern\n", "1. Also Known As **Policy Pattern**\n", "1. Used to control operations of some objects\n", "\n", "> **Summary**\n", "> - It takes a family of Algos\n", "> - Encapsulates each one\n", "> - And make them interchangeable with each other\n", "> - Algos may vary independently\n", "> - No more if-elif\n", "> - Each Algo can be unit tested as well as system tested" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Shipping Module\n", "1. Supports FedEx, Postal, UPS\n", "1. Must be extendable" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from abc import ABCMeta, abstractmethod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interface" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class IPaymentStrategy(metaclass = ABCMeta):\n", " @abstractmethod\n", " def calculate(self, order):\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Payment Methods" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FedExStrategy(IPaymentStrategy):\n", " def calculate(self, order):\n", " return order + 0.1 * order" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class PostalStrategy(IPaymentStrategy):\n", " def calculate(self, order):\n", " return order + 0.05 * order" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class UPSStrategy(IPaymentStrategy):\n", " def calculate(self, order):\n", " return order + 0.12 * order" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Unified Payment Calculator" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class ShippingCost():\n", " def __init__(self, payment_strategy):\n", " self._payment_strategy = payment_strategy\n", " \n", " def shipping_cost(self, order):\n", " return self._payment_strategy.calculate(order)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Driver Program" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "33.0\n" ] } ], "source": [ "payment_strategy = FedExStrategy()\n", "cost_calc = ShippingCost(payment_strategy)\n", "\n", "print(cost_calc.shipping_cost(30))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "31.5\n" ] } ], "source": [ "payment_strategy = PostalStrategy()\n", "cost_calc = ShippingCost(payment_strategy)\n", "\n", "print(cost_calc.shipping_cost(30))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "33.6\n" ] } ], "source": [ "payment_strategy = UPSStrategy()\n", "cost_calc = ShippingCost(payment_strategy)\n", "\n", "print(cost_calc.shipping_cost(30))" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }