Skip to content

Commit 04bf566

Browse files
authored
task: Explanations and grammar fixes for all the GoF patterns (iluwatar#1791)
* Grammatical fixes to command pattern * Update bridge pattern readme * Fixes to builder pattern grammar * Update chain of responsibility * Improvements to the composite example * Fixes to headings * Minor updates to decorator pattern * Update facade * Update factory example * Update factory method * Update flyweight * Interpreter explanation * Update iterator readme * Add explanation for mediator pattern * Grammatical fixes to memento * Grammar fixes for observer * Update explanation for the prototype pattern * Proxy pattern grammar fixes * Update singleton * Grammar fixes to state pattern * Grammar fixes for strategy * Grammar fixes, template method * Grammar fixes for visitor * Fix typo
1 parent bbdff14 commit 04bf566

File tree

66 files changed

+870
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+870
-355
lines changed

abstract-factory/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ Example use cases
196196
* Unit test case writing becomes much easier
197197
* UI tools for different OS
198198

199-
## Consequences:
199+
## Consequences
200200

201201
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
202202
* While the pattern is great when creating predefined objects, adding the new ones might be challenging.
203203
* The code becomes more complicated than it should be since a lot of new interfaces and classes are introduced along with the pattern.
204204

205-
## Tutorial
205+
## Tutorials
206206

207207
* [Abstract Factory Pattern Tutorial](https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java)
208208

adapter/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Use the Adapter pattern when
105105
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
106106
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
107107

108-
## Consequences:
108+
## Consequences
109109
Class and object adapters have different trade-offs. A class adapter
110110

111111
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
@@ -118,7 +118,7 @@ An object adapter
118118
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
119119

120120

121-
## Real world examples
121+
## Known uses
122122

123123
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
124124
* [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)

bridge/README.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Decouple an abstraction from its implementation so that the two can vary indepen
1919

2020
## Explanation
2121

22-
Real world example
22+
Real-world example
2323

2424
> Consider you have a weapon with different enchantments, and you are supposed to allow mixing
2525
> different weapons with different enchantments. What would you do? Create multiple copies of each
@@ -161,27 +161,36 @@ public class SoulEatingEnchantment implements Enchantment {
161161
Here are both hierarchies in action:
162162

163163
```java
164+
LOGGER.info("The knight receives an enchanted sword.");
164165
var enchantedSword = new Sword(new SoulEatingEnchantment());
165166
enchantedSword.wield();
166167
enchantedSword.swing();
167168
enchantedSword.unwield();
168-
// The sword is wielded.
169-
// The item spreads bloodlust.
170-
// The sword is swinged.
171-
// The item eats the soul of enemies.
172-
// The sword is unwielded.
173-
// Bloodlust slowly disappears.
174169

170+
LOGGER.info("The valkyrie receives an enchanted hammer.");
175171
var hammer = new Hammer(new FlyingEnchantment());
176172
hammer.wield();
177173
hammer.swing();
178174
hammer.unwield();
179-
// The hammer is wielded.
180-
// The item begins to glow faintly.
181-
// The hammer is swinged.
182-
// The item flies and strikes the enemies finally returning to owner's hand.
183-
// The hammer is unwielded.
184-
// The item's glow fades.
175+
```
176+
177+
Here's the console output.
178+
179+
```
180+
The knight receives an enchanted sword.
181+
The sword is wielded.
182+
The item spreads bloodlust.
183+
The sword is swung.
184+
The item eats the soul of enemies.
185+
The sword is unwielded.
186+
Bloodlust slowly disappears.
187+
The valkyrie receives an enchanted hammer.
188+
The hammer is wielded.
189+
The item begins to glow faintly.
190+
The hammer is swung.
191+
The item flies and strikes the enemies finally returning to owner's hand.
192+
The hammer is unwielded.
193+
The item's glow fades.
185194
```
186195

187196
## Class diagram

builder/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ process can create different representations.
1616

1717
## Explanation
1818

19-
Real world example
19+
Real-world example
2020

2121
> Imagine a character generator for a role-playing game. The easiest option is to let the computer
2222
> create the character for you. If you want to manually select the character details like
23-
> profession, gender, hair color etc. the character generation becomes a step-by-step process that
23+
> profession, gender, hair color, etc. the character generation becomes a step-by-step process that
2424
> completes when all the selections are ready.
2525
2626
In plain words
@@ -49,7 +49,7 @@ anti-pattern.
4949

5050
**Programmatic Example**
5151

52-
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to
52+
The sane alternative is to use the Builder pattern. First of all, we have our hero that we want to
5353
create:
5454

5555
```java
@@ -134,7 +134,7 @@ Use the Builder pattern when
134134
* The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
135135
* The construction process must allow different representations for the object that's constructed
136136

137-
## Real world examples
137+
## Known uses
138138

139139
* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)
140140
* [java.nio.ByteBuffer](http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#put-byte-) as well as similar buffers such as FloatBuffer, IntBuffer and so on.

chain/README.md chain-of-responsibility/README.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
---
22
layout: pattern
33
title: Chain of responsibility
4-
folder: chain
5-
permalink: /patterns/chain/
4+
folder: chain-of-responsibility
5+
permalink: /patterns/chain-of-responsibility/
66
categories: Behavioral
77
language: en
88
tags:
99
- Gang of Four
1010
---
1111

1212
## Intent
13+
1314
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
1415
handle the request. Chain the receiving objects and pass the request along the chain until an object
1516
handles it.
1617

1718
## Explanation
1819

19-
Real world example
20+
Real-world example
2021

2122
> The Orc King gives loud orders to his army. The closest one to react is the commander, then
22-
> officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
23+
> an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.
2324
2425
In plain words
2526

@@ -35,7 +36,7 @@ Wikipedia says
3536
3637
**Programmatic Example**
3738

38-
Translating our example with the orcs from above. First we have the `Request` class:
39+
Translating our example with the orcs from above. First, we have the `Request` class:
3940

4041
```java
4142
public class Request {
@@ -66,7 +67,7 @@ public enum RequestType {
6667
}
6768
```
6869

69-
Then the request handler hierarchy
70+
Next, we show the request handler hierarchy.
7071

7172
```java
7273
@Slf4j
@@ -116,7 +117,7 @@ public class OrcCommander extends RequestHandler {
116117

117118
```
118119

119-
Then we have the Orc King who gives the orders and forms the chain
120+
The Orc King gives the orders and forms the chain.
120121

121122
```java
122123
public class OrcKing {
@@ -136,18 +137,26 @@ public class OrcKing {
136137
}
137138
```
138139

139-
Then it is used as follows
140+
The chain of responsibility in action.
140141

141142
```java
142143
var king = new OrcKing();
143-
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
144-
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
145-
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
144+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
145+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
146+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
147+
```
148+
149+
The console output.
150+
151+
```
152+
Orc commander handling request "defend castle"
153+
Orc officer handling request "torture prisoner"
154+
Orc soldier handling request "collect tax"
146155
```
147156

148157
## Class diagram
149158

150-
![alt text](./etc/chain.urm.png "Chain of Responsibility class diagram")
159+
![alt text](./etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
151160

152161
## Applicability
153162

@@ -157,7 +166,7 @@ Use Chain of Responsibility when
157166
* You want to issue a request to one of several objects without specifying the receiver explicitly.
158167
* The set of objects that can handle a request should be specified dynamically.
159168

160-
## Real world examples
169+
## Known uses
161170

162171
* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
163172
* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)

chain/pom.xml chain-of-responsibility/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<artifactId>java-design-patterns</artifactId>
3232
<version>1.25.0-SNAPSHOT</version>
3333
</parent>
34-
<artifactId>chain</artifactId>
34+
<artifactId>chain-of-responsibility</artifactId>
3535
<dependencies>
3636
<dependency>
3737
<groupId>org.junit.jupiter</groupId>

command/README.md

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
layout: pattern
33
title: Command
44
folder: command
@@ -19,7 +19,7 @@ Encapsulate a request as an object, thereby letting you parameterize clients wit
1919
requests, queue or log requests, and support undoable operations.
2020

2121
## Explanation
22-
Real world example
22+
Real-world example
2323

2424
> There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one.
2525
> The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses
@@ -135,7 +135,7 @@ public class Goblin extends Target {
135135
}
136136
```
137137

138-
Finally we have the wizard in main function who casts spell
138+
Finally, we have the wizard in the main function casting spells.
139139

140140
```java
141141
public static void main(String[] args) {
@@ -202,32 +202,29 @@ Use the Command pattern when you want to:
202202
* Parameterize objects by an action to perform. You can express such parameterization in a
203203
procedural language with a callback function, that is, a function that's registered somewhere to be
204204
called at a later point. Commands are an object-oriented replacement for callbacks.
205-
* Specify, queue, and execute requests at different times. A Command object can have a lifetime
205+
* Specify, queue, and execute requests at different times. A Command object can have a life
206206
independent of the original request. If the receiver of a request can be represented in an address
207207
space-independent way, then you can transfer a command object for the request to a different process
208208
and fulfill the request there.
209209
* Support undo. The Command's execute operation can store state for reversing its effects in the
210210
command itself. The Command interface must have an added un-execute operation that reverses the
211211
effects of a previous call to execute. The executed commands are stored in a history list.
212-
Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling
213-
un-execute and execute, respectively.
212+
Unlimited-level undo and redo functionality is achieved by traversing this list backward and forward
213+
calling un-execute and execute, respectively.
214214
* Support logging changes so that they can be reapplied in case of a system crash. By augmenting the
215215
Command interface with load and store operations, you can keep a persistent log of changes.
216-
Recovering from a crash involves reloading logged commands from disk and re-executing them with
216+
Recovering from a crash involves reloading logged commands from the disk and re-executing them with
217217
the execute operation.
218218
* Structure a system around high-level operations build on primitive operations. Such a structure is
219-
common in information systems that support transactions. A transaction encapsulates a set of changes
220-
to data. The Command pattern offers a way to model transactions. Commands have a common interface,
219+
common in information systems that support transactions. A transaction encapsulates a set of data
220+
changes. The Command pattern offers a way to model transactions. Commands have a common interface,
221221
letting you invoke all transactions the same way. The pattern also makes it easy to extend the
222222
system with new transactions.
223+
* Keep a history of requests.
224+
* Implement callback functionality.
225+
* Implement the undo functionality.
223226

224-
## Typical Use Case
225-
226-
* To keep a history of requests
227-
* Implement callback functionality
228-
* Implement the undo functionality
229-
230-
## Real world examples
227+
## Known uses
231228

232229
* [java.lang.Runnable](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)
233230
* [org.junit.runners.model.Statement](https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/runners/model/Statement.java)

command/src/main/java/com/iluwatar/command/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
2828
* information needed to perform an action or trigger an event at a later time. This information
29-
* includes the method name, the object that owns the method and values for the method parameters.
29+
* includes the method name, the object that owns the method, and values for the method parameters.
3030
*
3131
* <p>Four terms always associated with the command pattern are command, receiver, invoker and
3232
* client. A command object (spell) knows about the receiver (target) and invokes a method of the

composite/README.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ treat individual objects and compositions of objects uniformly.
1616

1717
## Explanation
1818

19-
Real world example
19+
Real-world example
2020

2121
> Every sentence is composed of words which are in turn composed of characters. Each of these
22-
> objects is printable and they can have something printed before or after them like sentence always
23-
> ends with full stop and word always has space before it.
22+
> objects are printable and they can have something printed before or after them like sentence
23+
> always ends with full stop and word always has space before it.
2424
2525
In plain words
2626

27-
> Composite pattern lets clients treat the individual objects in a uniform manner.
27+
> Composite pattern lets clients uniformly treat the individual objects.
2828
2929
Wikipedia says
3030

@@ -154,10 +154,22 @@ public class Messenger {
154154
And then it can be used as:
155155

156156
```java
157-
var orcMessage = new Messenger().messageFromOrcs();
158-
orcMessage.print(); // Where there is a whip there is a way.
159-
var elfMessage = new Messenger().messageFromElves();
160-
elfMessage.print(); // Much wind pours from your mouth.
157+
var messenger = new Messenger();
158+
159+
LOGGER.info("Message from the orcs: ");
160+
messenger.messageFromOrcs().print();
161+
162+
LOGGER.info("Message from the elves: ");
163+
messenger.messageFromElves().print();
164+
```
165+
166+
The console output:
167+
168+
```
169+
Message from the orcs:
170+
Where there is a whip there is a way.
171+
Message from the elves:
172+
Much wind pours from your mouth.
161173
```
162174

163175
## Class diagram
@@ -172,7 +184,7 @@ Use the Composite pattern when
172184
* You want clients to be able to ignore the difference between compositions of objects and
173185
individual objects. Clients will treat all objects in the composite structure uniformly.
174186

175-
## Real world examples
187+
## Known uses
176188

177189
* [java.awt.Container](http://docs.oracle.com/javase/8/docs/api/java/awt/Container.html) and [java.awt.Component](http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html)
178190
* [Apache Wicket](https://github.com/apache/wicket) component tree, see [Component](https://github.com/apache/wicket/blob/91e154702ab1ff3481ef6cbb04c6044814b7e130/wicket-core/src/main/java/org/apache/wicket/Component.java) and [MarkupContainer](https://github.com/apache/wicket/blob/b60ec64d0b50a611a9549809c9ab216f0ffa3ae3/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java)

composite/src/main/java/com/iluwatar/composite/App.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ public class App {
4545
* @param args command line args
4646
*/
4747
public static void main(String[] args) {
48-
LOGGER.info("Message from the orcs: ");
4948

50-
var orcMessage = new Messenger().messageFromOrcs();
51-
orcMessage.print();
49+
var messenger = new Messenger();
5250

53-
LOGGER.info("\nMessage from the elves: ");
51+
LOGGER.info("Message from the orcs: ");
52+
messenger.messageFromOrcs().print();
5453

55-
var elfMessage = new Messenger().messageFromElves();
56-
elfMessage.print();
54+
LOGGER.info("Message from the elves: ");
55+
messenger.messageFromElves().print();
5756
}
5857
}

composite/src/main/java/com/iluwatar/composite/Sentence.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public Sentence(List<Word> words) {
3939

4040
@Override
4141
protected void printThisAfter() {
42-
System.out.print(".");
42+
System.out.print(".\n");
4343
}
4444
}

0 commit comments

Comments
 (0)