Skip to content

Commit 25cca35

Browse files
committed
Update README.md
1 parent b9b6777 commit 25cca35

File tree

3 files changed

+105
-70
lines changed

3 files changed

+105
-70
lines changed

prototype/README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,32 @@ tags:
1010
---
1111

1212
## 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.
1416

1517
## Explanation
1618

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.
1921

2022
Real world example
2123

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.
2326
2427
In plain words
2528

2629
> Create object based on an existing object through cloning.
2730
2831
Wikipedia says
2932

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.
3136
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.
3339

3440
**Programmatic Example**
3541

@@ -52,7 +58,7 @@ class Sheep implements Cloneable {
5258
}
5359
```
5460

55-
Then it can be cloned like below
61+
Then it can be cloned like below:
5662

5763
```java
5864
var original = new Sheep("Jolly");
@@ -65,15 +71,20 @@ System.out.println(cloned.getName()); // Dolly
6571
```
6672

6773
## Class diagram
74+
6875
![alt text](./etc/prototype.urm.png "Prototype pattern class diagram")
6976

7077
## Applicability
71-
Use the Prototype pattern when a system should be independent of how its products are created, composed and represented; and
7278

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.
7788

7889
## Real world examples
7990

proxy/README.md

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,38 @@ tags:
1010
---
1111

1212
## Also known as
13+
1314
Surrogate
1415

1516
## 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.
1819

1920
## Explanation
21+
2022
Real world example
2123

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.
2327
2428
In plain words
2529

2630
> Using the proxy pattern, a class represents the functionality of another class.
2731
2832
Wikipedia says
2933

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.
3140
3241
**Programmatic Example**
3342

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.
3545

3646
```java
3747
public interface WizardTower {
@@ -50,7 +60,7 @@ public class IvoryTower implements WizardTower {
5060
}
5161
```
5262

53-
Then a simple wizard class
63+
Then a simple `Wizard` class.
5464

5565
```java
5666
public class Wizard {
@@ -68,7 +78,7 @@ public class Wizard {
6878
}
6979
```
7080

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`.
7282

7383
```java
7484
public class WizardTowerProxy implements WizardTower {
@@ -97,28 +107,41 @@ public class WizardTowerProxy implements WizardTower {
97107
}
98108
```
99109

100-
And here is tower entering scenario
110+
And here is the tower entering scenario.
101111

102112
```java
103113
var proxy = new WizardTowerProxy(new IvoryTower());
104-
proxy.enter(new Wizard("Red wizard")); // Red wizard enters the tower.
105-
proxy.enter(new Wizard("White wizard")); // White wizard enters the tower.
106-
proxy.enter(new Wizard("Black wizard")); // Black wizard enters the tower.
107-
proxy.enter(new Wizard("Green wizard")); // Green wizard is not allowed to enter!
108-
proxy.enter(new Wizard("Brown wizard")); // Brown wizard is not allowed to enter!
114+
proxy.enter(new Wizard("Red wizard"));
115+
proxy.enter(new Wizard("White wizard"));
116+
proxy.enter(new Wizard("Black wizard"));
117+
proxy.enter(new Wizard("Green wizard"));
118+
proxy.enter(new Wizard("Brown wizard"));
119+
```
120+
121+
Program output:
122+
123+
```
124+
Red wizard enters the tower.
125+
White wizard enters the tower.
126+
Black wizard enters the tower.
127+
Green wizard is not allowed to enter!
128+
Brown wizard is not allowed to enter!
109129
```
110130

111131
## Class diagram
132+
112133
![alt text](./etc/proxy.urm.png "Proxy pattern class diagram")
113134

114135
## Applicability
115-
Proxy is applicable whenever there is a need for a more
116-
versatile or sophisticated reference to an object than a simple pointer. Here
117-
are several common situations in which the Proxy pattern is applicable
136+
137+
Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an
138+
object than a simple pointer. Here are several common situations in which the Proxy pattern is
139+
applicable.
118140

119141
* Remote proxy provides a local representative for an object in a different address space.
120142
* Virtual proxy creates expensive objects on demand.
121-
* Protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
143+
* Protection proxy controls access to the original object. Protection proxies are useful when
144+
objects should have different access rights.
122145

123146
## Typical Use Case
124147

@@ -136,7 +159,8 @@ are several common situations in which the Proxy pattern is applicable
136159

137160
* [java.lang.reflect.Proxy](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html)
138161
* [Apache Commons Proxy](https://commons.apache.org/proper/commons-proxy/)
139-
* Mocking frameworks Mockito, Powermock, EasyMock
162+
* Mocking frameworks [Mockito](https://site.mockito.org/),
163+
[Powermock](https://powermock.github.io/), [EasyMock](https://easymock.org/)
140164

141165
## Related patterns
142166

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
/*
2-
* The MIT License
3-
* Copyright © 2014-2019 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-
24-
package com.iluwatar.proxy;
25-
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
29-
/**
30-
* The object to be proxyed.
31-
*/
32-
public class IvoryTower implements WizardTower {
33-
34-
private static final Logger LOGGER = LoggerFactory.getLogger(IvoryTower.class);
35-
36-
public void enter(Wizard wizard) {
37-
LOGGER.info("{} enters the tower.", wizard);
38-
}
39-
40-
}
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 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+
24+
package com.iluwatar.proxy;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* The object to be proxied.
31+
*/
32+
public class IvoryTower implements WizardTower {
33+
34+
private static final Logger LOGGER = LoggerFactory.getLogger(IvoryTower.class);
35+
36+
public void enter(Wizard wizard) {
37+
LOGGER.info("{} enters the tower.", wizard);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)