python-design-pattern-rag

The Three-Tier Pattern (Structural)

Purpose

The Three-Tier pattern divides an application into three distinct layers: data, logic, and presentation. This structure helps keep code organized, easier to maintain, and more scalable as the application grows.

Problem It Solves

In many applications, data handling, business logic, and user interfaces are mixed together. This can make the code hard to manage, test, or update. The Three-Tier pattern solves this by assigning each responsibility to its own layer, allowing you to change one part without affecting the others.

When to Use It

Use this pattern when:

When Not to Use It

Avoid this pattern if:

How It Works

The application is divided into three layers:

  1. Data Layer – Manages data storage and retrieval (e.g., databases or files).
  2. Logic Layer – Handles rules, processing, and decision-making.
  3. Presentation Layer – Manages how users interact with the app (e.g., UI or command-line interface).

Each layer communicates only with its neighboring layer, promoting clean separation of responsibilities.

Real-World Analogy

Think of a restaurant:

Each part does its job without needing full knowledge of the others.

Simplified Example

Here’s a basic example in Python:

# Data Layer
_data = []

def save(item):
    _data.append(item)

def fetch():
    return list(_data)

# Logic Layer
def add(item):
    if item:
        save(item)
    else:
        raise ValueError("Item cannot be empty.")

def get_all():
    return fetch()

# Presentation Layer
while True:
    print("\n=== Menu ===")
    print("1. Add Item")
    print("2. View Items")
    choice = input("Choose an option: ")

    if choice == "1":
        item = input("Enter the item: ")
        try:
            add(item)
            print("Item added.")
        except ValueError as e:
            print(f"Error: {e}")
    elif choice == "2":
        items = get_all()
        if not items:
            print("No items found.")
        else:
            for i, item in enumerate(items, 1):
                print(f"{i}. {item}")

This example separates storage, processing, and user interaction into three simple layers, each with a single responsibility.

Learn More

View the full implementation in Python here: Three-Tier Pattern on GitHub