ebook img

Design Patterns in Python: Common GOF (Gang of Four) Design Patterns implemented in Python PDF

238 Pages·2021·1.558 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Design Patterns in Python: Common GOF (Gang of Four) Design Patterns implemented in Python

Design Patterns In Python Common GoF (Gang of Four) Design Patterns Implemented In Python Sean Bradley Copyright © 2019-2021 Sean Bradley Table of contents Table of contents 1. Design Patterns In Python 4 1.1 Pattern Types 5 1.2 Class Scope and Object Scope Patterns 5 2. Development Environment Setup 7 2.1 Example Code 8 2.2 Course Videos 9 2.3 VSCode 9 3. Coding Conventions 11 3.1 Python Interactive Console Versus *.py Scripts 11 3.2 PEP8 12 3.3 Pylint 12 3.4 Common Pylint Warning and Error Messages 12 3.5 Command Line Interfaces 13 4. UML Diagrams 15 4.1 A Basic Class 15 4.2 Directed Association 15 4.3 A Class That Extends/Inherits A Class 16 4.4 A Class That Implements An Interface 16 4.5 Aggregates 17 4.6 Composition 18 4.7 Pseudocode Annotation 18 5. Creational 19 5.1 Factory Design Pattern 19 5.2 Abstract Factory Design Pattern 28 5.3 Builder Design Pattern 43 5.4 Prototype Design Pattern 53 5.5 Singleton Design Pattern 61 6. Structural 69 6.1 Decorator Design Pattern 69 Copyright © 2019-2021 Sean Bradley - 2/238 - Table of contents 6.2 Adapter Design Pattern 78 6.3 Facade Design Pattern 88 6.4 Bridge Design Pattern 100 6.5 Composite Design Pattern 109 6.6 Flyweight Design Pattern 118 6.7 Proxy Design Pattern 127 7. Behavioral 135 7.1 Command Design Pattern 135 7.2 Chain of Responsibility Design Pattern 144 7.3 Observer Pattern 153 7.4 Interpreter Design Pattern 165 7.5 Iterator Design Pattern 180 7.6 Mediator Design Pattern 186 7.7 Memento Design Pattern 197 7.8 State Design Pattern 207 7.9 Strategy Design Pattern 213 7.10 Template Method Design Pattern 219 7.11 Visitor Design Pattern 227 8. Summary 237 Copyright © 2019-2021 Sean Bradley - 3/238 - 1. Design Patterns In Python 1. Design Patterns In Python Hello, I'm Sean Bradley, and welcome to my book on Design Patterns in Python. For over 20 years I have been an IT engineer building and managing real time, low latency, high availability, asynchronous, multi threaded, remotely managed, fully automated, monitored solutions in the education, aeronautical, banking, drone, gaming and telecommunications industries. I have also created and written hundreds of Open Source GitHub Repositories, Medium Articles and video tutorials on YouTube, Udemy and Skillshare. This book focuses on the 23 famous GoF (Gang of Four) Design Patterns implemented in Python. A Design Pattern is a description or template that can be repeatedly applied to a commonly recurring problem in software design. A familiarity of Design Patterns will be very useful when planning, discussing, managing and documenting your applications from now on and into the future. Also, throughout the book, as each design pattern is discussed and demonstrated using example code, I also introduce new python coding concepts with each new design pattern. So that as you progress through the book and try out the examples, you will also get experience and familiarity with some of the finer details of programming with python. So, in this book, you will learn about these 23 Design Patterns, • Creational • Factory • Abstract Factory • Builder • Prototype • Singleton • Structural • Decorator • Adapter • Facade • Bridge • Composite • Flyweight • Proxy Copyright © 2019-2021 Sean Bradley - 4/238 - 1.1 Pattern Types • Behavioral • Command • Chain of Responsibility • Observer Pattern • Interpreter • Iterator • Mediator • Memento • State • Strategy • Template • Visitor 1.1 Pattern Types In the list of patterns above, there are Creational, Structural and Behavioral patterns. • Creational : Abstracts the instantiation process so that there is a logical separation between how objects are composed and finally represented. • Structural : Focuses more on how classes and objects are composed using the different structural techniques, and to form structures with more or altered flexibility. • Behavioral : Are concerned with the inner algorithms, process flow, the assignment of responsibilities and the intercommunication between objects. 1.2 Class Scope and Object Scope Patterns Each pattern can be further specified whether it relates more specifically to classes or instantiated objects. Class scope patterns deal more with relationships between classes and their subclasses. Object scope patterns deal more with relationships that can be altered at runtime Pattern Description Scope Type Factory, Abstract Factory Defers object Class Creational creation to subclasses Copyright © 2019-2021 Sean Bradley - 5/238 - 1.2 Class Scope and Object Scope Patterns Pattern Description Scope Type Builder, Prototype, Singleton Defers object Object Creational creation to objects Adapter, Bridge, Composite, Describes a way Object Structural Decorator, Facade, Flyweight, to assemble Proxy objects Interpreter, Template Describes Class Behavioral algorithms and flow control Chain of Responsibility, Describes how Object Behavioral Command, Iterator, Mediator, groups of objects Memento, Observer, State, co-operate Strategy, Visitor Copyright © 2019-2021 Sean Bradley - 6/238 - 2. Development Environment Setup 2. Development Environment Setup SBCODE Video ID #d29be7 The most universal approach to installing Python is to visit the official Python download page at, https://www.python.org/downloads/ Normally this page will detect your operating system from the useragent in your browser and select which install is appropriate for you. There will be 64 and 32 bit versions for your operating system. At the time of writing this book, the option of downloading the 64bit version was the most common, and the version was 3.9.x. The code in this book will be using a simplified generic style of Python that should work in all versions since Python version 3. To test if you already have python on your system, depending on your operating system, whether Windows, Linux or Mac OSX, open a terminal/bash/cmd or PowerShell prompt. and type python -V Note the capital V in the above command. Sometimes python is named as python3 So you can also try python3 -V You are looking for a response that indicates you have Python 3 or above installed. Not an error, or Python 2.x On my windows workstation, if I use PowerShell, the response is PS> python -V Python 3.9.2 I have Python3 already installed using the official python link from above. If you are using a recent version of Linux or Mac OSX, then the command to check for the Python version on your system is most likely to be, Copyright © 2019-2021 Sean Bradley - 7/238 - 2.1 Example Code python3 -V Remember to follow the official install instructions for your operating system at https:// www.python.org/downloads/ 2.1 Example Code All the code examples in this book can be viewed from my GitHub repository at https://github.com/ Sean-Bradley/Design-Patterns-In-Python If you have Git installed, you can download a copy locally using the command git clone https://github.com/Sean-Bradley/Design-Patterns-In-Python.git or you can download a zip of all the code, using the link https://sbcode.net/python/zips/Design-Patterns-In-Python.zip or using wget on Linux wget https://sbcode.net/python/zips/Design-Patterns-In-Python.zip sudo apt install unzip unzip Design-Patterns-In-Python.zip cd Design-Patterns-In-Python/ You can then experiment with the code at your own pace and try out different variations. If you would rather type the code from the book, then follow these further recommendations. On my system, I have created a working folder on one of my spare drives, E:\, and then created a new folder in it named python_design_patterns, and then cd into it. You can use a different folder name if you prefer. PS C:\> e: PS E:\> mkdir python_design_patterns PS E:\> cd .\python_design_patterns Each section will be in a new folder named after the design pattern section. I.e,. The code that I write for the Factory pattern will be in its own subfolder named factory PS E:\python_design_patterns> mkdir factory PS E:\python_design_patterns> cd .\factory PS E:\python_design_patterns\factory> Copyright © 2019-2021 Sean Bradley - 8/238 - 2.2 Course Videos 2.2 Course Videos As part of the purchase of this book, I have also provided you with the ability to view for free, all of the videos which are part of my official Design Patterns in Python courses on Udemy, YouTube and Skillshare. To view the videos, in each section of this book, you will find several SBCODE Video IDs. Visit https://sbcode.net/python/ and at the beginning of each instructional page on the website, there are options to enter the code and view the related video. Press the SBCODE button in the Video Links section on the website and then enter the code that you found in each related section of this book. 2.3 VSCode If you are working on Windows, then I recommend to also install VSCode to use as your IDE when learning Python. This is optional and you may prefer to use Notepad or any other popular IDE that you can download or use online that will also assist you when writing Python. You can download VSCode from https://code.visualstudio.com/download You can then open VSCode from your current working folder by typing code . PS E:\python_design_patterns> code . And VSCode will open ready for you in your working folder where you can use it to add new files or folders as needed. Copyright © 2019-2021 Sean Bradley - 9/238 - 2.3 VSCode Copyright © 2019-2021 Sean Bradley - 10/238 -

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.