DEV Community

Enterprise Design Patterns: Practical Examples from Martin Fowler's Catalog

Enterprise applications handle complex business processes and data at scale. To manage this complexity, established design patterns provide reusable architectural solutions that promote maintainability, scalability, and clarity. Martin Fowler’s Patterns of Enterprise Application Architecture catalog organizes these solutions into categories that guide developers in building robust enterprise software.

This article introduces three fundamental Enterprise Design Patterns — Repository, Service Layer, and Model View Controller (MVC) — with real code examples in Python.

Repository Pattern
The Repository pattern mediates between the domain and data mapping layers, providing a clean API to access domain objects without exposing database details.

Example in Python

class Customer:
    def __init__(self, customer_id, name):
        self.customer_id = customer_id
        self.name = name

class CustomerRepository:
    def __init__(self):
        self.customers = {}

    def add(self, customer: Customer):
        self.customers[customer.customer_id] = customer

    def get_by_id(self, customer_id):
        return self.customers.get(customer_id)

repo = CustomerRepository()
repo.add(Customer(1, "Alice"))
print(repo.get_by_id(1))  # Customer object for Alice
Enter fullscreen mode Exit fullscreen mode

Service Layer Pattern
This pattern centralizes an application's business logic in a service layer, decoupling it from controllers or UI components, improving modularity and testability.

Example in Python

class OrderService:
    def __init__(self, customer_repo):
        self.customer_repo = customer_repo

    def place_order(self, customer_id, items):
        customer = self.customer_repo.get_by_id(customer_id)
        if not customer:
            raise ValueError("Customer not found")
        # Process order for the customer
        print(f"Order placed for {customer.name} with items: {items}")

customer_repo = CustomerRepository()
customer_repo.add(Customer(1, "Alice"))

order_service = OrderService(customer_repo)
order_service.place_order(1, ["item1", "item2"])
Enter fullscreen mode Exit fullscreen mode

Model View Controller (MVC) Pattern
MVC separates an application into three components: Model (business logic/data), View (UI), and Controller (handles input and updates).

Basic Python Console Example

class Model:
    def __init__(self):
        self.data = "Hello from Model"

class View:
    def display(self, data):
        print(f"View displaying: {data}")

class Controller:
    def __init__(self, model, view):
        self.model = model
        self.view = view

    def update_view(self):
        data = self.model.data
        self.view.display(data)

model = Model()
view = View()
controller = Controller(model, view)
controller.update_view()
Enter fullscreen mode Exit fullscreen mode

Final Remarks
These patterns provide powerful ways to architect scalable and maintainable enterprise applications. Applying them effectively can reduce code duplication, isolate concerns, and improve clarity, leading to higher-quality software.

Top comments (0)