Skip to content

Implementing design patterns from the book "Head First. Design patterns" in python

Notifications You must be signed in to change notification settings

balancy/design_patterns_head_first

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Implementation of design patterns in Python

Design patterns

Head First. Design patterns.

Description

  1. Strategy

Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside the original context object.

Different implementations of the Duck abstract class use different fly and quack behavior. Specific behavior is injected during Duck instance initialization.

  1. Observer

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Different sensor displays are subscribed to the weather station. When the weather station changes its measurements, all displays are notified.

  1. Decorator

The Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Condiment class is a decorator that wraps condiment class instance of beverage instance. So we could build a chain of condiments wrappers above beverages. It keeps beverage implementation close for modification, but open for extension.

  1. Abstract factory

Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.

  1. Factory method

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

  1. Singleton

Singleton is a creational design pattern that lets you ensure that a class has only one instance while providing a global access point to this instance.

The Singleton class creates a new instance or returns the existing one on instance creation.

  1. Command

The command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as method arguments, delay or queue a request’s execution, and support undoable operations.

The invoker gives commands to receivers via a common interface and keeps the list of executed commands. When needed, we could undo commands from the last to the first one.

  1. Adapter

The adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

There are two types of adapter: object and class adapter, one is implemented via composition and another one is implemented via multiple inheritance. The turkey adapter for duck allows the execution of duck methods on the Turkey class instance.

  1. Facade

The facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

The home theater facade class allows 'watch movie' and 'end movie' methods that encapsulate the complex logic of orchestrating all necessary devices.

  1. Template method

The Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.

The abstract caffeine beverage class has 2 implementations: Tea and Coffee. It contains a few abstract methods which are implemented their proper way in the child classes.

  1. Iterator

Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

There is an abstract menu class and two implementations where menu items are implemented as different type structures. The waitress could iterate over menu items on both menus in the same manner.

  1. Composite

Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

There is an abstract human class and two implementations: Human and Child. When we print the Human instance's family tree, it prints it recursively.

  1. State

State is a behavioral design pattern that lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.

The gumball machine changes its state according to performed by user actions.

  1. Proxy

Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

When the user asks to display the remote image, the proxy displays the fake image until the real remote image is downloaded.

Run

You need to have at least Python3.8 installed.

  1. Clone the repo
git clone https://github.com/balancy/design_patterns_head_first.git
  1. Run specific pattern example
python3 -m patterns.chapter_01_strategy.main

Test and Lint

You also need to have Poetry installed.

Run linting

make lint

Run type checking

make typecheck

Run tests

make test

Run all together

make check

About

Implementing design patterns from the book "Head First. Design patterns" in python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published