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

The Factory Method


The Factory Method is similar to SimpleFactory, but it is a little bit more complicated. As shown in the following diagram, typically this design pattern has an abstract class, Creator, that contains the factory_method which is responsible for creating some kind of objects. The some_operation method then works with the created object. The ConcreteCreator class can redefine the factory_method to change the created object in the runtime. The some_operation method does not care which object is created as long as it implements the Product interface and provides the implementation for all methods in that interface.

The essence of this pattern is to define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The interface is factory_method in the Creator and ConcreteCreator classes, which decides which subclass of Product to create. The Factory Method is based on inheritance; object creation is delegated to the...