Skip to content

Commit b639f36

Browse files
committed
iluwatar#590 Add explanation for Prototype
1 parent f6c8bfb commit b639f36

6 files changed

Lines changed: 47 additions & 267 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@
464464
<param>factory-method</param>
465465
<param>abstract-factory</param>
466466
<param>builder</param>
467+
<param>prototype</param>
467468
</skipForProjects>
468469
</configuration>
469470
</plugin>

prototype/README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,57 @@ tags:
1515
Specify the kinds of objects to create using a prototypical
1616
instance, and create new objects by copying this prototype.
1717

18-
![alt text](./etc/prototype_1.png "Prototype")
18+
## Explanation
19+
Real world example
20+
21+
> Remember Dolly? The sheep that was cloned! Lets not get into the details but the key point here is that it is all about cloning.
22+
23+
In plain words
24+
25+
> Create object based on an existing object through cloning.
26+
27+
Wikipedia says
28+
29+
> The prototype pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
30+
31+
In short, it allows you to create a copy of an existing object and modify it to your needs, instead of going through the trouble of creating an object from scratch and setting it up.
32+
33+
**Programmatic Example**
34+
35+
In Java, it can be easily done by implementing `Cloneable` and overriding `clone` from `Object`
36+
37+
```
38+
class Sheep implements Cloneable {
39+
privage String name;
40+
public Sheep(String name) { this.name = name; }
41+
public void setName(String name) { this.name = name; }
42+
public String getName() { return name; }
43+
@Override
44+
public Sheep clone() throws CloneNotSupportedException {
45+
return new Sheep(name);
46+
}
47+
}
48+
```
49+
50+
Then it can be cloned like below
51+
52+
```
53+
Sheep original = new Sheep("Jolly");
54+
System.out.println(original.getName()); // Jolly
55+
56+
// Clone and modify what is required
57+
Sheep cloned = original.clone();
58+
cloned.setName("Dolly");
59+
System.out.println(cloned.getName()); // Dolly
60+
```
1961

2062
## Applicability
2163
Use the Prototype pattern when a system should be independent of how its products are created, composed and represented; and
2264

23-
* when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
24-
* to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
65+
* when the classes to instantiate are specified at run-time, for example, by dynamic loading
66+
* to avoid building a class hierarchy of factories that parallels the class hierarchy of products
2567
* when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state
68+
* when object creation is expensive compared to cloning
2669

2770
## Real world examples
2871

prototype/etc/prototype.png

-36.9 KB
Binary file not shown.

prototype/etc/prototype.ucls

Lines changed: 0 additions & 182 deletions
This file was deleted.

prototype/etc/prototype.urm.puml

Lines changed: 0 additions & 82 deletions
This file was deleted.

prototype/etc/prototype_1.png

-95.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)