Skip to content

Examples from "Head First Design Patterns" implemented in Python

License

Notifications You must be signed in to change notification settings

ajbrzoz/Head-First-Design-Patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Examples from "Head First Design Patterns" implemented in Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages