Skip to content

Commit 58da95b

Browse files
author
Mike Giovenco
committed
Added DAO implementation, modified readme, and added UML diagram
1 parent 3356680 commit 58da95b

File tree

10 files changed

+272
-0
lines changed

10 files changed

+272
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ Presentation Tier patterns are the top-most level of the application, this is co
6464

6565
* [Model-View-Presenter](#model-view-presenter)
6666

67+
### Architectual Patterns
68+
69+
An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context.
70+
71+
* [Data Access Object](#dao)
72+
6773
### Idioms
6874

6975
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.
@@ -402,6 +408,15 @@ A programming idiom is a means of expressing a recurring construct in one or mor
402408
* when you want to improve the "Separation of Concerns" principle in presentation logic
403409
* when a user interface development and testing is necessary.
404410

411+
## <a name="dao">Data Access Object</a> [&#8593;](#list-of-design-patterns)
412+
**Intent:** Object provides an abstract interface to some type of database or other persistence mechanism.
413+
414+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/dao/etc/dao.png "Data Access Object")
415+
416+
**Applicability:** Use the Data Access Object in any of the following situations
417+
* when you want to consolidate how the data layer is accessed
418+
* when you want to avoid writing multiple data retrieval/persistence layers
419+
405420
## <a name="double-checked-locking">Double Checked Locking</a> [&#8593;](#list-of-design-patterns)
406421
**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.
407422

dao/etc/dao.png

210 KB
Loading

dao/etc/dao.ucls

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.Customer" project="dao"
5+
file="/dao/src/main/java/com/iluwatar/Customer.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="176" y="337"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.CustomerDaoImpl" project="dao"
14+
file="/dao/src/main/java/com/iluwatar/CustomerDaoImpl.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="540" y="334"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<interface id="3" language="java" name="com.iluwatar.CustomerDao" project="dao"
23+
file="/dao/src/main/java/com/iluwatar/CustomerDao.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="536" y="131"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</interface>
31+
<realization id="4">
32+
<end type="SOURCE" refId="2"/>
33+
<end type="TARGET" refId="3"/>
34+
</realization>
35+
<association id="5">
36+
<end type="SOURCE" refId="2" navigable="false">
37+
<attribute id="6" name="customers"/>
38+
<multiplicity id="7" minimum="0" maximum="2147483647"/>
39+
</end>
40+
<end type="TARGET" refId="1" navigable="true"/>
41+
<display labels="true" multiplicity="true"/>
42+
</association>
43+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44+
sort-features="false" accessors="true" visibility="true">
45+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46+
<operations public="true" package="true" protected="true" private="true" static="true"/>
47+
</classifier-display>
48+
<association-display labels="true" multiplicity="true"/>
49+
</class-diagram>

dao/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>dao</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.iluwatar;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
9+
* interacting with the data directly. The below example demonstrates basic operations: select, add, update, and delete.
10+
*/
11+
public class App {
12+
13+
public static void main(String[] args) {
14+
15+
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
16+
17+
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
18+
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
19+
20+
Customer customer = new Customer(4, "Dan", "Danson");
21+
customerDao.addCustomer(customer);
22+
23+
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
24+
25+
customer.setFirstName("Daniel");
26+
customer.setLastName("Danielson");
27+
customerDao.updateCustomer(customer);
28+
29+
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
30+
31+
customerDao.deleteCustomer(customer);
32+
33+
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
34+
}
35+
36+
public static List<Customer> generateSampleCustomers() {
37+
Customer customer1 = new Customer(1, "Adam", "Adamson");
38+
Customer customer2 = new Customer(2, "Bob", "Bobson");
39+
Customer customer3 = new Customer(3, "Carl", "Carlson");
40+
41+
List<Customer> customers = new ArrayList<Customer>();
42+
customers.add(customer1);
43+
customers.add(customer2);
44+
customers.add(customer3);
45+
return customers;
46+
}
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.iluwatar;
2+
3+
public class Customer {
4+
private int id;
5+
private String firstName;
6+
private String lastName;
7+
8+
public Customer(int id, String firstName, String lastName) {
9+
this.id = id;
10+
this.firstName = firstName;
11+
this.lastName = lastName;
12+
}
13+
14+
public int getId() {
15+
return id;
16+
}
17+
18+
public void setId(int id) {
19+
this.id = id;
20+
}
21+
22+
public String getFirstName() {
23+
return firstName;
24+
}
25+
26+
public void setFirstName(String firstName) {
27+
this.firstName = firstName;
28+
}
29+
30+
public String getLastName() {
31+
return lastName;
32+
}
33+
34+
public void setLastName(String lastName) {
35+
this.lastName = lastName;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "Customer{" +
41+
"id=" + id +
42+
", firstName='" + firstName + '\'' +
43+
", lastName='" + lastName + '\'' +
44+
'}';
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass()) return false;
51+
52+
Customer customer = (Customer) o;
53+
54+
if (id != customer.id) return false;
55+
56+
return true;
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
int result = id;
62+
return result;
63+
}
64+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
public interface CustomerDao {
6+
public List<Customer> getAllCustomers();
7+
public Customer getCusterById(int id);
8+
public void addCustomer(Customer customer);
9+
public void updateCustomer(Customer customer);
10+
public void deleteCustomer(Customer customer);
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
/**
6+
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
7+
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
8+
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
9+
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
10+
* database schema, etc.
11+
*/
12+
public class CustomerDaoImpl implements CustomerDao {
13+
14+
// Represents the DB structure for our example so we don't have to managed it ourselves
15+
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
16+
private List<Customer> customers;
17+
18+
public CustomerDaoImpl(List<Customer> customers) {
19+
this.customers = customers;
20+
}
21+
22+
@Override
23+
public List<Customer> getAllCustomers() {
24+
return customers;
25+
}
26+
27+
@Override
28+
public Customer getCusterById(int id) {
29+
for (int i = 0; i < customers.size(); i++) {
30+
if (customers.get(i).getId() == id) {
31+
return customers.get(i);
32+
}
33+
}
34+
// No customer found
35+
return null;
36+
}
37+
38+
@Override
39+
public void addCustomer(Customer customer) {
40+
customers.add(customer);
41+
}
42+
43+
44+
@Override
45+
public void updateCustomer(Customer customer) {
46+
if (customers.contains(customer)) {
47+
customers.set(customers.indexOf(customer), customer);
48+
}
49+
}
50+
51+
@Override
52+
public void deleteCustomer(Customer customer) {
53+
customers.remove(customer);
54+
}
55+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
import org.junit.Test;
4+
5+
public class AppTest {
6+
7+
@Test
8+
public void test() {
9+
String[] args = {};
10+
App.main(args);
11+
}
12+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<module>adapter</module>
2020
<module>bridge</module>
2121
<module>composite</module>
22+
<module>dao</module>
2223
<module>decorator</module>
2324
<module>facade</module>
2425
<module>flyweight</module>

0 commit comments

Comments
 (0)