Design Patterns
https://github.com/ashishps1/awesome-low-level-design
Creational
How to create objects.
Singleton
Method for creating a single instance of a class.
(in python, __new__ created the object, __init__ initializes)
# Straightforward implementation of the Singleton Pattern
class Logger(object):
_instance = None
def __new__(cls):
if cls._instance is None:
print('Creating the object')
cls._instance = super(Logger, cls).__new__(cls)
# Put any initialization here.
return cls._instance
print(Logger())
print(Logger())In java, more intuitive:
Builder Pattern
With complex objects, instead of complex constructor, "build" the object step-by-step:
In python, just used named parameters (not builder, but conventional).
There is a more complex pattern with the director and more boilerplate , but in practice it is not frequently used outside of textbook examples.
Factory Pattern
Used for hiding the complexities of object creation. Use a simpler interface and the complex part is behind the scenes.
No factory
Factory:
This gives us a cleaner "interface"
Structural
How objects relate to eachother.
Facade
"Fancy way to encapsulate"
Hides all the nasty behaviour.
Maybe you have a bunch of complex subsystems that are required for a payment processor. Fraud detection, inventory, shipping calculator etc. Rather than exposing all this logic and having a bunch of if statements, just provide your single facade that is for making an order with the payment processor. It hides all the details required in the background.
Adapter
Provide a standard interface, and then wrap other implementations such as legacy implementations by implementing the interface and holding a dependency to a concrete type that is the other implementation.
EG, Weather app expects celsius, third party API is in F.
We implement a standard interface and hide the 'adapting'
Behavioral
Behavioral patterns are all about efficient communication and the delegation of responsibilities among objects. They help in defining not just how objects are composed, but also how they communicate within the application's architecture.
Strategy
And there's multiple ways to do something, you can dynamically change strategies. Define an interface for a strategy, and then implement it for each strategy. This is in contrast with just a bunch of different if statements inside a single class based on the strategy in use without the pattern.
You can dynamically change the strategy just by setting the strategy.
Observer
"Observe" events happening to other objects.
In this way you can subscribe to events that are happening to another object, and be notified when the event occurs.
Last updated