Giter Site home page Giter Site logo

head-first-design-patterns's Introduction

Head-First-Design-Patterns

Examples from "Head First Design Patterns" implemented in Python

This repo contains code examples from the book "Head First Design Patterns" ported to Python. My main aim was to make them as pythonic as possible.

Patterns covered so far:
  • Strategy
  • Observer
  • Decorator
  • Factory
  • Singleton
  • Command
  • Adapter
Remarks:

It should be noted that the book's code was written in Java and that in Python, since it allows for multiple inheritance, there's no Java-style distinction between abstract superclass and interface.

  • Strategy - There are several ways to define abstract classes in Python. Apart from the way I've used (raising an error when an unimplemented method is called from a subclass), abstract classes can be created with the abc module. There's also an interesting example in Peter Norvig's IAQ where he uses a separate function not only raising the appropriate error but also keeping track of the stack frame.

  • Observer - This implementation is a little different from the original one: the Subject's attributes are not passed to the Observer directly as its update() function's parameters but they can be accessed through self.weather_data.

  • Singleton - Here we take advantage of Python's new special method.

  • Adapter - Since Java doesn't support multiple inheritance, it's impossible to implement a Class Adapter. Python, on the other hand, has multiple inheritance, so I've added an implementation of this type of Adapter pattern. The class inherits from the concrete WildTurkey class as it has all the necessary methods already implemented.

head-first-design-patterns's People

Contributors

ajbrzoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.