Skip to content

Commit 59b6b81

Browse files
committed
Test/Doc added
Test/Doc added
1 parent eb72493 commit 59b6b81

File tree

10 files changed

+475
-328
lines changed

10 files changed

+475
-328
lines changed

data-mapper/etc/data-mapper.png

52.8 KB
Loading

data-mapper/etc/data-mapper.ucls

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.datamapper.Student" project="java-design-patterns"
5+
file="/java-design-patterns/java/com/iluwatar/datamapper/Student.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="966" y="283"/>
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+
<interface id="2" language="java" name="com.iluwatar.datamapper.StudentDataMapper" project="java-design-patterns"
14+
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapper.java" binary="false"
15+
corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="749" y="278"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</interface>
23+
<class id="3" language="java" name="com.iluwatar.datamapper.StudentFirstDataMapper" project="java-design-patterns"
24+
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentFirstDataMapper.java" binary="false"
25+
corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="525" y="81"/>
27+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
28+
sort-features="false" accessors="true" visibility="true">
29+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
30+
<operations public="true" package="true" protected="true" private="true" static="true"/>
31+
</display>
32+
</class>
33+
<class id="4" language="java" name="com.iluwatar.datamapper.StudentSecondDataMapper" project="java-design-patterns"
34+
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentSecondDataMapper.java" binary="false"
35+
corner="BOTTOM_RIGHT">
36+
<position height="-1" width="-1" x="537" y="500"/>
37+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
38+
sort-features="false" accessors="true" visibility="true">
39+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
40+
<operations public="true" package="true" protected="true" private="true" static="true"/>
41+
</display>
42+
</class>
43+
<class id="5" language="java" name="com.iluwatar.datamapper.App" project="java-design-patterns"
44+
file="/java-design-patterns/java/com/iluwatar/datamapper/App.java" binary="false" corner="BOTTOM_RIGHT">
45+
<position height="-1" width="-1" x="485" y="285"/>
46+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
47+
sort-features="false" accessors="true" visibility="true">
48+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
49+
<operations public="true" package="true" protected="true" private="true" static="true"/>
50+
</display>
51+
</class>
52+
<association id="6">
53+
<end type="SOURCE" refId="4" navigable="false">
54+
<attribute id="7" name="students"/>
55+
<multiplicity id="8" minimum="0" maximum="2147483647"/>
56+
</end>
57+
<end type="TARGET" refId="1" navigable="true"/>
58+
<display labels="true" multiplicity="true"/>
59+
</association>
60+
<association id="9">
61+
<end type="SOURCE" refId="3" navigable="false">
62+
<attribute id="10" name="students"/>
63+
<multiplicity id="11" minimum="0" maximum="2147483647"/>
64+
</end>
65+
<end type="TARGET" refId="1" navigable="true"/>
66+
<display labels="true" multiplicity="true"/>
67+
</association>
68+
<dependency id="12">
69+
<end type="SOURCE" refId="5"/>
70+
<end type="TARGET" refId="1"/>
71+
</dependency>
72+
<realization id="13">
73+
<end type="SOURCE" refId="3"/>
74+
<end type="TARGET" refId="2"/>
75+
</realization>
76+
<realization id="14">
77+
<end type="SOURCE" refId="4"/>
78+
<end type="TARGET" refId="2"/>
79+
</realization>
80+
<dependency id="15">
81+
<end type="SOURCE" refId="5"/>
82+
<end type="TARGET" refId="4"/>
83+
</dependency>
84+
<dependency id="16">
85+
<end type="SOURCE" refId="5"/>
86+
<end type="TARGET" refId="2"/>
87+
</dependency>
88+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
89+
sort-features="false" accessors="true" visibility="true">
90+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
91+
<operations public="true" package="true" protected="true" private="true" static="true"/>
92+
</classifier-display>
93+
<association-display labels="true" multiplicity="true"/>
94+
</class-diagram>

data-mapper/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tags:
1313
Object provides an abstract interface to some type of database or
1414
other persistence mechanism.
1515

16-
![alt text](./etc/dm.png "Data Mapper")
16+
![alt text](./etc/data-mapper.png "Data Mapper")
1717

1818
## Applicability
1919
Use the Data Mapper in any of the following situations

data-mapper/src/main/java/com/iluwatar/datamapper/App.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.log4j.Logger;
2424

2525
/**
26-
*
2726
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
2827
* database. Its responsibility is to transfer data between the two and also to isolate them from
2928
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
@@ -39,43 +38,18 @@ public final class App {
3938
private static Logger log = Logger.getLogger(App.class);
4039

4140

42-
private static final String DB_TYPE_ORACLE = "Oracle";
43-
private static final String DB_TYPE_MYSQL = "MySQL";
44-
4541

4642
/**
4743
* Program entry point.
4844
*
4945
* @param args command line args.
5046
*/
51-
public static final void main(final String... args) {
52-
53-
if (log.isInfoEnabled() & args.length > 0) {
54-
log.debug("App.main(), db type: " + args[0]);
55-
}
56-
57-
StudentDataMapper mapper = null;
58-
59-
/* Check the desired db type from runtime arguments */
60-
if (args.length == 0) {
47+
public static void main(final String... args) {
6148

62-
/* Create default data mapper for mysql */
63-
mapper = new StudentMySQLDataMapper();
6449

65-
} else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) {
66-
67-
/* Create new data mapper for mysql */
68-
mapper = new StudentMySQLDataMapper();
69-
70-
} else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) {
71-
72-
/* Create new data mapper for oracle */
73-
mapper = new StudentMySQLDataMapper();
74-
} else {
75-
76-
/* Don't couple any Data Mapper to java.sql.SQLException */
77-
throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported");
78-
}
50+
/* Create any type of mapper at implementation which is desired */
51+
/* final StudentDataMapper mapper = new StudentFirstDataMapper(); */
52+
final StudentDataMapper mapper = new StudentSecondDataMapper();
7953

8054
/* Create new student */
8155
Student student = new Student(1, "Adam", 'A');

data-mapper/src/main/java/com/iluwatar/datamapper/Student.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ public final class Student implements Serializable {
2929
private String name;
3030
private char grade;
3131

32-
public Student() {
33-
34-
}
3532

33+
/**
34+
* Use this constructor to create a Student with all details
35+
*
36+
* @param studentId as unique student id
37+
* @param name as student name
38+
* @param grade as respective grade of student
39+
*/
3640
public Student(final int studentId, final String name, final char grade) {
3741
super();
3842

@@ -41,30 +45,57 @@ public Student(final int studentId, final String name, final char grade) {
4145
this.grade = grade;
4246
}
4347

44-
public final int getStudentId() {
48+
/**
49+
*
50+
* @return the student id
51+
*/
52+
public int getStudentId() {
4553
return studentId;
4654
}
4755

48-
public final void setStudentId(final int studentId) {
56+
/**
57+
*
58+
* @param studentId as unique student id
59+
*/
60+
public void setStudentId(final int studentId) {
4961
this.studentId = studentId;
5062
}
5163

52-
public final String getName() {
64+
/**
65+
*
66+
* @return name of student
67+
*/
68+
public String getName() {
5369
return name;
5470
}
5571

56-
public final void setName(final String name) {
72+
/**
73+
*
74+
* @param name as 'name' of student
75+
*/
76+
public void setName(final String name) {
5777
this.name = name;
5878
}
5979

60-
public final char getGrade() {
80+
/**
81+
*
82+
* @return grade of student
83+
*/
84+
public char getGrade() {
6185
return grade;
6286
}
6387

64-
public final void setGrade(final char grade) {
88+
/**
89+
*
90+
* @param grade as 'grade of student'
91+
*/
92+
public void setGrade(final char grade) {
6593
this.grade = grade;
6694
}
6795

96+
/**
97+
*
98+
*/
6899
@Override
69100
public boolean equals(final Object inputObject) {
70101

@@ -74,30 +105,35 @@ public boolean equals(final Object inputObject) {
74105
if (this == inputObject) {
75106

76107
isEqual = true;
77-
}
78-
/* Check if objects belong to same class */
79-
else if (inputObject != null && getClass() == inputObject.getClass()) {
108+
} else if (inputObject != null && getClass() == inputObject.getClass()) {
80109

81-
final Student student = (Student) inputObject;
110+
final Student inputStudent = (Student) inputObject;
82111

83112
/* If student id matched */
84-
if (this.getStudentId() == student.getStudentId()) {
113+
if (this.getStudentId() == inputStudent.getStudentId()) {
85114

86115
isEqual = true;
87116
}
88117
}
118+
89119
return isEqual;
90120
}
91121

122+
/**
123+
*
124+
*/
92125
@Override
93126
public int hashCode() {
94127

95128
/* Student id is assumed to be unique */
96129
return this.getStudentId();
97130
}
98131

132+
/**
133+
*
134+
*/
99135
@Override
100-
public final String toString() {
136+
public String toString() {
101137
return "Student [studentId=" + studentId + ", name=" + name + ", grade=" + grade + "]";
102138
}
103139
}

data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
public interface StudentDataMapper {
2424

25-
public Optional<Student> find(final int studentId);
25+
Optional<Student> find(int studentId);
2626

27-
public void insert(final Student student) throws DataMapperException;
27+
void insert(Student student) throws DataMapperException;
2828

29-
public void update(final Student student) throws DataMapperException;
29+
void update(Student student) throws DataMapperException;
3030

31-
public void delete(final Student student) throws DataMapperException;
31+
void delete(Student student) throws DataMapperException;
3232
}

0 commit comments

Comments
 (0)