- The Singleton pattern is a way to guarantee that a class has only one instance throughout your entire application
- Factory Method pattern creates objects without specifying exact class.
- Abstract Factory Create families of related objects.
- Builder Construct complex objects step-by-step.
- Prototype Clone objects without depending on their class.
- Adapter Make incompatible interfaces work together.
- Decorator Add responsibilities dynamically.
- Composite Treat groups and individuals the same way.
- Strategy Select an algorithm at runtime.
- Observer Notify multiple objects when one changes
- Command Encapsulate requests as objects.
- Template Method Skeleton algorithm in base class; steps defined in subclasses.
in real world example, such as Apache Kafka written in java, multiple patterns are appliied

More examples

In Python, abstract classes and methods are tools provided by the abc module (Abstract Base Classes) to help enforce a certain structure in your class hierarchies. They are particularly useful in defining interfaces or blueprints for derived classes.
Why Use Abstract Classes and Methods?
- Enforce Interface: They ensure that derived classes adhere to a specific contract or interface. This is crucial for maintaining consistency and predictability in a larger system.
- Code Organization: They help organize code by defining a clear hierarchy and the essential methods that each subclass must implement.
- Prevent Instantiation of Base Class: Sometimes, you don’t want the base class itself to be instantiated because it’s more of a template. Abstract classes prevent this.
from abc import ABC, abstractmethod
class Shape(ABC): # Inheriting from ABC makes this an abstract class
@abstractmethod
def area(self):
pass # No implementation in the abstract class
@abstractmethod
def perimeter(self):
pass # No implementation in the abstract class
def description(self):
return "This is a shape."
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
import math
return math.pi * self.radius**2
def perimeter(self):
import math
return 2 * math.pi * self.radius
# You cannot instantiate the abstract class directly:
# my_shape = Shape() # This will raise a TypeError
# You can instantiate the concrete subclasses:
my_rectangle = Rectangle(5, 10)
print(f"Rectangle Area: {my_rectangle.area()}")
print(f"Rectangle Perimeter: {my_rectangle.perimeter()}")
print(my_rectangle.description())
my_circle = Circle(7)
print(f"Circle Area: {my_circle.area()}")
print(f"Circle Perimeter: {my_circle.perimeter()}")
print(my_circle.description())
Here is a real world example
from abc import ABC, abstractmethod
class PaymentGateway(ABC):
@abstractmethod
def process_payment(self, amount: float, details: dict) -> bool:
"""Processes a payment."""
pass
@abstractmethod
def refund_payment(self, transaction_id: str, amount: float) -> bool:
"""Refunds a previous payment."""
pass
class StripeGateway(PaymentGateway):
def process_payment(self, amount: float, details: dict) -> bool:
print(f"Processing ${amount} via Stripe with details: {details}")
# Actual Stripe API call would go here
return True
def refund_payment(self, transaction_id: str, amount: float) -> bool:
print(f"Refunding ${amount} for transaction {transaction_id} via Stripe")
# Actual Stripe API call would go here
return True
class PayPalGateway(PaymentGateway):
def process_payment(self, amount: float, details: dict) -> bool:
print(f"Processing ${amount} via PayPal with details: {details}")
# Actual PayPal API call would go here
return True
def refund_payment(self, transaction_id: str, amount: float) -> bool:
print(f"Refunding ${amount} for transaction {transaction_id} via PayPal")
# Actual PayPal API call would go here
return True
def checkout(gateway: PaymentGateway, total_amount: float, payment_info: dict):
if gateway.process_payment(total_amount, payment_info):
print("Payment successful!")
else:
print("Payment failed.")
# In your application:
stripe_processor = StripeGateway()
paypal_processor = PayPalGateway()
checkout(stripe_processor, 100.00, {"card_number": "****-****-****-1234", "expiry": "12/25"})
checkout(paypal_processor, 50.00, {"email": "user@example.com"})