Skip to content

Commit 86ee59c

Browse files
committed
Add Java annotation to code blocks in README files
1 parent 6879990 commit 86ee59c

File tree

13 files changed

+49
-45
lines changed

13 files changed

+49
-45
lines changed

Diff for: abstract-factory/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Wikipedia says
3434

3535
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the kingdom
3636

37-
```
37+
```java
3838
public interface Castle {
3939
String getDescription();
4040
}
@@ -74,7 +74,7 @@ public class ElfArmy implements Army {
7474

7575
Then we have the abstraction and implementations for the kingdom factory
7676

77-
```
77+
```java
7878
public interface KingdomFactory {
7979
Castle createCastle();
8080
King createKing();
@@ -108,7 +108,7 @@ public class OrcKingdomFactory implements KingdomFactory {
108108

109109
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
110110

111-
```
111+
```java
112112
KingdomFactory factory = new ElfKingdomFactory();
113113
Castle castle = factory.createCastle();
114114
King king = factory.createKing();
@@ -123,7 +123,7 @@ Now, we can design a factory for our different kingdom factories. In this exampl
123123
The client can use FactoryMaker to create the desired concrete factory which, in turn, will produce different concrete objects (Army, King, Castle).
124124
In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for.
125125

126-
```
126+
```java
127127
public static class FactoryMaker {
128128

129129
public enum KingdomType {

Diff for: adapter/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Consider a captain that can only use rowing boats and cannot sail at all.
4040

4141
First we have interfaces `RowingBoat` and `FishingBoat`
4242

43-
```
43+
```java
4444
public interface RowingBoat {
4545
void row();
4646
}
@@ -55,7 +55,7 @@ public class FishingBoat {
5555

5656
And captain expects an implementation of `RowingBoat` interface to be able to move
5757

58-
```
58+
```java
5959
public class Captain implements RowingBoat {
6060

6161
private RowingBoat rowingBoat;
@@ -73,7 +73,7 @@ public class Captain implements RowingBoat {
7373

7474
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
7575

76-
```
76+
```java
7777
public class FishingBoatAdapter implements RowingBoat {
7878

7979
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
@@ -93,7 +93,7 @@ public class FishingBoatAdapter implements RowingBoat {
9393

9494
And now the `Captain` can use the `FishingBoat` to escape the pirates.
9595

96-
```
96+
```java
9797
Captain captain = new Captain(new FishingBoatAdapter());
9898
captain.row();
9999
```

Diff for: bridge/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Wikipedia says
3434

3535
Translating our weapon example from above. Here we have the `Weapon` hierarchy
3636

37-
```
37+
```java
3838
public interface Weapon {
3939
void wield();
4040
void swing();
@@ -109,7 +109,7 @@ public class Hammer implements Weapon {
109109

110110
And the separate enchantment hierarchy
111111

112-
```
112+
```java
113113
public interface Enchantment {
114114
void onActivate();
115115
void apply();
@@ -155,7 +155,7 @@ public class SoulEatingEnchantment implements Enchantment {
155155

156156
And both the hierarchies in action
157157

158-
```
158+
```java
159159
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
160160
enchantedSword.wield();
161161
enchantedSword.swing();

Diff for: builder/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Wikipedia says
3131
3232
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point or the other we have all seen a constructor like below:
3333

34-
```
34+
```java
3535
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
3636
}
3737
```
@@ -42,7 +42,7 @@ As you can see the number of constructor parameters can quickly get out of hand
4242

4343
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create
4444

45-
```
45+
```java
4646
public final class Hero {
4747
private final Profession profession;
4848
private final String name;
@@ -64,7 +64,7 @@ public final class Hero {
6464

6565
And then we have the builder
6666

67-
```
67+
```java
6868
public static class Builder {
6969
private final Profession profession;
7070
private final String name;
@@ -109,7 +109,7 @@ And then we have the builder
109109

110110
And then it can be used as:
111111

112-
```
112+
```java
113113
Hero mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
114114
```
115115

Diff for: chain/README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Wikipedia says
3333

3434
Translating our example with orcs from above. First we have the request class
3535

36-
```
36+
```java
3737
public class Request {
3838

3939
private final RequestType requestType;
@@ -64,7 +64,7 @@ public enum RequestType {
6464

6565
Then the request handler hierarchy
6666

67-
```
67+
```java
6868
public abstract class RequestHandler {
6969
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
7070
private RequestHandler next;
@@ -114,7 +114,7 @@ public class OrcCommander extends RequestHandler {
114114

115115
Then we have the Orc King who gives the orders and forms the chain
116116

117-
```
117+
```java
118118
public class OrcKing {
119119
RequestHandler chain;
120120

@@ -134,11 +134,15 @@ public class OrcKing {
134134

135135
Then it is used as follows
136136

137-
```
138-
OrcKing king = new OrcKing();
139-
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
140-
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
141-
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
137+
```java
138+
public class Main() {
139+
public static void main(String[] args) {
140+
OrcKing king = new OrcKing();
141+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
142+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
143+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
144+
}
145+
}
142146
```
143147

144148
## Applicability

Diff for: composite/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Wikipedia says
3333

3434
Taking our sentence example from above. Here we have the base class and different printable types
3535

36-
```
36+
```java
3737
public abstract class LetterComposite {
3838
private List<LetterComposite> children = new ArrayList<>();
3939
public void add(LetterComposite letter) {
@@ -91,7 +91,7 @@ public class Sentence extends LetterComposite {
9191

9292
Then we have a messenger to carry messages
9393

94-
```
94+
```java
9595
public class Messenger {
9696
LetterComposite messageFromOrcs() {
9797
List<Word> words = new ArrayList<>();
@@ -122,7 +122,7 @@ public class Messenger {
122122

123123
And then it can be used as
124124

125-
```
125+
```java
126126
LetterComposite orcMessage = new Messenger().messageFromOrcs();
127127
orcMessage.print(); // Where there is a whip there is a way.
128128
LetterComposite elfMessage = new Messenger().messageFromElves();

Diff for: decorator/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Wikipedia says
3636

3737
Let's take the troll example. First of all we have a simple troll implementing the troll interface
3838

39-
```
39+
```java
4040
public interface Troll {
4141
void attack();
4242
int getAttackPower();
@@ -66,7 +66,7 @@ public class SimpleTroll implements Troll {
6666

6767
Next we want to add club for the troll. We can do it dynamically by using a decorator
6868

69-
```
69+
```java
7070
public class ClubbedTroll implements Troll {
7171

7272
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
@@ -97,7 +97,7 @@ public class ClubbedTroll implements Troll {
9797

9898
Here's the troll in action
9999

100-
```
100+
```java
101101
// simple troll
102102
Troll troll = new SimpleTroll();
103103
troll.attack(); // The troll tries to grab you!

Diff for: facade/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Wikipedia says
3232

3333
Taking our goldmine example from above. Here we have the dwarven mine worker hierarchy
3434

35-
```
35+
```java
3636
public abstract class DwarvenMineWorker {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenMineWorker.class);
@@ -140,7 +140,7 @@ public class DwarvenCartOperator extends DwarvenMineWorker {
140140

141141
To operate all these goldmine workers we have the facade
142142

143-
```
143+
```java
144144
public class DwarvenGoldmineFacade {
145145

146146
private final List<DwarvenMineWorker> workers;
@@ -175,7 +175,7 @@ public class DwarvenGoldmineFacade {
175175

176176
Now to use the facade
177177

178-
```
178+
```java
179179
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
180180
facade.startNewDay();
181181
// Dwarf gold digger wakes up.

Diff for: factory-method/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Wikipedia says
3535

3636
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
3737

38-
```
38+
```java
3939
public interface Blacksmith {
4040
Weapon manufactureWeapon(WeaponType weaponType);
4141
}
@@ -55,7 +55,7 @@ public class OrcBlacksmith implements Blacksmith {
5555

5656
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
5757

58-
```
58+
```java
5959
Blacksmith blacksmith = new ElfBlacksmith();
6060
blacksmith.manufactureWeapon(WeaponType.SPEAR);
6161
blacksmith.manufactureWeapon(WeaponType.AXE);

Diff for: flyweight/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Wikipedia says
3232

3333
Translating our alchemist shop example from above. First of all we have different potion types
3434

35-
```
35+
```java
3636
public interface Potion {
3737
void drink();
3838
}
@@ -64,7 +64,7 @@ public class InvisibilityPotion implements Potion {
6464

6565
Then the actual Flyweight object which is the factory for creating potions
6666

67-
```
67+
```java
6868
public class PotionFactory {
6969

7070
private final Map<PotionType, Potion> potions;
@@ -100,7 +100,7 @@ public class PotionFactory {
100100

101101
And it can be used as below
102102

103-
```
103+
```java
104104
PotionFactory factory = new PotionFactory();
105105
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
106106
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)

Diff for: prototype/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ In short, it allows you to create a copy of an existing object and modify it to
3333

3434
In Java, it can be easily done by implementing `Cloneable` and overriding `clone` from `Object`
3535

36-
```
36+
```java
3737
class Sheep implements Cloneable {
3838
private String name;
3939
public Sheep(String name) { this.name = name; }
@@ -48,7 +48,7 @@ class Sheep implements Cloneable {
4848

4949
Then it can be cloned like below
5050

51-
```
51+
```java
5252
Sheep original = new Sheep("Jolly");
5353
System.out.println(original.getName()); // Jolly
5454

Diff for: proxy/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Wikipedia says
3434

3535
Taking our wizard tower example from above. Firstly we have the wizard tower interface and the ivory tower class
3636

37-
```
37+
```java
3838
public interface WizardTower {
3939

4040
void enter(Wizard wizard);
@@ -53,7 +53,7 @@ public class IvoryTower implements WizardTower {
5353

5454
Then a simple wizard class
5555

56-
```
56+
```java
5757
public class Wizard {
5858

5959
private final String name;
@@ -71,7 +71,7 @@ public class Wizard {
7171

7272
Then we have the proxy to add access control to wizard tower
7373

74-
```
74+
```java
7575
public class WizardTowerProxy implements WizardTower {
7676

7777
private static final Logger LOGGER = LoggerFactory.getLogger(WizardTowerProxy.class);
@@ -100,7 +100,7 @@ public class WizardTowerProxy implements WizardTower {
100100

101101
And here is tower entering scenario
102102

103-
```
103+
```java
104104
WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
105105
proxy.enter(new Wizard("Red wizard")); // Red wizard enters the tower.
106106
proxy.enter(new Wizard("White wizard")); // White wizard enters the tower.

Diff for: singleton/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ Joshua Bloch, Effective Java 2nd Edition p.18
3434

3535
> A single-element enum type is the best way to implement a singleton
3636
37-
```
37+
```java
3838
public enum EnumIvoryTower {
3939
INSTANCE;
4040
}
4141
```
4242

4343
Then in order to use
4444

45-
```
45+
```java
4646
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
4747
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
4848
assertEquals(enumIvoryTower1, enumIvoryTower2); // true

0 commit comments

Comments
 (0)