Giter Club home page Giter Club logo

headfirstdesignpatterns_python's Introduction

Head First Design Patterns python code

Head First Design Patterns Second EditionExample code from Head First Design Patterns second edition translated to python to help me understand and memorise the patterns.

I have added examples of pattern usage in the Python standard library and pypi - I am starting to see patterns everywhere!

Note I am aiming for a mostly literal translation whilst trying to make the code a little more pythonic by, e.g. using python conventions for ClassNames and method_names and putting all of the code in a single file where it makes sense to do so.

Patterns Implemented

Sample Code : Java

From the book πŸ“–:

package headfirst.designpatterns.strategy;

public abstract class Duck {
    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;

    public Duck() {
    }

    public void setFlyBehavior(FlyBehavior fb) {
        flyBehavior = fb;
    }

    public void setQuackBehavior(QuackBehavior qb) {
        quackBehavior = qb;
    }

    abstract void display();

    public void performFly() {
        flyBehavior.fly();
    }

    public void performQuack() {
        quackBehavior.quack();
    }

    public void swim() {
        System.out.println("All ducks float, even decoys!");
    }
}

Sample Code : Python 🐍

From this repository :

class Duck():
    _fly_behavior = None
    _quack_behavior = None

    def set_fly_behavior(self, fly_behavior):
        self._fly_behavior = fly_behavior

    def set_quack_behavior(self, quack_behavior):
        self._quack_behavior = quack_behavior

    def display(self):
        raise NotImplementedError

    def perform_fly(self):
        self._fly_behavior.fly()

    def perform_quack(self):
        self._quack_behavior.quack()

    def swim(self):
        print("All ducks float, even decoys! γ€°πŸ¦†γ€°")

Object Oriented Design Principles

  • Encapsulate what varies
  • Open-Closed Principle: Open for extension, closed for modification
  • Program to an interface, not to an implementation
  • Favour composition over inheritence
  • Dependency Inversion Principle
  • Depend upon abstractions
  • The Hollywood Principle : "Don't call us, we'll call you"
  • One Class, One Responsibility Principle
  • Single Responsibility Principle
  • Principle of Least Knowledge

Model View Controller (MVC)

I started to work on the MVC pattern here but have a small complete MVC implementation in JavaScript in another repo.

headfirstdesignpatterns_python's People

Contributors

dancergraham avatar thebjko avatar grahamknappacernis 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.