Skip to content

Commit 339db2a

Browse files
committed
Merge branch 'master' of https://github.com/themoffster/java-design-patterns into themoffster-master
Conflicts: dao/pom.xml
2 parents 9cbc4a2 + d0af12b commit 339db2a

File tree

11 files changed

+349
-158
lines changed

11 files changed

+349
-158
lines changed

abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class AppTest {
99

10-
private App app = new App();;
10+
private App app = new App();
1111
private KingdomFactory elfFactory;
1212
private KingdomFactory orcFactory;
1313

dao/pom.xml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?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.7.0</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>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.iluwatar</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.7.0</version>
10+
</parent>
11+
<artifactId>dao</artifactId>
12+
13+
<properties>
14+
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>log4j</groupId>
25+
<artifactId>log4j</artifactId>
26+
</dependency>
27+
</dependencies>
1828
</project>

dao/src/main/java/com/iluwatar/dao/App.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.apache.log4j.Logger;
7+
68
/**
79
*
810
* Data Access Object (DAO) is an object that provides an abstract interface to some type of database or other
@@ -17,43 +19,38 @@
1719
*/
1820
public class App {
1921

22+
private static Logger LOGGER = Logger.getLogger(App.class);
23+
2024
/**
21-
* Program entry point
22-
* @param args command line args
25+
* Program entry point.
26+
*
27+
* @param args command line args.
2328
*/
24-
public static void main(String[] args) {
25-
26-
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
27-
28-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
29-
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
30-
31-
Customer customer = new Customer(4, "Dan", "Danson");
29+
public static void main(final String[] args) {
30+
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
31+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
32+
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
33+
final Customer customer = new Customer(4, "Dan", "Danson");
3234
customerDao.addCustomer(customer);
33-
34-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
35-
35+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
3636
customer.setFirstName("Daniel");
3737
customer.setLastName("Danielson");
3838
customerDao.updateCustomer(customer);
39-
40-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
41-
39+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
4240
customerDao.deleteCustomer(customer);
43-
44-
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
41+
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
4542
}
4643

4744
/**
48-
* Generate customers
49-
* @return list of customers
45+
* Generate customers.
46+
*
47+
* @return list of customers.
5048
*/
5149
public static List<Customer> generateSampleCustomers() {
52-
Customer customer1 = new Customer(1, "Adam", "Adamson");
53-
Customer customer2 = new Customer(2, "Bob", "Bobson");
54-
Customer customer3 = new Customer(3, "Carl", "Carlson");
55-
56-
List<Customer> customers = new ArrayList<Customer>();
50+
final Customer customer1 = new Customer(1, "Adam", "Adamson");
51+
final Customer customer2 = new Customer(2, "Bob", "Bobson");
52+
final Customer customer3 = new Customer(3, "Carl", "Carlson");
53+
final List<Customer> customers = new ArrayList<Customer>();
5754
customers.add(customer1);
5855
customers.add(customer2);
5956
customers.add(customer3);

dao/src/main/java/com/iluwatar/dao/Customer.java

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,63 @@
66
*
77
*/
88
public class Customer {
9-
10-
private int id;
11-
private String firstName;
12-
private String lastName;
139

14-
public Customer(int id, String firstName, String lastName) {
15-
this.id = id;
16-
this.firstName = firstName;
17-
this.lastName = lastName;
18-
}
10+
private int id;
11+
private String firstName;
12+
private String lastName;
1913

20-
public int getId() {
21-
return id;
22-
}
14+
public Customer(final int id, final String firstName, final String lastName) {
15+
this.id = id;
16+
this.firstName = firstName;
17+
this.lastName = lastName;
18+
}
2319

24-
public void setId(int id) {
25-
this.id = id;
26-
}
20+
public int getId() {
21+
return id;
22+
}
2723

28-
public String getFirstName() {
29-
return firstName;
30-
}
24+
public void setId(final int id) {
25+
this.id = id;
26+
}
3127

32-
public void setFirstName(String firstName) {
33-
this.firstName = firstName;
34-
}
28+
public String getFirstName() {
29+
return firstName;
30+
}
3531

36-
public String getLastName() {
37-
return lastName;
38-
}
32+
public void setFirstName(final String firstName) {
33+
this.firstName = firstName;
34+
}
3935

40-
public void setLastName(String lastName) {
41-
this.lastName = lastName;
42-
}
43-
44-
@Override
45-
public String toString() {
46-
return "Customer{" +
47-
"id=" + id +
48-
", firstName='" + firstName + '\'' +
49-
", lastName='" + lastName + '\'' +
50-
'}';
51-
}
36+
public String getLastName() {
37+
return lastName;
38+
}
5239

53-
@Override
54-
public boolean equals(Object o) {
55-
if (this == o) return true;
56-
if (o == null || getClass() != o.getClass()) return false;
40+
public void setLastName(final String lastName) {
41+
this.lastName = lastName;
42+
}
5743

58-
Customer customer = (Customer) o;
44+
@Override
45+
public String toString() {
46+
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
47+
+ getLastName() + '\'' + '}';
48+
}
5949

60-
if (id != customer.id) return false;
61-
62-
return true;
50+
@Override
51+
public boolean equals(final Object o) {
52+
boolean isEqual = false;
53+
if (this == o) {
54+
isEqual = true;
55+
} else if (o != null && (getClass() == o.getClass())) {
56+
final Customer customer = (Customer) o;
57+
if (getId() == customer.getId())
58+
isEqual = true;
6359
}
60+
return isEqual;
61+
}
6462

65-
@Override
66-
public int hashCode() {
67-
int result = id;
68-
return result;
69-
}
70-
}
63+
@Override
64+
public int hashCode() {
65+
int result = getId();
66+
return result;
67+
}
68+
}

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
*
99
*/
1010
public interface CustomerDao {
11-
12-
public List<Customer> getAllCustomers();
13-
public Customer getCusterById(int id);
14-
public void addCustomer(Customer customer);
15-
public void updateCustomer(Customer customer);
16-
public void deleteCustomer(Customer customer);
17-
}
11+
12+
List<Customer> getAllCustomers();
13+
14+
Customer getCustomerById(int id);
15+
16+
void addCustomer(Customer customer);
17+
18+
void updateCustomer(Customer customer);
19+
20+
void deleteCustomer(Customer customer);
21+
}

dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,59 @@
44

55
/**
66
*
7-
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
8-
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
9-
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
10-
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
11-
* database schema, etc.
7+
* The data access object (DAO) is an object that provides an abstract interface to some type of
8+
* database or other persistence mechanism. By mapping application calls to the persistence layer,
9+
* DAO provide some specific data operations without exposing details of the database. This
10+
* isolation supports the Single responsibility principle. It separates what data accesses the
11+
* application needs, in terms of domain-specific objects and data types (the public interface of
12+
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
1213
*
1314
*/
1415
public class CustomerDaoImpl implements CustomerDao {
1516

16-
// Represents the DB structure for our example so we don't have to managed it ourselves
17-
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
18-
private List<Customer> customers;
19-
20-
public CustomerDaoImpl(List<Customer> customers) {
21-
this.customers = customers;
22-
}
23-
24-
@Override
25-
public List<Customer> getAllCustomers() {
26-
return customers;
17+
// Represents the DB structure for our example so we don't have to managed it ourselves
18+
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
19+
private List<Customer> customers;
20+
21+
public CustomerDaoImpl(final List<Customer> customers) {
22+
this.customers = customers;
23+
}
24+
25+
@Override
26+
public List<Customer> getAllCustomers() {
27+
return customers;
28+
}
29+
30+
@Override
31+
public Customer getCustomerById(final int id) {
32+
Customer customer = null;
33+
for (final Customer cus : getAllCustomers()) {
34+
if (cus.getId() == id) {
35+
customer = cus;
36+
break;
37+
}
2738
}
39+
return customer;
40+
}
2841

29-
@Override
30-
public Customer getCusterById(int id) {
31-
for (int i = 0; i < customers.size(); i++) {
32-
if (customers.get(i).getId() == id) {
33-
return customers.get(i);
34-
}
35-
}
36-
// No customer found
37-
return null;
38-
}
39-
40-
@Override
41-
public void addCustomer(Customer customer) {
42-
customers.add(customer);
42+
@Override
43+
public void addCustomer(final Customer customer) {
44+
if (getCustomerById(customer.getId()) == null) {
45+
customers.add(customer);
4346
}
47+
}
4448

4549

46-
@Override
47-
public void updateCustomer(Customer customer) {
48-
if (customers.contains(customer)) {
49-
customers.set(customers.indexOf(customer), customer);
50-
}
50+
@Override
51+
public void updateCustomer(final Customer customer) {
52+
if (getAllCustomers().contains(customer)) {
53+
final int index = getAllCustomers().indexOf(customer);
54+
getAllCustomers().set(index, customer);
5155
}
56+
}
5257

53-
@Override
54-
public void deleteCustomer(Customer customer) {
55-
customers.remove(customer);
56-
}
57-
}
58+
@Override
59+
public void deleteCustomer(final Customer customer) {
60+
getAllCustomers().remove(customer);
61+
}
62+
}

dao/src/test/java/com/iluwatar/dao/AppTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)