Python Language Design Patterns Proxy


Proxy object is often used to ensure guarded access to another object, which internal business logic we don't want to pollute with safety requirements.

Suppose we'd like to guarantee that only user of specific permissions can access resource.

Proxy definition: (it ensure that only users which actually can see reservations will be able to consumer reservation_service)

from datetime import date
from operator import attrgetter

class Proxy:
    def __init__(self, current_user, reservation_service):
        self.current_user = current_user
        self.reservation_service = reservation_service

    def highest_total_price_reservations(self, date_from, date_to, reservations_count):
        if self.current_user.can_see_reservations:
            return self.reservation_service.highest_total_price_reservations(
            return []

#Models and ReservationService:

class Reservation:
    def __init__(self, date, total_price): = date
        self.total_price = total_price

class ReservationService:
    def highest_total_price_reservations(self, date_from, date_to, reservations_count):
        # normally it would be read from database/external service
        reservations = [
            Reservation(date(2014, 5, 15), 100),
            Reservation(date(2017, 5, 15), 10),
            Reservation(date(2017, 1, 15), 50)

        filtered_reservations = [r for r in reservations if (date_from <= <= date_to)]

        sorted_reservations = sorted(filtered_reservations, key=attrgetter('total_price'), reverse=True)

        return sorted_reservations[0:reservations_count]

class User:
    def __init__(self, can_see_reservations, name):
        self.can_see_reservations = can_see_reservations = name

#Consumer service:

class StatsService:
    def __init__(self, reservation_service):
        self.reservation_service = reservation_service

    def year_top_100_reservations_average_total_price(self, year):
        reservations = self.reservation_service.highest_total_price_reservations(
            date(year, 1, 1),
            date(year, 12, 31),

        if len(reservations) > 0:
            total = sum(r.total_price for r in reservations)

            return total / len(reservations)
            return 0

def test(user, year):
    reservations_service = Proxy(user, ReservationService())
    stats_service = StatsService(reservations_service)
    average_price = stats_service.year_top_100_reservations_average_total_price(year)
    print("{0} will see: {1}".format(, average_price))

test(User(True, "John the Admin"), 2017)
test(User(False, "Guest"),         2017)

  • we're avoiding any changes in ReservationService when access restrictions are changed.
  • we're not mixing business related data (date_from, date_to, reservations_count) with domain unrelated concepts (user permissions) in service.
  • Consumer (StatsService) is free from permissions related logic as well

  • Proxy interface is always exactly the same as the object it hides, so that user that consumes service wrapped by proxy wasn't even aware of proxy presence.