Skip to content

Commit 3a9d168

Browse files
committed
Initial commit of the object mother iluwatar#498
Add a simple king and queen classes which implement the behaviour of the royalty interface. Also wrote the object mother of royalty objects which is final so you can just call the static methods in it to create objects with a specific state to use them fast in tests. The tests are already created for testing the behaviour and the type of the objects which are created by the object mother. I also created the UML diagrams via object aid and updated the readme.
1 parent 4ca205c commit 3a9d168

File tree

9 files changed

+475
-0
lines changed

9 files changed

+475
-0
lines changed

Diff for: object-mother/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: pattern
3+
title: Object Mother
4+
folder: object-mother
5+
permalink: /patterns/object-mother/
6+
pumlid:
7+
categories: Creational
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Object Mother
14+
Define a factory of immutable content with separated builder and factory interfaces.
15+
16+
![alt text](./etc/object-mother.png "Object Mother")
17+
18+
## Applicability
19+
Use the Object Mother pattern when
20+
21+
* You want consistent objects over several tests
22+
* you want to reduce code for creation of objects in tests
23+
* every test should run with fresh data
24+
25+
## Credits
26+
27+
* [Answer by David Brown](http://stackoverflow.com/questions/923319/what-is-an-objectmother) to the stackoverflow question: [What is an ObjectMother?](http://stackoverflow.com/questions/923319/what-is-an-objectmother)
28+
29+
* [c2wiki - Object Mother](http://c2.com/cgi/wiki?ObjectMother)
30+
31+
* [Nat Pryce - Test Data Builders: an alternative to the Object Mother pattern](http://www.natpryce.com/articles/000714.html)

Diff for: object-mother/etc/object-mother.png

19.2 KB
Loading

Diff for: object-mother/etc/object-mother.ucls

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.objectmother.RoyaltyObjectMother" project="object-mother"
6+
file="/object-mother/src/main/java/com/iluwatar/objectmother/RoyaltyObjectMother.java" binary="false"
7+
corner="BOTTOM_RIGHT">
8+
<position height="-1" width="-1" x="535" y="366"/>
9+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
10+
sort-features="false" accessors="true" visibility="true">
11+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
12+
<operations public="true" package="true" protected="true" private="true" static="true"/>
13+
</display>
14+
</class>
15+
<class id="2" language="java" name="com.iluwatar.objectmother.King" project="object-mother"
16+
file="/object-mother/src/main/java/com/iluwatar/objectmother/King.java" binary="false" corner="BOTTOM_RIGHT">
17+
<position height="-1" width="-1" x="285" y="141"/>
18+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
19+
sort-features="false" accessors="true" visibility="true">
20+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
21+
<operations public="true" package="true" protected="true" private="true" static="true"/>
22+
</display>
23+
</class>
24+
<class id="3" language="java" name="com.iluwatar.objectmother.Queen" project="object-mother"
25+
file="/object-mother/src/main/java/com/iluwatar/objectmother/Queen.java" binary="false" corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="783" y="143"/>
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+
<interface id="4" language="java" name="com.iluwatar.objectmother.Royalty" project="object-mother"
34+
file="/object-mother/src/main/java/com/iluwatar/objectmother/Royalty.java" binary="false" corner="BOTTOM_RIGHT">
35+
<position height="-1" width="-1" x="536" y="142"/>
36+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
37+
sort-features="false" accessors="true" visibility="true">
38+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
39+
<operations public="true" package="true" protected="true" private="true" static="true"/>
40+
</display>
41+
</interface>
42+
<realization id="5">
43+
<end type="SOURCE" refId="2"/>
44+
<end type="TARGET" refId="4"/>
45+
</realization>
46+
<realization id="6">
47+
<end type="SOURCE" refId="3"/>
48+
<end type="TARGET" refId="4"/>
49+
</realization>
50+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
51+
sort-features="false" accessors="true" visibility="true">
52+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
53+
<operations public="true" package="true" protected="true" private="true" static="true"/>
54+
</classifier-display>
55+
<association-display labels="true" multiplicity="true"/>
56+
</class-diagram>

Diff for: object-mother/pom.xml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2016 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
27+
xmlns="http://maven.apache.org/POM/4.0.0"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.14.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>object-mother</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.objectmother;
24+
25+
public class King implements Royalty {
26+
boolean isDrunk = false;
27+
boolean isHappy = false;
28+
29+
@Override
30+
public void makeDrunk() {
31+
isDrunk = true;
32+
}
33+
34+
@Override
35+
public void makeSober() {
36+
isDrunk = false;
37+
}
38+
39+
@Override
40+
public void makeHappy() {
41+
isHappy = true;
42+
}
43+
44+
@Override
45+
public void makeUnhappy() {
46+
isHappy = false;
47+
}
48+
49+
public boolean isHappy() {
50+
return isHappy;
51+
}
52+
53+
/**
54+
* Method to flirt to a queen.
55+
* @param queen Queen which should be flirted.
56+
*/
57+
public void flirt(Queen queen) {
58+
boolean flirtStatus = queen.getFlirted(this);
59+
if (flirtStatus == false) {
60+
this.makeUnhappy();
61+
} else {
62+
this.makeHappy();
63+
}
64+
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.objectmother;
24+
25+
public class Queen implements Royalty {
26+
private boolean isDrunk = false;
27+
private boolean isHappy = false;
28+
private boolean isFlirty = false;
29+
30+
@Override
31+
public void makeDrunk() {
32+
isDrunk = true;
33+
}
34+
35+
@Override
36+
public void makeSober() {
37+
isDrunk = false;
38+
}
39+
40+
@Override
41+
public void makeHappy() {
42+
isHappy = true;
43+
}
44+
45+
@Override
46+
public void makeUnhappy() {
47+
isHappy = false;
48+
}
49+
50+
public boolean isFlirty() {
51+
return isFlirty;
52+
}
53+
54+
public void setFlirtiness(boolean flirtiness) {
55+
this.isFlirty = flirtiness;
56+
}
57+
58+
/**
59+
* Method which is called when the king is flirting to a queen.
60+
* @param king King who initialized the flirt.
61+
* @return A value which describes if the flirt was successful or not.
62+
*/
63+
public boolean getFlirted(King king) {
64+
if (this.isFlirty && king.isHappy && !king.isDrunk) {
65+
return true;
66+
}
67+
return false;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.objectmother;
24+
25+
public interface Royalty {
26+
void makeDrunk();
27+
28+
void makeSober();
29+
30+
void makeHappy();
31+
32+
void makeUnhappy();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.objectmother;
24+
25+
public final class RoyaltyObjectMother {
26+
27+
/**
28+
* Method to create a sober and unhappy king. The standard paramters are set.
29+
* @return An instance of {@link com.iluwatar.objectmother.King} with the standard properties.
30+
*/
31+
public static King createSoberUnhappyKing() {
32+
return new King();
33+
}
34+
35+
/**
36+
* Method of the object mother to create a drunk king.
37+
* @return A drunk {@link com.iluwatar.objectmother.King}.
38+
*/
39+
public static King createDrunkKing() {
40+
King king = new King();
41+
king.makeDrunk();
42+
return king;
43+
}
44+
45+
/**
46+
* Method to create a happy king.
47+
* @return A happy {@link com.iluwatar.objectmother.King}.
48+
*/
49+
public static King createHappyKing() {
50+
King king = new King();
51+
king.makeHappy();
52+
return king;
53+
}
54+
55+
/**
56+
* Method to create a happy and drunk king.
57+
* @return A drunk and happy {@link com.iluwatar.objectmother.King}.
58+
*/
59+
public static King createHappyDrunkKing() {
60+
King king = new King();
61+
king.makeHappy();
62+
king.makeDrunk();
63+
return king;
64+
}
65+
66+
/**
67+
* Method to create a flirty queen.
68+
* @return A flirty {@link com.iluwatar.objectmother.Queen}.
69+
*/
70+
public static Queen createFlirtyQueen() {
71+
Queen queen = new Queen();
72+
queen.setFlirtiness(true);
73+
return queen;
74+
}
75+
76+
/**
77+
* Method to create a not flirty queen.
78+
* @return A not flirty {@link com.iluwatar.objectmother.Queen}.
79+
*/
80+
public static Queen createNotFlirtyQueen() {
81+
return new Queen();
82+
}
83+
}

0 commit comments

Comments
 (0)