You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: prototype/README.md
+46-3Lines changed: 46 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,14 +15,57 @@ tags:
15
15
Specify the kinds of objects to create using a prototypical
16
16
instance, and create new objects by copying this prototype.
17
17
18
-

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
+
```
19
61
20
62
## Applicability
21
63
Use the Prototype pattern when a system should be independent of how its products are created, composed and represented; and
22
64
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
25
67
* 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
0 commit comments