34,79 €
Leverage the power of Python design patterns to solve real-world problems in software architecture and design
This book is for Software architects and Python application developers who are passionate about software design. It will be very useful to engineers with beginner level proficiency in Python and who love to work with Python 3.5
With the increasing focus on optimized software architecture and design it is important that software architects think about optimizations in object creation, code structure, and interaction between objects at the architecture or design level. This makes sure that the cost of software maintenance is low and code can be easily reused or is adaptable to change. The key to this is reusability and low maintenance in design patterns.
Building on the success of the previous edition, Learning Python Design Patterns, Second Edition will help you implement real-world scenarios with Python's latest release, Python v3.5.
We start by introducing design patterns from the Python perspective. As you progress through the book, you will learn about Singleton patterns, Factory patterns, and Facade patterns in detail. After this, we'll 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 book, you will have enhanced your professional abilities in software architecture, design, and development.
This is an easy-to-follow guide to design patterns with hands-on examples of real-world scenarios and their implementation in Python v3.5. Each topic is explained and placed in context, and for the more inquisitive, there are more details on the concepts used.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 175
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book 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 book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, 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 book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2013
Second edition: February 2016
Production reference: 1080216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-803-8
www.packtpub.com
Author
Chetan Giridhar
Reviewer
Maurice HT Ling
Commissioning Editor
Kunal Parikh
Acquisition Editor
Denim Pinto
Content Development Editor
Merint Thomas Mathew
Technical Editor
Chinmay S. Puranik
Copy Editor
Tasneem Fatehi
Project Coordinator
Suzanne Coutinho
Proofreader
Safis Editing
Indexer
Priya Sane
Graphics
Kirk D'Penha
Production Coordinator
Shantanu N. Zagade
Cover Work
Shantanu N. Zagade
"Controlling complexity is the essence of computer programming."
--Brian Kernighan"All problems in computer science can be solved by another level of indirection (abstraction)."
--David WheelerThe preceding two quotes by two well known computer scientists illustrate the problem faced by the modern software designer—coming up with a good, stable, reusable, flexible solution to a software design problem.
Design patterns solve the preceding problems in the most elegant way. Design patterns abstract and present in neat, well-defined components and interfaces the experience of many software designers and architects over many years of solving similar problems. These are solutions that have withstood the test of time with respect to reusability, flexibility, scalability, and maintainability.
There have been many books on design patterns with the well-known Gang of Four (GoF) book forming the cornerstone of nearly the entire domain.
However, in this era of web and mobile computing, where programs tend to get written in high-level languages such as Python, Ruby, and Clojure, there is often a need for books that translate the rather esoteric language used in such books into more familiar terms, with reusable code written in these newer, more dynamic programming languages. This is especially true when it comes to newbie programmers who often tend to get lost in the complexities of design versus implementation and often require an expert helping hand.
This book fulfills that role very well. It uses the template of design patterns as laid out in the GoF book and adds a few others as well for completion—but before jumping into the patterns itself, gives the young and inexperienced reader the fundamentals of software design principles that have gone into the thinking behind the creation and evolution of these design patterns. It doesn't walk the gentle reader blindly into the maze of the pattern world, but lays out the fundamentals well before opening that door and carrying the reader along that path of learning.
The book is written with Python as the language for implementing the sample code for the patterns—and this makes excellent sense. As someone who has spent more than 12 years in the company of this wonderful programming language, I can attest to its beauty and simplicity and its effectiveness in solving problems ranging from routine to the most complex. Python is ideally suited to the rookie and young programmer, and with the ease of learning it, it is also a lot of fun to code in. The young programmer would find their time spent in the company of Python along in this book very rewarding and fruitful.
Chetan Giridhar has been working and contributing to Python for well over 7 years. He is ideally suited for the job of penning a book like this, as he has gone through some of the cycles of learning the complexities of implementation and design himself and has learned well through that process. He is a well-known speaker on a number of varied topics in Python and has delivered well-attended talks at Python conferences, such as PyCon India. He was amongst the invited speakers for conferences in the USA, Asia-Pacific, and New Zealand.
I believe this book, Learning Python Design Patterns, Second Edition, would be an excellent addition to the Learning series by Packt Publishing and would provide a set of skills to the toolbox of the young Python programmer that would take them gently and expertly to being able to design modular and efficient programs in Python.
Anand B Pillai
CTO—Skoov.com
Board Member—Python Software Foundation
Founder—Bangalore Python User's Group
Chetan Giridhar is a technology leader, open source enthusiast, and Python developer. He has written multiple articles on technology and development practices in magazines such as LinuxForYou and Agile Record, and has published technical papers in the Python Papers journal. He has been a speaker at PyCon conferences such as PyCon India, Asia-Pacific, and New Zealand and loves working on real-time communications, distributed systems, and cloud applications. Chetan has been a reviewer at Packt Publishing and has contributed to books on IPython Visualizations and Core Python.
I'd like to thank the Packt Publishing team, especially Merint Thomas Mathew, and the technical reviewer, Maurice HT Ling, for bringing out the best content in this book. Special thanks to my mentor, Anand B Pillai, for graciously accepting to review this book and writing the foreword. This book wouldn't be possible without the blessings of my parents, Jyotsana and Jayant Giridhar, and constant support and encouragement from my wife, Deepti, and my daughter, Pihu!
Maurice HT Ling has been programming in Python since 2003. Having completed his Ph D in bioinformatics and B Sc (honors) in molecular and cell biology from The University of Melbourne, he is currently a research fellow in Nanyang Technological University, Singapore, and an honorary fellow at The University of Melbourne, Australia. Maurice is the chief editor for computational and mathematical biology, and co-editor for The Python Papers. Recently, Maurice cofounded the first synthetic biology startup in Singapore, AdvanceSyn Pte. Ltd., as a director and chief technology officer. He is also the principal partner of Colossus Technologies LLP, Singapore. His research interests lie in life—biological life, artificial life, and artificial intelligence—using computer science and statistics as tools to understand life and its numerous aspects. In his free time, Maurice likes to read, enjoy a cup of coffee, write his personal journal, or philosophize on various aspects of life. You can reach him at his website and on his LinkedIn profile at http://maurice.vodien.com and http://www.linkedin.com/in/mauriceling, respectively.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Design patterns are among the most powerful methods of building large software systems. With an increasing focus on optimized software architecture and design, it is important that software architects think about optimizations in object creation, code structure, and interaction between objects at the architecture or design level. This makes sure that the cost of software maintenance is low, and code can be easily reused and is adaptable to change. Moreover, providing frameworks for reusability and separation of concerns is key to software development today.
Chapter 1, Introduction to Design Patterns, goes through the basics of object-oriented programming and discusses object-oriented design principles in detail. This chapter gives a brief introduction to the concept of design patterns so that you will be able to appreciate the context and application of design patterns in software development.
Chapter 2, The Singleton Design Pattern, covers one of the simplest and well-known Creational design patterns used in application development—the Singleton design pattern. The different ways in which we can create a Singleton pattern in Python are also covered in this chapter along with examples. This chapter also covers the Monostate (or Borg) design pattern, which is a variant of the Singleton design pattern.
Chapter 3, The Factory Pattern – Building Factories to Create Objects, discusses another creational pattern, the Factory pattern. You will also learn about the Factory Method pattern and Abstract Factory pattern with a UML diagram, real-world scenarios, and Python v3.5 implementations.
Chapter 4, The Façade Pattern – Being Adaptive with Façade, shows you another type of design pattern, the Structural design pattern. We will be introduced to the concept of Façade and learn how it is applicable to software design with the help of the Façade design pattern. You'll also learn its implementation with a sample Python application using a real-world scenario.
Chapter 5, The Proxy Pattern – Controlling Object Access, deals with the Proxy pattern that falls into the category of Structural design patterns. We will be introduced to the Proxy as a concept and discuss the design pattern and see how it is used in software application development. You'll also learn about the different variants of the Proxy pattern—Virtual Proxy, Smart Proxy, Remote Proxy, and Protective Proxy.
Chapter 6, The Observer Pattern – Keeping Objects in the Know, talks about the third type of design pattern—the behavioral design pattern. We will be introduced to the Observer design pattern with examples. In this chapter, you'll learn how to implement the Push and Pull models of the Observer pattern and the principles of loose coupling. We'll also see how this pattern is critical when it comes to applying it to cloud applications and distributed systems.
Chapter 7, The Command Pattern – Encapsulating Invocation, tells you about the Command design pattern. We will be introduced to the Command design pattern and discuss how it is used in software application development with a real-world scenario and Python implementation. We will also study two main aspects of the Command pattern—an implementation of redo/rollback operations and asynchronous task execution.
Chapter 8, The Template Method Pattern – Encapsulating Algorithm, discusses the Template design pattern. Like the Command pattern, the Template pattern falls into the category of Behavioral patterns. Here, we discuss the Template method pattern, and you will learn about Hooks with an implementation. We'll also cover the Hollywood principle that helps us appreciate this pattern better.
Chapter 9, Model-View-Controller – Compound Patterns, talks about Compound patterns. We will be introduced to the Model-View-Controller design pattern and discuss how it is used in software application development. MVC is easily one of the most used design patterns; in fact, many Python frameworks are based on this principle. You will learn about the details of MVC implementation with an example application written in Python Tornado (a framework used by Facebook).
Chapter 10, The State Design Pattern, introduces you to the State design pattern, which falls into the category of Behavioral patterns just like the Command or Template design patterns. We will discuss how it is used in software application development.
Chapter 11, AntiPatterns, tells you about AntiPatterns—what we shouldn't do as architects or software engineers.
All you need is Python v3.5, and you can download it from https://www.python.org/downloads/.
This book is for Python developers and software architects who care about software design principles and details of application development aspects in Python. It requires a basic understanding of programming concepts and beginner-level Python development experience. It will also be helpful for students and teachers in live learning environments.
Feedback from our readers is always welcome. Let us know what you think about this book—what 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.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
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.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we 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 of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
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.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
