Beruflich Dokumente
Kultur Dokumente
Design Patterns
(QVXUH\RXUFRGHLVVOHHN
HIILFLHQWDQGHOHJDQWE\PDVWHULQJ
SRZHUIXO3\WKRQGHVLJQSDWWHUQV
BIRMINGHAM - MUMBAI
Python: Master the Art of Design Patterns
Copyright 2016 Packt Publishing
All rights reserved. No part of this course may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this course to ensure the accuracy
of the information presented. However, the information contained in this course
is sold without warranty, either express or implied. Neither the authors, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this course.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this course by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN
www.packtpub.com
Credits
Reviewers
AMahdy AbdElAziz Production Coordinator
Shantanu N. Zagade
Grigoriy Beziuk
Krishna Bharadwaj
Justin Cano
Anthony Petitbois
Claudio Rodriguez
Maurice HT Ling
Evan Dempsey
Amitabh Sharma
Yogendra Sharma
3DWU\FMD6]DERZVND
Preface
Python is an object-oriented, scripting language that is used in wide range of
categories. In software engineering, a design pattern is a recommended solution
to a software design problem. Although not new, design patterns remain one of
the hottest topics in software engineering. Python 3 is more versatile and easier to
use than ever. It runs on all major platforms in a huge array of use cases. Coding
in Python minimizes development time and increases productivity in comparison
to other languages. Clean, maintainable code is easy to both read and write using
Pythons clear, concise syntax.
If you love designing and want to learn everything about it but have no idea where
to begin, then this course is built just for you. This learning path is divided into three
modules which will take you in this incredible journey of design patterns.
[i]
Preface
Module 2, Learning Python Design Patterns - Second Edition, Building on the success of the
previous edition, Learning Python Design Patterns, Second Edition will help you implement
real-world scenarios with Pythons latest release, Python v3.5. We start by introducing design
patterns from the Python perspective. As you progress through the module, you will learn
about Singleton patterns, Factory patterns, and Faade patterns in detail. After this, well look
at how to control object access with proxy patterns. It also covers observer patterns, command
patterns, and compound patterns. By the end of the module, you will have enhanced your
professional abilities in software architecture, design, and development.
All of the examples should run on any operating system supported by Python. If this
is not the case, please report it as a bug.
Some of the examples need a working Internet connection. Youll probably want to
have one of these for extracurricular research and debugging anyway!
In addition, some of the examples in this course rely on third-party libraries that do
not ship with Python. These are introduced within the course at the time they are
used, so you do not need to install them in advance. However, for completeness,
here is a list:
x pip
x requests
x pillow
x bitarray
[ ii ]
Preface
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about
this coursewhat you liked or disliked. Reader feedback is important for us as it
helps us develop titles that you will really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt course, we have a number of things to
help you to get the most from your purchase.
<RXFDQGRZQORDGWKHFRGHOHVE\IROORZLQJWKHVHVWHSV
1. Log in or register to our website using your e-mail address and password.
2. Hover the mouse pointer on the SUPPORT tab at the top.
3. Click on Code Downloads & Errata.
4. Enter the name of the course in the Search box.
5. 6HOHFWWKHFRXUVHIRUZKLFK\RXUHORRNLQJWRGRZQORDGWKHFRGHOHV
6. Choose from the drop-down menu where you purchased this course from.
7. Click on Code Download.
[ iii ]
Preface
2QFHWKHOHLVGRZQORDGHGSOHDVHPDNHVXUHWKDW\RXXQ]LSRUH[WUDFWWKHIROGHU
using the latest version of:
The code bundle for the course is also hosted on GitHub at https://github.com/
PacktPublishing/Python_Master-the-Art-of-Design-Patterns. We also have
other code bundles from our rich catalog of books, videos, and courses available at
https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes
GRKDSSHQ,I\RXQGDPLVWDNHLQRQHRIRXUFRXUVHVPD\EHDPLVWDNHLQWKHWH[W
or the codewe would be grateful if you could report this to us. By doing so, you
can save other readers from frustration and help us improve subsequent versions
RIWKLVFRXUVH,I\RXQGDQ\HUUDWDSOHDVHUHSRUWWKHPE\YLVLWLQJhttp://www.
packtpub.com/submit-errata, selecting your course, clicking on the Errata
Submission Form link, and entering the details of your errata. Once your errata are
YHULHG\RXUVXEPLVVLRQZLOOEHDFFHSWHGDQGWKHHUUDWDZLOOEHXSORDGHGWRRXU
website or added to any list of existing errata under the Errata section of that title.
[ iv ]
Preface
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all
media. At Packt, we take the protection of our copyright and licenses very seriously.
If you come across any illegal copies of our works in any form on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors and our ability to bring you
valuable content.
Questions
If you have a problem with any aspect of this course, you can contact us at
questions@packtpub.com, and we will do our best to address the problem.
[v]
Module 1: Python 3 Object-Oriented Programming - Second
Edition
[i]
Table of Contents
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Table of Contents
[ vi ]
Table of Contents
Implementation 592
Summary 601
Chapter 3: The Prototype Pattern 603
A real-life example 605
A software example 606
Use cases 606
Implementation 607
Summary 612
Chapter 4: The Adapter Pattern 615
A real-life example 616
A software example 616
Use cases 617
Implementation 617
Summary 621
Chapter 5: The Decorator Pattern 623
A real-life example 624
A software example 625
Use cases 625
Implementation 626
Summary 631
Chapter 6: The Facade Pattern 633
A real-life example 634
A software example 634
Use cases 635
Implementation 635
Summary 641
Chapter 7: The Flyweight Pattern 643
A real-life example 644
A software example 644
Use cases 644
Implementation 645
Summary 650
Chapter 8: The Model-View-Controller Pattern 651
A real-life example 652
A software example 652
Use cases 653
Implementation 654
Summary 658
[ vii ]
Table of Contents
[ viii ]
Table of Contents
[ ix ]