Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Example code from Head First Design Patterns translated to python

License

NotificationsYou must be signed in to change notification settings

dancergraham/HeadFirstDesignPatterns_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Head First Design Patterns Second EditionExample code fromHead 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!

NoteI am aiming for a mostly literal translation whilst trying to make the code a little more pythonic by, e.g. using python conventions forClassNames andmethod_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 📖:

packageheadfirst.designpatterns.strategy;publicabstractclassDuck {FlyBehaviorflyBehavior;QuackBehaviorquackBehavior;publicDuck() {    }publicvoidsetFlyBehavior(FlyBehaviorfb) {flyBehavior =fb;    }publicvoidsetQuackBehavior(QuackBehaviorqb) {quackBehavior =qb;    }abstractvoiddisplay();publicvoidperformFly() {flyBehavior.fly();    }publicvoidperformQuack() {quackBehavior.quack();    }publicvoidswim() {System.out.println("All ducks float, even decoys!");    }}

Sample Code : Python 🐍

From this repository :

classDuck():_fly_behavior=None_quack_behavior=Nonedefset_fly_behavior(self,fly_behavior):self._fly_behavior=fly_behaviordefset_quack_behavior(self,quack_behavior):self._quack_behavior=quack_behaviordefdisplay(self):raiseNotImplementedErrordefperform_fly(self):self._fly_behavior.fly()defperform_quack(self):self._quack_behavior.quack()defswim(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 asmall complete MVC implementation in JavaScript in another repo.

About

Example code from Head First Design Patterns translated to python

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp