Design Patterns

Examples provided by ChatGPT.

Design Patterns Examples

Here are some examples of design patterns in Python:

Factory Pattern

class Shape:
    def draw(self):
        pass
    
class Circle(Shape):
    def draw(self):
        print("Drawing a circle")
    
class Rectangle(Shape):
    def draw(self):
        print("Drawing a rectangle")

class ShapeFactory:
    def get_shape(self, shape_type: str):
        if shape_type == 'circle':
            return Circle()
        elif shape_type == 'rectangle':
            return Rectangle()
        else:
            raise ValueError('Invalid shape type')

In the above example, ShapeFactory creates objects of Circle or Rectangle based on the input parameter shape_type.

Observer Pattern

The Observer pattern is used to establish a one-to-many dependency between objects. When the state of one object changes, all its dependents are notified and updated automatically.

In the above example, Subject maintains a list of Observer objects and notifies them when its...

In the above example, Subject is a class that maintains a list of Observer objects and notifies them when its state changes. attach and detach methods are used to add and remove observers from the list. notify method iterates over the list of observers and calls their update method with the current state. set_state method sets the current state of the subject and calls notify to update all observers.

Observer is an abstract class that defines the interface for objects that should be notified when the subject changes. ConcreteObserverA and ConcreteObserverB inherit from Observer and implement the update method to define their own behavior when notified.

Here is an example of how to use the Observer pattern:

In the above example, we create a Subject object and two observers, ConcreteObserverA and ConcreteObserverB. We attach both observers to the subject and then change its state using set_state. Both observers are notified and print the new state. We then detach ConcreteObserverB from the subject and change its state again. Only ConcreteObserverA is notified this time.

Singleton Pattern

In the above example, Singleton is a class that can only have one instance. The __new__ method ensures that if an instance of Singleton already exists, it will return that instance instead of creating a new one.

Decorator Pattern

In the above example, Component is an interface that defines the base functionality of an object. ConcreteComponent implements the interface. Decorator is an abstract class that implements the same interface and holds a reference to a Component. ConcreteDecoratorA and ConcreteDecoratorB inherit from Decorator and add their own functionality to the base operation.

Strategy Pattern

In the above example, Strategy is an interface that defines the algorithm that will be used by the Context object. ConcreteStrategyA and ConcreteStrategyB implement the interface with different algorithms. Context holds a reference to a Strategy object and executes its algorithm using execute_strategy.

Template Method Pattern

In the above example, AbstractClass is an abstract class that defines a template method that calls two abstract methods, operation1 and operation2. ConcreteClassA and ConcreteClassB inherit from AbstractClass and implement the abstract methods to define their own behavior.

Iterator Pattern

Last updated

Was this helpful?