Merge pull request #47 from mgiovenco/master

Added DAO implementation, modified readme, and added UML diagram
This commit is contained in:
Ilkka Seppälä 2015-04-15 23:15:07 +03:00
commit fa931bdcfa
10 changed files with 272 additions and 0 deletions

View File

@ -65,6 +65,12 @@ Presentation Tier patterns are the top-most level of the application, this is co
* [Model-View-Presenter](#model-view-presenter)
### Architectual Patterns
An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context.
* [Data Access Object](#dao)
### Idioms
A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task, algorithm, or data structure that is not a built-in feature in the programming language being used, or, conversely, the use of an unusual or notable feature that is built into a programming language. What distinguishes idioms from patterns is generally the size, the idioms tend to be something small while the patterns are larger.
@ -403,6 +409,15 @@ A programming idiom is a means of expressing a recurring construct in one or mor
* when you want to improve the "Separation of Concerns" principle in presentation logic
* when a user interface development and testing is necessary.
## <a name="dao">Data Access Object</a> [&#8593;](#list-of-design-patterns)
**Intent:** Object provides an abstract interface to some type of database or other persistence mechanism.
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/dao/etc/dao.png "Data Access Object")
**Applicability:** Use the Data Access Object in any of the following situations
* when you want to consolidate how the data layer is accessed
* when you want to avoid writing multiple data retrieval/persistence layers
## <a name="double-checked-locking">Double Checked Locking</a> [&#8593;](#list-of-design-patterns)
**Intent:** Reduce the overhead of acquiring a lock by first testing the locking criterion (the "lock hint") without actually acquiring the lock. Only if the locking criterion check indicates that locking is required does the actual locking logic proceed.

BIN
dao/etc/dao.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

49
dao/etc/dao.ucls Normal file
View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.Customer" project="dao"
file="/dao/src/main/java/com/iluwatar/Customer.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="176" y="337"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.CustomerDaoImpl" project="dao"
file="/dao/src/main/java/com/iluwatar/CustomerDaoImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="540" y="334"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="3" language="java" name="com.iluwatar.CustomerDao" project="dao"
file="/dao/src/main/java/com/iluwatar/CustomerDao.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="536" y="131"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<realization id="4">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="3"/>
</realization>
<association id="5">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="6" name="customers"/>
<multiplicity id="7" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

18
dao/pom.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dao</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,47 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
/**
*
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
* interacting with the data directly. The below example demonstrates basic operations: select, add, update, and delete.
*/
public class App {
public static void main(String[] args) {
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
Customer customer = new Customer(4, "Dan", "Danson");
customerDao.addCustomer(customer);
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
customer.setFirstName("Daniel");
customer.setLastName("Danielson");
customerDao.updateCustomer(customer);
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
customerDao.deleteCustomer(customer);
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
}
public static List<Customer> generateSampleCustomers() {
Customer customer1 = new Customer(1, "Adam", "Adamson");
Customer customer2 = new Customer(2, "Bob", "Bobson");
Customer customer3 = new Customer(3, "Carl", "Carlson");
List<Customer> customers = new ArrayList<Customer>();
customers.add(customer1);
customers.add(customer2);
customers.add(customer3);
return customers;
}
}

View File

@ -0,0 +1,64 @@
package com.iluwatar;
public class Customer {
private int id;
private String firstName;
private String lastName;
public Customer(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
if (id != customer.id) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
return result;
}
}

View File

@ -0,0 +1,11 @@
package com.iluwatar;
import java.util.List;
public interface CustomerDao {
public List<Customer> getAllCustomers();
public Customer getCusterById(int id);
public void addCustomer(Customer customer);
public void updateCustomer(Customer customer);
public void deleteCustomer(Customer customer);
}

View File

@ -0,0 +1,55 @@
package com.iluwatar;
import java.util.List;
/**
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
* database schema, etc.
*/
public class CustomerDaoImpl implements CustomerDao {
// Represents the DB structure for our example so we don't have to managed it ourselves
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
private List<Customer> customers;
public CustomerDaoImpl(List<Customer> customers) {
this.customers = customers;
}
@Override
public List<Customer> getAllCustomers() {
return customers;
}
@Override
public Customer getCusterById(int id) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getId() == id) {
return customers.get(i);
}
}
// No customer found
return null;
}
@Override
public void addCustomer(Customer customer) {
customers.add(customer);
}
@Override
public void updateCustomer(Customer customer) {
if (customers.contains(customer)) {
customers.set(customers.indexOf(customer), customer);
}
}
@Override
public void deleteCustomer(Customer customer) {
customers.remove(customer);
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar;
import org.junit.Test;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -22,6 +22,7 @@
<module>adapter</module>
<module>bridge</module>
<module>composite</module>
<module>dao</module>
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>