Giter VIP home page Giter VIP logo

python-design-patterns's Introduction

Design patterns in Python

This repository contains python implementations of common design patterns based on Design Patterns with Python course.

Requirements

  • Python 3.6+
  • pip3

Installation

$ git clone https://github.com/kaduev13/python-design-patterns.git
$ cd python-design-patterns
$ pip3 install -r requirements.txt

Testing

$ pip3 install -r requirements-test.txt
$ pytest

Patterns

ABC Usage

Simple ABC usage scenario.

  • Runnable – is an interface with methods run, stop and property is_running.
  • BadClass – the class that inherited from Runnable, but does not implement it's methods/property.
  • GoodClass – the class that fully implements Runnable interface.

Strategy

Strategy pattern implementation. It allows you to encapsulate algorithms into the absolutely separated objects.

In this example we need to calculate Order shipping cost, that depends on shipping type: postal, FedEx, UPS.

  • Order – just an empty class, that represents real order.
  • ShippingCost – context, that contains selected shipping strategy.
  • Strategy – abstract strategy with the only method calculate(order: Order).
  • FedExStrategy, PostalStrategy, UPSStrategy – implementations of concrete shipping strategies.

Observer

Classic observer pattern implementation located in observer.lib package.

  • observer.lib.Observer – abstract observer class
  • observer.lib.Subject – abstract subject class with implemented default behavior

In the example we have some "source" – KPIs, that is a Subject descendant. Also we have two observers: ClosedTickets and KPIsDisplay. When the KPIs changes, it notifies observers instances and they do their job.

  • KPIs – event source, subject
  • ClosedTickets – observer, calculates (and prints) the sum of total closed tickets
  • KPIsDisplay – observer, just displays the current KPIs every time update is fired.

Command

Command pattern with commands auto-discovery and fallback to NoCommand if there is no available command.

  • command.commands.* – contains custom available commands.
  • command.executor.CommandExecutor – the class, that is able to discovery and execute commands.
  • command.command.Command – abstract console command.
  • command.no_command.NoCommand – fallback command, that will be executed if no other command found.

Singleton

Two implementations of Singleton pattern.

  • singleton.singleton_base.SingletonBase – singleton, implemented as a base class.
  • singleton.singleton_meta.SingletonMeta – singleton, implemented as a meta class.

Builder

In this example, we need to build computers with different configurations.

  • builder.computer.Computer – computer that we build.
  • builder.computer_builder.ComputerBuilder – abstract computer builder.
  • builder.default_computer_builder.DefaultComputerBuilder – default computer configuration.
  • builder.cheap_computer_builder.CheapComputerBuilder – cheap computer configuration.
  • builder.director.Director – director, the class, that knows how to build a computer.

Factory

Classic factory pattern implementation with factory-loader.

  • factory.loader.load_factory – loads factory based on factory's name.
  • factory.factories.factory.Factory – abstract car factory.
  • factory.factories.* – concrete cars factories implementations.
  • factory.cars.car.Car – abstract car.
  • factory.cars.* – concrete cars.

Abstract factory

There are two car manufacturers: GM and Ford. Each of them can create sport, luxury and economy cars.

  • abstract_factory.factories.factory.Factory – abstract factory that can create sport, luxury and sport cars.
  • abstract_factory.factories.* – manufacturer-specific factories (derived from base Factory).
  • abstract_factory.cars.car.* – abstract SportCar, EconomyCar and LuxuryCar
  • abstract_factory.cars.ford.* and abstract_factory.cars.gm.* – manufacturer-specific cars.

python-design-patterns's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.