Visitor with examples in Python
The Visitor design pattern is like a tool that lets us add new features to our code without changing the original code. Imagine we have a bunch of different types of files on our computer (like documents, music, and videos), and we want to perform various tasks on them, like opening or scanning for viruses. Instead of adding new code to each file type, we create a “visitor” – a separate set of instructions or tool – that can interact with each type of file to perform the task....
Template Method with examples in Python
The Template Method design pattern is a behavioral design pattern that defines the program skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. In simpler terms, it’s like a recipe with certain steps defined and fixed and allows the child classes to implement the specifics of these steps. Imagine the process of making a sandwich. The steps might include gathering ingredients, putting ingredients between bread, and serving the sandwich....
Strategy with examples in Python
The Strategy design pattern lets us change the way an object behaves while the program is running. Imagine having a video game character that can switch tools like a hammer, a sword, or a shield based on what we choose during the game. Similarly, by using the Strategy pattern, we can change how an object acts on the fly, without stopping or changing the rest of our program. Consider a navigation app that suggests routes to the destination....
State with examples in Python
This one can be bit tricky to understand first, but please read through the complete post, you’ll get it. The State design pattern is a method used in programming to allow an object to behave differently depending on its internal condition (refers to the current situation or status of an object, determined by its properties or attributes.) or “state”. This pattern involves creating different “state” objects for each possible condition, and a main object, called the “context”, which changes its behavior based on whichever state object is currently active....
Observer with examples in Python
The Observer design pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It’s commonly used to implement distributed event handling systems. A real-world analogy for the Observer pattern is a subscription to a newspaper or magazine. In this analogy, the newspaper is the subject, and the subscribers are the observers....