Published on

Design Patterns: Repository

Authors
  • avatar
    Name
    Loi Tran
    Twitter

Introduction

from abc import ABC, abstractmethod

# Abstract Repository
class Repository(ABC):
    @abstractmethod
    def add(self, item):
        pass

    @abstractmethod
    def get(self, item_id):
        pass

# Concrete Repository
class InMemoryRepository(Repository):
    def __init__(self):
        self._data = {}

    def add(self, item):
        self._data[item["id"]] = item

    def get(self, item_id):
        return self._data.get(item_id)

# Usage
repo = InMemoryRepository()
repo.add({"id": 1, "name": "Item 1"})
print(repo.get(1))  # Output: {'id': 1, 'name': 'Item 1'}
// Abstract Repository is not enforced in JS, but we define a concrete repository
class InMemoryRepository {
    constructor() {
        this.data = new Map();
    }

    add(item) {
        this.data.set(item.id, item);
    }

    get(itemId) {
        return this.data.get(itemId);
    }
}

// Usage
const repo = new InMemoryRepository();
repo.add({ id: 1, name: "Item 1" });
console.log(repo.get(1)); // Output: { id: 1, name: "Item 1" }
// Abstract Repository
abstract class Repository<T> {
  void add(T item);
  T? get(int id);
}

// Concrete Repository
class InMemoryRepository<T> implements Repository<T> {
  final Map<int, T> _data = {};

  
  void add(T item) {
    if (item is Map && item.containsKey("id")) {
      _data[item["id"]] = item;
    }
  }

  
  T? get(int id) {
    return _data[id];
  }
}

// Usage
void main() {
  final repo = InMemoryRepository<Map<String, dynamic>>();
  repo.add({"id": 1, "name": "Item 1"});
  print(repo.get(1)); // Output: {id: 1, name: Item 1}
}
# Abstract Repository is not enforced in Ruby, but we define a concrete repository
class InMemoryRepository
    def initialize
        @data = {}
    end

    def add(item)
        @data[item[:id]] = item
    end

    def get(item_id)
        @data[item_id]
    end
end

# Usage
repo = InMemoryRepository.new
repo.add({ id: 1, name: "Item 1" })
puts repo.get(1) # Output: {:id=>1, :name=>"Item 1"}