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
+23-12Lines changed: 23 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -10,26 +10,32 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
13
+
14
+
Specify the kinds of objects to create using a prototypical instance, and create new objects by
15
+
copying this prototype.
14
16
15
17
## Explanation
16
18
17
-
First it should be noted that Prototype pattern is not used to gain performance benefits. It's only used for creating
18
-
new objects from prototype instance.
19
+
First it should be noted that Prototype pattern is not used to gain performance benefits. It's only
20
+
used for creating new objects from prototype instance.
19
21
20
22
Real world example
21
23
22
-
> 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.
24
+
> Remember Dolly? The sheep that was cloned! Lets not get into the details but the key point here is
25
+
> that it is all about cloning.
23
26
24
27
In plain words
25
28
26
29
> Create object based on an existing object through cloning.
27
30
28
31
Wikipedia says
29
32
30
-
> 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.
33
+
> The prototype pattern is a creational design pattern in software development. It is used when the
34
+
> type of objects to create is determined by a prototypical instance, which is cloned to produce new
35
+
> objects.
31
36
32
-
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.
37
+
In short, it allows you to create a copy of an existing object and modify it to your needs, instead
38
+
of going through the trouble of creating an object from scratch and setting it up.
33
39
34
40
**Programmatic Example**
35
41
@@ -52,7 +58,7 @@ class Sheep implements Cloneable {

69
76
70
77
## Applicability
71
-
Use the Prototype pattern when a system should be independent of how its products are created, composed and represented; and
72
78
73
-
* When the classes to instantiate are specified at run-time, for example, by dynamic loading
74
-
* To avoid building a class hierarchy of factories that parallels the class hierarchy of products
75
-
* 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
76
-
* When object creation is expensive compared to cloning
79
+
Use the Prototype pattern when a system should be independent of how its products are created,
80
+
composed, represented and
81
+
82
+
* When the classes to instantiate are specified at run-time, for example, by dynamic loading.
83
+
* To avoid building a class hierarchy of factories that parallels the class hierarchy of products.
84
+
* When instances of a class can have one of only a few different combinations of state. It may be
85
+
more convenient to install a corresponding number of prototypes and clone them rather than
86
+
instantiating the class manually, each time with the appropriate state.
87
+
* When object creation is expensive compared to cloning.
Copy file name to clipboardExpand all lines: proxy/README.md
+42-18Lines changed: 42 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -10,28 +10,38 @@ tags:
10
10
---
11
11
12
12
## Also known as
13
+
13
14
Surrogate
14
15
15
16
## Intent
16
-
Provide a surrogate or placeholder for another object to control
17
-
access to it.
17
+
18
+
Provide a surrogate or placeholder for another object to control access to it.
18
19
19
20
## Explanation
21
+
20
22
Real world example
21
23
22
-
> Imagine a tower where the local wizards go to study their spells. The ivory tower can only be accessed through a proxy which ensures that only the first three wizards can enter. Here the proxy represents the functionality of the tower and adds access control to it.
24
+
> Imagine a tower where the local wizards go to study their spells. The ivory tower can only be
25
+
> accessed through a proxy which ensures that only the first three wizards can enter. Here the proxy
26
+
> represents the functionality of the tower and adds access control to it.
23
27
24
28
In plain words
25
29
26
30
> Using the proxy pattern, a class represents the functionality of another class.
27
31
28
32
Wikipedia says
29
33
30
-
> A proxy, in its most general form, is a class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked.
34
+
> A proxy, in its most general form, is a class functioning as an interface to something else.
35
+
> A proxy is a wrapper or agent object that is being called by the client to access the real serving
36
+
> object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can
37
+
> provide additional logic. In the proxy extra functionality can be provided, for example caching
38
+
> when operations on the real object are resource intensive, or checking preconditions before
39
+
> operations on the real object are invoked.
31
40
32
41
**Programmatic Example**
33
42
34
-
Taking our wizard tower example from above. Firstly we have the wizard tower interface and the ivory tower class
43
+
Taking our wizard tower example from above. Firstly we have the `WizardTower` interface and the
44
+
`IvoryTower` class.
35
45
36
46
```java
37
47
publicinterfaceWizardTower {
@@ -50,7 +60,7 @@ public class IvoryTower implements WizardTower {
50
60
}
51
61
```
52
62
53
-
Then a simple wizard class
63
+
Then a simple `Wizard` class.
54
64
55
65
```java
56
66
publicclassWizard {
@@ -68,7 +78,7 @@ public class Wizard {
68
78
}
69
79
```
70
80
71
-
Then we have the proxy to add access control to wizard tower
81
+
Then we have the `WizardTowerProxy` to add access control to `WizardTower`.
0 commit comments