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

Commit11efae3

Browse files
committed
Update explanation for the prototype pattern
1 parentbbc2a9d commit11efae3

File tree

2 files changed

+117
-25
lines changed

2 files changed

+117
-25
lines changed

‎prototype/README.md

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ copying this prototype.
1717

1818
##Explanation
1919

20-
First it should be noted that Prototype pattern is not used to gain performance benefits. It's only
21-
used for creating new objects from prototypeinstance.
20+
First, it should be noted that the Prototype pattern is not used to gain performance benefits. It's only
21+
used for creating new objects from prototypeinstances.
2222

23-
Realworld example
23+
Real-world example
2424

2525
>Remember Dolly? The sheep that was cloned! Lets not get into the details but the key point here is
2626
>that it is all about cloning.
2727
2828
In plain words
2929

30-
>Create object based on an existing object through cloning.
30+
>Createanobject based on an existing object through cloning.
3131
3232
Wikipedia says
3333

@@ -40,35 +40,127 @@ of going through the trouble of creating an object from scratch and setting it u
4040

4141
**Programmatic Example**
4242

43-
In Java, it can be easily done by implementing`Cloneable` and overriding`clone` from`Object`
43+
In Java, the prototype pattern is recommended to be implemented as follows. First, create an
44+
interface with a method for cloning objects. In this example,`Prototype` interface accomplishes
45+
this with its`copy` method.
4446

4547
```java
46-
classSheepimplementsCloneable {
47-
privateString name;
48-
publicSheep(Stringname) {this.name= name; }
49-
publicvoidsetName(Stringname) {this.name= name; }
50-
publicStringgetName() {return name; }
48+
publicinterfacePrototype {
49+
Objectcopy();
50+
}
51+
```
52+
53+
Our example contains a hierarchy of different creatures. For example, let's look at`Beast` and
54+
`OrcBeast` classes.
55+
56+
```java
57+
@EqualsAndHashCode
58+
@NoArgsConstructor
59+
publicabstractclassBeastimplementsPrototype {
60+
61+
publicBeast(Beastsource) {
62+
}
63+
64+
@Override
65+
publicabstractBeastcopy();
66+
}
67+
68+
@EqualsAndHashCode(callSuper=false)
69+
@RequiredArgsConstructor
70+
publicclassOrcBeastextendsBeast {
71+
72+
privatefinalString weapon;
73+
74+
publicOrcBeast(OrcBeastorcBeast) {
75+
super(orcBeast);
76+
this.weapon= orcBeast.weapon;
77+
}
78+
79+
@Override
80+
publicOrcBeastcopy() {
81+
returnnewOrcBeast(this);
82+
}
83+
5184
@Override
52-
publicSheepclone() {
53-
try {
54-
return (Sheep)super.clone();
55-
}catch(CloneNotSuportedException) {
56-
thrownewInternalError();
57-
}
85+
publicStringtoString() {
86+
return"Orcish wolf attacks with"+ weapon;
5887
}
5988
}
6089
```
6190

62-
Then it can be cloned like below:
91+
We don't want to go into too much details, but the full example contains also base classes`Mage`
92+
and`Warlord` and there are specialized implementations for those for elves in addition to orcs.
93+
94+
To take full advantage of the prototype pattern, we create`HeroFactory` and`HeroFactoryImpl`
95+
classes to produce different kinds of creatures from prototypes.
96+
97+
```java
98+
publicinterfaceHeroFactory {
99+
100+
MagecreateMage();
101+
WarlordcreateWarlord();
102+
BeastcreateBeast();
103+
}
104+
105+
@RequiredArgsConstructor
106+
publicclassHeroFactoryImplimplementsHeroFactory {
107+
108+
privatefinalMage mage;
109+
privatefinalWarlord warlord;
110+
privatefinalBeast beast;
111+
112+
publicMagecreateMage() {
113+
return mage.copy();
114+
}
115+
116+
publicWarlordcreateWarlord() {
117+
return warlord.copy();
118+
}
119+
120+
publicBeastcreateBeast() {
121+
return beast.copy();
122+
}
123+
}
124+
```
125+
126+
Now, we are able to show the full prototype pattern in action producing new creatures by cloning
127+
existing instances.
63128

64129
```java
65-
var original=newSheep("Jolly");
66-
System.out.println(original.getName());// Jolly
130+
var factory=newHeroFactoryImpl(
131+
newElfMage("cooking"),
132+
newElfWarlord("cleaning"),
133+
newElfBeast("protecting")
134+
);
135+
var mage= factory.createMage();
136+
var warlord= factory.createWarlord();
137+
var beast= factory.createBeast();
138+
LOGGER.info(mage.toString());
139+
LOGGER.info(warlord.toString());
140+
LOGGER.info(beast.toString());
141+
142+
factory=newHeroFactoryImpl(
143+
newOrcMage("axe"),
144+
newOrcWarlord("sword"),
145+
newOrcBeast("laser")
146+
);
147+
mage= factory.createMage();
148+
warlord= factory.createWarlord();
149+
beast= factory.createBeast();
150+
LOGGER.info(mage.toString());
151+
LOGGER.info(warlord.toString());
152+
LOGGER.info(beast.toString());
153+
```
67154

68-
// Clone and modify what is required
69-
var cloned= original.clone();
70-
cloned.setName("Dolly");
71-
System.out.println(cloned.getName());// Dolly
155+
Here's the console output from running the example.
156+
157+
```
158+
Elven mage helps in cooking
159+
Elven warlord helps in cleaning
160+
Elven eagle helps in protecting
161+
Orcish mage attacks with axe
162+
Orcish warlord attacks with sword
163+
Orcish wolf attacks with laser
72164
```
73165

74166
##Class diagram
@@ -87,7 +179,7 @@ more convenient to install a corresponding number of prototypes and clone them r
87179
instantiating the class manually, each time with the appropriate state.
88180
* When object creation is expensive compared to cloning.
89181

90-
##Real world examples
182+
##Known uses
91183

92184
*[java.lang.Object#clone()](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone%28%29)
93185

‎prototype/src/main/java/com/iluwatar/prototype/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* The Prototype pattern is a creational design pattern in software development. It is used when the
3030
* type of objects to create is determined by a prototypical instance, which is cloned to produce
3131
* new objects. This pattern is used to: - avoid subclasses of an object creator in the client
32-
* application, like the abstract factory pattern does. - avoid the inherent cost of creating a new
32+
* application, like the abstract factory pattern, does. - avoid the inherent cost of creating a new
3333
* object in the standard way (e.g., using the 'new' keyword)
3434
*
3535
* <p>In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp