Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dancergraham/headfirstdesignpatterns_python
Example code from Head First Design Patterns translated to python
https://github.com/dancergraham/headfirstdesignpatterns_python
design-pattern design-patterns factory headfirst object-oriented-programming observer-pattern oop python strategy
Last synced: 5 days ago
JSON representation
Example code from Head First Design Patterns translated to python
- Host: GitHub
- URL: https://github.com/dancergraham/headfirstdesignpatterns_python
- Owner: dancergraham
- License: mit
- Created: 2021-06-13T17:43:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-26T20:53:13.000Z (about 1 month ago)
- Last Synced: 2025-01-20T08:06:07.940Z (12 days ago)
- Topics: design-pattern, design-patterns, factory, headfirst, object-oriented-programming, observer-pattern, oop, python, strategy
- Language: Python
- Homepage:
- Size: 256 KB
- Stars: 102
- Watchers: 3
- Forks: 39
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Head First Design Patterns python code
Example code from [Head First Design Patterns second edition](https://wickedlysmart.com/head-first-design-patterns/) 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
- [x] [Strategy](chapter01_strategy)
- [x] [Observer](chapter02_observer)
- [x] [Decorator](chapter03_decorator)
- [ ] [Factory Method](chapter04_factory)
- [ ] [Simple Factory](chapter04_factory)
- [x] [Abstract Factory](chapter04_factory)
- [ ] [Builder](chapter04_factory#builder-%EF%B8%8F%EF%B8%8F) _(Bonus Pattern)_
- [x] [Singleton](chapter05_singleton)
- [x] [Command](chapter06_command)
- [x] [Adapter](chapter07_adapter_facade)
- [x] [Façade](chapter07_adapter_facade)
- [x] [Template Method](chapter08_template)
- [x] [Iterator](chapter09_iterator_composite)
- [ ] [Composite](chapter09_iterator_composite)
- [x] [State](chapter10_state)
- [ ] Proxy
- [ ] [Model View Controller (MVC)](chapter12_compound)
- [ ] [Manager](chapter14_leftover) _(Bonus Pattern)_
- [ ] [Result](extra_result) _(Bonus Pattern)_## Sample Code : Java
From the book π:
```java
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 :
```python
class Duck():
_fly_behavior = None
_quack_behavior = Nonedef set_fly_behavior(self, fly_behavior):
self._fly_behavior = fly_behaviordef set_quack_behavior(self, quack_behavior):
self._quack_behavior = quack_behaviordef display(self):
raise NotImplementedErrordef 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](https://github.com/dancergraham/HeadFirstJs/blob/master/battleship2D.js) in another repo.