Design Patterns

DataSource - Model - Repository Pattern


Most likely a database but may be a flat file as follows in employees.xml


Model Represents a single entity in memory

class Employee {
    int id;
    String name;

DataSource Responsible for reading from / writing to underlying physical data.

class DataSource {
    Map<String, String> configuration; // username, password etc..
    String path; // filepath, jdbcurl etc..
    InputStream query(String query); // SELECT FROM etc..


“A Repository represents all objects of a certain type as a conceptual set. It acts like a collection, except with more elaborate querying capability.”

Eric Evans, Domain Driven Design

class EmployeeRepository {
   DataSource ds;
   Employee byId(int id);
   Employee[] all();