Skip to content

Commit cc02ff4

Browse files
authored
Merge pull request iluwatar#503 from igeligel/master
Object Mother pattern iluwatar#498
2 parents 7d99505 + 6aed26e commit cc02ff4

File tree

11 files changed

+522
-1
lines changed

11 files changed

+522
-1
lines changed

object-mother/README.md

Lines changed: 31 additions & 0 deletions
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)

object-mother/etc/object-mother.png

19.2 KB
Loading

object-mother/etc/object-mother.ucls

Lines changed: 56 additions & 0 deletions
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>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@startuml
2+
package com.iluwatar.objectmother {
3+
class RoyaltyObjectMother {
4+
+ RoyaltyObjectMother()
5+
+ createDrunkKing() : King {static}
6+
+ createFlirtyQueen() : Queen {static}
7+
+ createHappyDrunkKing() : King {static}
8+
+ createHappyKing() : King {static}
9+
+ createNotFlirtyQueen() : Queen {static}
10+
+ createSoberUnhappyKing() : King {static}
11+
}
12+
class Queen {
13+
- isDrunk : boolean
14+
- isFlirty : boolean
15+
- isHappy : boolean
16+
+ Queen()
17+
+ getFlirted(king : King) : boolean
18+
+ isFlirty() : boolean
19+
+ makeDrunk()
20+
+ makeHappy()
21+
+ makeSober()
22+
+ makeUnhappy()
23+
+ setFlirtiness(flirtiness : boolean)
24+
}
25+
interface Royalty {
26+
+ makeDrunk() {abstract}
27+
+ makeHappy() {abstract}
28+
+ makeSober() {abstract}
29+
+ makeUnhappy() {abstract}
30+
}
31+
class King {
32+
~ isDrunk : boolean
33+
~ isHappy : boolean
34+
+ King()
35+
+ flirt(queen : Queen)
36+
+ isHappy() : boolean
37+
+ makeDrunk()
38+
+ makeHappy()
39+
+ makeSober()
40+
+ makeUnhappy()
41+
}
42+
}
43+
Queen ..|> Royalty
44+
King ..|> Royalty
45+
@enduml

object-mother/pom.xml

Lines changed: 48 additions & 0 deletions
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>
Lines changed: 66 additions & 0 deletions
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+
}
Lines changed: 69 additions & 0 deletions
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+
}
Lines changed: 33 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)