Book Image

Learning Python Design Patterns

By : Gennadiy Zlobin
Book Image

Learning Python Design Patterns

By: Gennadiy Zlobin

Overview of this book

<p>Design pattern is a well-known approach to solve some specific problems which each software developer comes across during his work. Design patterns capture higher-level constructs that commonly appear in programs. If you know how to implement the design pattern in one language, typically you will be able to port and use it in another object-oriented programming language.</p> <p>The choice of implementation language affects the use of design patterns. Naturally, some languages are more applicable for certain tasks than others. Each language has its own set of strengths and weaknesses. In this book, we introduce some of the better known design patterns in Python. You will learn when and how to use the design patterns, and implement a real-world example which you can run and examine by yourself.</p> <p>You will start with one of the most popular software architecture patterns which is the Model- View-Controller pattern. Then you will move on to learn about two creational design patterns which are Singleton and Factory, and two structural patterns which are Facade and Proxy. Finally, the book also explains three behavioural patterns which are Command, Observer, and Template.</p>
Table of Contents (14 chapters)
Learning Python Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

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.

What this book covers

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.

What you need for this book

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/.

Who this book is for

This book is for developers with an intermediate Python knowledge who want to make learning design patterns their next step in their development career.

Conventions

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."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

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 , 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.

Customer support

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.

Downloading the example code

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.

Errata

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

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 with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.