Python is a great programming language, elegant and concise, and at the same time, very powerful. It has all the essential object-oriented features and can be used to implement design patterns. A design pattern is a general reusable solution to a commonly occurring problem within a given context. In everyday work, a programmer faces issues that have been solved so many times in the past by other developers that they have evolved common patterns to solve them.
The design pattern is not a concrete step to solve a problem, such as an algorithm; it is rather a practice or a description of how to solve a problem that can be used in different situations and implemented in different languages.
The design pattern accelerates the development process, providing a proven practice to solve some type of problem. It is often more preferable than using an unproven one because invisible problems often occur during the implementation, and the solving of unforeseen problems slows down the development dramatically.
Besides that, it's a tool of communication between programmers. It's much easier to say, "We use here the observer design pattern" rather than describing what the code actually does.
Studying design patterns is a good next step on the road to becoming a great developer, and this book is a good jumpstart.
Chapter 1, Model-View-Controller, describes what the model, view, and controller are, how to use them together, and ends with the implementation of a very simple URL shortening service.
Chapter 2, Creating Only One Object with the Singleton Pattern, describes ways to create a class whose instantiated object will only be one throughout the lifecycle of an application.
Chapter 3, Building Factories to Create Objects, describes the simple factory, Factory Method, Abstract Factory patterns, and how to use them to separate object creation.
Chapter 4, The Facade Design Pattern, is about simplifying the interface of a complex subsystem to facilitate the development.
Chapter 5, Facilitating Object Communication with Proxy and Observer Patterns, is a pattern for implementing a publisher-subscriber model and a proxy, which provides an object that controls access to another object.
Chapter 6, Encapsulating Calls with the Command Pattern, describes a pattern that encapsulates an action and its parameters.
Chapter 7, Redefining Algorithms with the Template Method, is about a pattern that provides the ability to create variations of the algorithm with minimum modifications.
You will require a Python 2.7 installation. It's usually available out of the box on most Unix and Linux distributives and can be downloaded and installed on Windows from http://python.org/.
This book is for developers with an intermediate Python knowledge who want to make learning design patterns their next step in their development career.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "As we see, Atom uses the <entry>
tag instead of the <item>
tag, link is stored in attribute instead of text node."
A block of code is set as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?> <rss version="2.0"> <channel> <title>A RSS example</title> <link>http://example.com</link> <description>Description of RSS example</description> <item> <title>The first news</title> <link>http://example.com/first</link> <description>Some description of the first news</description> </item> <item> <guid>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <title>The second news</title> <link>example.com/second</link> <description>Some description of the second news</description> <pubDate>Wed, 30 Sep 2013 13:00:00 GMT</pubDate> </item> </channel> </rss>
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
{
"main": {
"temp": 280.28,
}, "dt_txt": "2013-10-24 00:00:00" }
Any command-line input or output is written as follows:
$ python controller.py
New terms and important words are shown in bold as: "The other frequent use is to pass the Subject instance itself instead of data."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via 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 on 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 for all Packt books you have purchased from your account at http://www.packtpub.com. 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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright 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.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.