You are previewing Learning Python Design Patterns - Second Edition.
O'Reilly logo
Learning Python Design Patterns - Second Edition

Book Description

Leverage the power of Python design patterns to solve real-world problems in software architecture and design

About This Book

  • Understand the structural, creational, and behavioral Python design patterns

  • Get to know the context and application of design patterns to solve real-world problems in software architecture, design, and application development

  • Get practical exposure through sample implementations in Python v3.5 for the design patterns featured

  • Who This Book Is For

    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

    What You Will Learn

  • Enhance your skills to create better software architecture

  • Understand proven solutions to commonly occurring design issues

  • Explore the design principles that form the basis of software design, such as loose coupling, the Hollywood principle and the Open Close principle among others

  • Delve into the object-oriented programming concepts and find out how they are used in software applications

  • Develop an understanding of Creational Design Patterns and the different object creation methods that help you solve issues in software development

  • Use Structural Design Patterns and find out how objects and classes interact to build larger applications

  • Focus on the interaction between objects with the command and observer patterns

  • Improve the productivity and code base of your application using Python design patterns

  • In Detail

    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 Façade 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.

    Style and approach

    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.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Learning Python Design Patterns Second Edition
      1. Table of Contents
      2. Learning Python Design Patterns Second Edition
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewer
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. 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
      9. 1. Introduction to Design Patterns
        1. Understanding object-oriented programming
          1. Objects
          2. Classes
          3. Methods
        2. Major aspects of object-oriented programming
          1. Encapsulation
          2. Polymorphism
          3. Inheritance
          4. Abstraction
          5. Composition
        3. Object-oriented design principles
          1. The open/close principle
          2. The inversion of control principle
          3. The interface segregation principle
          4. The single responsibility principle
          5. The substitution principle
        4. The concept of design patterns
          1. Advantages of design patterns
          2. Taxonomy of design patterns
          3. Context – the applicability of design patterns
        5. Patterns for dynamic languages
        6. Classifying patterns
          1. Creational patterns:
          2. Structural patterns
          3. Behavioral patterns
        7. Summary
      10. 2. The Singleton Design Pattern
        1. Understanding the Singleton design pattern
          1. Implementing a classical Singleton in Python
        2. Lazy instantiation in the Singleton pattern
        3. Module-level Singletons
        4. The Monostate Singleton pattern
        5. Singletons and metaclasses
        6. A real-world scenario – the Singleton pattern, part 1
        7. A real-world scenario – the Singleton pattern, part 2
        8. Drawbacks of the Singleton pattern
        9. Summary
      11. 3. The Factory Pattern – Building Factories to Create Objects
        1. Understanding the Factory pattern
        2. The Simple Factory pattern
        3. The Factory Method pattern
          1. Implementing the Factory Method
          2. Advantages of the Factory method pattern
        4. The Abstract Factory pattern
          1. Implementing the Abstract Factory pattern
        5. The Factory method versus Abstract Factory method
        6. Summary
      12. 4. The Façade Pattern – Being Adaptive with Façade
        1. Understanding Structural design patterns
        2. Understanding the Façade design pattern
        3. A UML class diagram
          1. Façade
          2. System
          3. Client
        4. Implementing the Façade pattern in the real world
        5. The principle of least knowledge
        6. Frequently asked questions
        7. Summary
      13. 5. The Proxy Pattern – Controlling Object Access
        1. Understanding the Proxy design pattern
        2. A UML class diagram for the Proxy pattern
        3. Understanding different types of Proxies
          1. A virtual proxy
          2. A remote proxy
          3. A protective proxy
          4. A smart proxy
        4. The Proxy pattern in the real world
        5. Advantages of the Proxy pattern
        6. Comparing the Façade and Proxy patterns
        7. Frequently asked questions
        8. Summary
      14. 6. The Observer Pattern – Keeping Objects in the Know
        1. Introducing Behavioral patterns
        2. Understanding the Observer design pattern
          1. A UML class diagram for the Observer pattern
        3. The Observer pattern in the real world
        4. The Observer pattern methods
          1. The pull model
          2. The push model
        5. Loose coupling and the Observer pattern
        6. The Observer pattern – advantages and disadvantages
        7. Frequently asked questions
        8. Summary
      15. 7. The Command Pattern – Encapsulating Invocation
        1. Introducing the Command pattern
        2. Understanding the Command design pattern
          1. A UML class diagram for the Command pattern
        3. Implementing the Command pattern in the real world
          1. Design considerations
        4. Advantages and disadvantages of Command patterns
        5. Frequently asked questions
        6. Summary
      16. 8. The Template Method Pattern – Encapsulating Algorithm
        1. Defining the Template Method pattern
          1. Understanding the Template Method design pattern
          2. A UML class diagram for the Template Method pattern
        2. The Template Method pattern in the real world
        3. The Template Method pattern – hooks
        4. The Hollywood principle and the Template Method
        5. The advantages and disadvantages of the Template Method pattern
        6. Frequently asked questions
        7. Summary
      17. 9. Model-View-Controller – Compound Patterns
        1. An introduction to Compound patterns
        2. The Model-View-Controller pattern
          1. Model – knowledge of the application
          2. View – the appearance
          3. Controller – the glue
        3. A UML class diagram for the MVC design pattern
        4. The MVC pattern in the real world
          1. Modules
        5. Benefits of the MVC pattern
        6. Frequently asked questions
        7. Summary
      18. 10. The State Design Pattern
        1. Defining the State design pattern
          1. Understanding the State design pattern
          2. Understanding the State design pattern with a UML diagram
        2. A simple example of the State design pattern
          1. The State design pattern with v3.5 implementation
        3. Advantages/disadvantages of the State pattern
        4. Summary
      19. 11. AntiPatterns
        1. An introduction to AntiPatterns
        2. Software development AntiPatterns
          1. Spaghetti code
          2. Golden Hammer
          3. Lava Flow
          4. Copy-and-paste or cut-and-paste programming
        3. Software architecture AntiPatterns
          1. Reinventing the wheel
          2. Vendor lock-in
          3. Design by committee
        4. Summary
      20. Index