Exploring Design Patterns in Python | by Dan Root | Towards Data Science

Design Patterns are used to help programmers with understanding concepts, teaching, learning, and building on other great working ideas and concepts. So, when you are thinking Design Patterns think of solving problems. Design Patterns are models built to help structure and solve simple to complicated issues. A good amount programmers have actually implemented them in their own code without realizing it. That is because it is such an abstract idea that you can use it without even really thinking about what it is. By knowing what Design Patterns are and how to use them you can overcome obstacles that would otherwise seem overwhelming. You also, bring another level of awareness to the table and instead of accidently implementing a shacky Design Pattern, you can implement a great Design Pattern on purpose. Design Patterns are being used increasingly in the software community and it is a big plus knowing them. It is important that you know at least the basics of Object-Oriented Programming. More specifically that you know about Inheritance and Polymorphism.
Design Patterns are often referred to as Design Templates, as they provide a template on how to handle common reoccurring issues. You can use existing Design Patterns or even create your own. There are many categories of Design Patterns, here are some popular ones:
Creative
Structural
Behavioral
They apply to any OOP (Object Orientated Programming) programming language.They are flexible. Constantly changing, evolving, and updating.Left unfinished often for creativity.
They apply to any OOP (Object Orientated Programming) programming language.
They are flexible. Constantly changing, evolving, and updating.
Left unfinished often for creativity.
Brief description of the Pattern.
What is the underlying issue you are trying to solve?
Specifies where a Pattern is applicable. Defines the structure and behavior of the Pattern.
The classes and objects that are involved in the Pattern.
Effects that may incur from using the Pattern.
Factory
Abstract Factory
Singleton
Builder
Prototype
Object Pool
Decorator
Proxy
Adaptor
Composite
Bridge
Facade
Flyweight
Observer
Visitor
Iterator
Strategy
Command
Mediator
Memento
State
Chain of Responsibility
Pattern Name: Factory
Pattern Type: Creative
When implementing a Design Pattern in Python it is important to be able to pick the right Pattern for the right use. Knowing your Design Pattern categories will help in this decision-making process.
Problem: You are unsure of what type of objects you need or what classes you will be using.
So, in this hypothetical scenario let's say that you own a computer store and have only been selling one type of monitor. The monitor you are already selling is a small monitor. You now want to add large monitors to your inventory to sell.
Solution: This Pattern is good for creating new objects. It also has wide applicability in many areas of programming problems involving interfaces or class instantiation. This can be great for expanding your class instantiations at scale and keep your code quick, simple, and organized.
# First Class for Small Monitorclass SmallMonitor():    def __init__(self, brand):        self._brand = brand    def details(self):        details = "24in x 24in 1080p | $87.00"        return details# Second Class for Large Monitorclass LargeMonitor():    def __init__(self, brand):        self._brand = brand    def details(self):        details = "32in x 32in 1080p | $115.00"        return details# Basic Creative Design Pattern Implementationdef get_monitor(monitor = 'small_monitor'):    """factory method"""    monitors = dict(small_monitor = SmallMonitor("ASUS"),                    large_monitor = LargeMonitor("HP"))    ruturn monitors[monitor]small_monitor = get_monitor("small_monitor").details()large_monitor = get_monitor("large_monitor").details()print(small_monitor)print(large_monitor)[out]24in x 24in 1080p | $87.0032in x 32in 1080p | $115.00
Participants: The participants would be the SmallMonitor() and LargeMonitor() classes. Also, the small_monitor and large_monitor objects would be included, as well.
Consequences: The pattern has positive consequences when applied to class instantiation at scale but can sometimes be confusing or not necessary for more simple instantiations. In some cases, it can limit the code available. This is also in many applications not recommended in Python.
Design Patterns support consistent coding among the community, as well as individually. They help maintain a healthy programming ecosystem that has well known solutions to common problems. Using Design Patterns can sometimes make things worse though and should be integrated into your coding regimen using logic, reason, and proper implementation. Sometimes you have the wrong Design Pattern for the problem at hand and sometimes not having one or creating your own is the best idea. No matter what though, the knowledge of Design Patterns will do nothing but make you a better programmer. In many cases they will improve your code and efficiency by a noticeable amount. I hope this helps anyone wanting to know more about the basics of Design Patterns in Python. Thank you and happy coding!