O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Python Design Patterns

Book Description

Written for intermediate Python programmers, this excellent tutorial will help streamline your work through the use of Design Patterns. You’ll learn through example and clear explanations to expand your know-how and speed up your output.

  • Explore the Model-View-Controller pattern and learn how to build a URL shortening service
  • All design patterns use a real-world example that can be modified and applied in your software
  • No unnecessary theory! The book consists of only the fundamental knowledge that you need to know

In Detail

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.

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.

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.

Table of Contents

  1. Learning Python Design Patterns
    1. Table of Contents
    2. Learning Python Design Patterns
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
      2. Why Subscribe?
      3. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Model-View-Controller
      1. Model – the knowledge of the application
      2. View – the appearance of knowledge
      3. Controller – the glue between the model and view
      4. Benefits of using the MVC
      5. Implementation in Python
      6. Summary
    9. 2. Creating Only One Object with the Singleton Pattern
      1. A module-level singleton
      2. A classic singleton
      3. The borg singleton
      4. Implementation in Python
      5. Summary
    10. 3. Building Factories to Create Objects
      1. The Factory Method
        1. Advantages of using the Factory Method pattern
        2. The Factory Method implementation
      2. Abstract Factory
        1. Advantages of using the Abstract Factory pattern
        2. Abstract Factory implementation
      3. Abstract Factory versus Factory Method
      4. Summary
    11. 4. The Facade Design Pattern
      1. The Facade design pattern
        1. Problems solved by the Facade pattern
        2. Advantages of the Facade design pattern
      2. Facades in Python's standard library
      3. Implementation in Python
      4. Summary
    12. 5. Facilitating Object Communication with Proxy and Observer Patterns
      1. Proxy design pattern
        1. Problems solved by the Proxy pattern
        2. The use of the Proxy pattern
        3. Advantages and disadvantages of the Proxy design pattern
        4. Implementation in Python
      2. Observer design pattern
        1. Problems solved by the Observer pattern
        2. Use of the Observer pattern
        3. Advantages of the Observer pattern
        4. Implementation in Python
      3. Summary
    13. 6. Encapsulating Calls with the Command Pattern
      1. Command Pattern terminology
      2. Use cases of the Command design pattern
      3. Advantages and disadvantages of the Command design pattern
      4. Implementation in Python
      5. Summary
    14. 7. Redefining Algorithms with the Template Method
      1. The Template Method design pattern
        1. The benefits of the Template Method design pattern
        2. Using hooks
      2. Implementation in Python
      3. Summary
    15. Index