Design Patterns

DataSource - Model - Repository Pattern

Data

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

<employees>
    <employee>
        <id>1</id>
        <name>Koray</name>
    </employee>
    <employee>
        <id>2</id>
        <name>Deniz</name>
    </employee>
    <employee>
        <id>3</id>
        <name>Toprak</name>    
    </employee>
</employees>

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..
}

Repository

“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(); 
}

🏠