Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc02ff4

Browse files
authored
Merge pull requestiluwatar#503 from igeligel/master
Object Mother patterniluwatar#498
2 parents7d99505 +6aed26e commitcc02ff4

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-diagramversion="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+
<classid="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+
<positionheight="-1"width="-1"x="535"y="366"/>
9+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
10+
sort-features="false"accessors="true"visibility="true">
11+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
12+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
13+
</display>
14+
</class>
15+
<classid="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+
<positionheight="-1"width="-1"x="285"y="141"/>
18+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
19+
sort-features="false"accessors="true"visibility="true">
20+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
21+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
22+
</display>
23+
</class>
24+
<classid="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+
<positionheight="-1"width="-1"x="783"y="143"/>
27+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
28+
sort-features="false"accessors="true"visibility="true">
29+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
30+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
31+
</display>
32+
</class>
33+
<interfaceid="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+
<positionheight="-1"width="-1"x="536"y="142"/>
36+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
37+
sort-features="false"accessors="true"visibility="true">
38+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
39+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
40+
</display>
41+
</interface>
42+
<realizationid="5">
43+
<endtype="SOURCE"refId="2"/>
44+
<endtype="TARGET"refId="4"/>
45+
</realization>
46+
<realizationid="6">
47+
<endtype="SOURCE"refId="3"/>
48+
<endtype="TARGET"refId="4"/>
49+
</realization>
50+
<classifier-displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
51+
sort-features="false"accessors="true"visibility="true">
52+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
53+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
54+
</classifier-display>
55+
<association-displaylabels="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+
packagecom.iluwatar.objectmother {
3+
classRoyaltyObjectMother {
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+
classQueen {
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+
interfaceRoyalty {
26+
+makeDrunk() {abstract}
27+
+makeHappy() {abstract}
28+
+makeSober() {abstract}
29+
+makeUnhappy() {abstract}
30+
}
31+
classKing {
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+
<projectxsi: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+
packagecom.iluwatar.objectmother;
24+
25+
publicclassKingimplementsRoyalty {
26+
booleanisDrunk =false;
27+
booleanisHappy =false;
28+
29+
@Override
30+
publicvoidmakeDrunk() {
31+
isDrunk =true;
32+
}
33+
34+
@Override
35+
publicvoidmakeSober() {
36+
isDrunk =false;
37+
}
38+
39+
@Override
40+
publicvoidmakeHappy() {
41+
isHappy =true;
42+
}
43+
44+
@Override
45+
publicvoidmakeUnhappy() {
46+
isHappy =false;
47+
}
48+
49+
publicbooleanisHappy() {
50+
returnisHappy;
51+
}
52+
53+
/**
54+
* Method to flirt to a queen.
55+
* @param queen Queen which should be flirted.
56+
*/
57+
publicvoidflirt(Queenqueen) {
58+
booleanflirtStatus =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+
packagecom.iluwatar.objectmother;
24+
25+
publicclassQueenimplementsRoyalty {
26+
privatebooleanisDrunk =false;
27+
privatebooleanisHappy =false;
28+
privatebooleanisFlirty =false;
29+
30+
@Override
31+
publicvoidmakeDrunk() {
32+
isDrunk =true;
33+
}
34+
35+
@Override
36+
publicvoidmakeSober() {
37+
isDrunk =false;
38+
}
39+
40+
@Override
41+
publicvoidmakeHappy() {
42+
isHappy =true;
43+
}
44+
45+
@Override
46+
publicvoidmakeUnhappy() {
47+
isHappy =false;
48+
}
49+
50+
publicbooleanisFlirty() {
51+
returnisFlirty;
52+
}
53+
54+
publicvoidsetFlirtiness(booleanflirtiness) {
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+
publicbooleangetFlirted(Kingking) {
64+
if (this.isFlirty &&king.isHappy && !king.isDrunk) {
65+
returntrue;
66+
}
67+
returnfalse;
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+
packagecom.iluwatar.objectmother;
24+
25+
publicinterfaceRoyalty {
26+
voidmakeDrunk();
27+
28+
voidmakeSober();
29+
30+
voidmakeHappy();
31+
32+
voidmakeUnhappy();
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp