Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Python Design Patterns

You're reading from   Mastering Python Design Patterns Start learning Python programming to a better standard by mastering the art of Python design patterns

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783989324
Length 212 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sakis Kasampalis Sakis Kasampalis
Author Profile Icon Sakis Kasampalis
Sakis Kasampalis
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. The Factory Pattern 2. The Builder Pattern FREE CHAPTER 3. The Prototype Pattern 4. The Adapter Pattern 5. The Decorator Pattern 6. The Facade Pattern 7. The Flyweight Pattern 8. The Model-View-Controller Pattern 9. The Proxy Pattern 10. The Chain of Responsibility Pattern 11. The Command Pattern 12. The Interpreter Pattern 13. The Observer Pattern 14. The State Pattern 15. The Strategy Pattern 16. The Template Pattern Index

Chapter 16. The Template Pattern

A key ingredient in writing good code is avoiding redundancy. In object-oriented programming (OOP), methods and functions are important tools that we can use to avoid writing redundant code. Remember the sorted() example in the previous chapter. The sorted() function is generic enough that it can be used to sort more than one data structure (lists, tuples, and namedtuples) using arbitrary keys. That's the definition of a good function.

Functions such as sorted() demonstrate the ideal case. In reality, we cannot always write 100 percent generic code. There are many algorithms that have some (but not all) common steps. A good example is breadth-first search (BFS) and depth-first search (DFS), two popular algorithms used in graph searching. Assume that we are asked to implement BFS and DFS in Python. Initially, we come up with two independent implementations (the graph.py file). The functions bfs() and dfs() return a tuple of (True, path) if a...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime