Skip to content

Commit

Permalink
Add Java annotation to code blocks in README files
Browse files Browse the repository at this point in the history
  • Loading branch information
Deathnerd committed Mar 28, 2018
1 parent 6879990 commit 86ee59c
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 45 deletions.
8 changes: 4 additions & 4 deletions abstract-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Wikipedia says

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

```
```java
public interface Castle {
String getDescription();
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public class ElfArmy implements Army {

Then we have the abstraction and implementations for the kingdom factory

```
```java
public interface KingdomFactory {
Castle createCastle();
King createKing();
Expand Down Expand Up @@ -108,7 +108,7 @@ public class OrcKingdomFactory implements KingdomFactory {

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.

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

```
```java
public static class FactoryMaker {

public enum KingdomType {
Expand Down
8 changes: 4 additions & 4 deletions adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Consider a captain that can only use rowing boats and cannot sail at all.

First we have interfaces `RowingBoat` and `FishingBoat`

```
```java
public interface RowingBoat {
void row();
}
Expand All @@ -55,7 +55,7 @@ public class FishingBoat {

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

```
```java
public class Captain implements RowingBoat {

private RowingBoat rowingBoat;
Expand All @@ -73,7 +73,7 @@ public class Captain implements RowingBoat {

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.

```
```java
public class FishingBoatAdapter implements RowingBoat {

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

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

```
```java
Captain captain = new Captain(new FishingBoatAdapter());
captain.row();
```
Expand Down
6 changes: 3 additions & 3 deletions bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Wikipedia says

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

```
```java
public interface Weapon {
void wield();
void swing();
Expand Down Expand Up @@ -109,7 +109,7 @@ public class Hammer implements Weapon {

And the separate enchantment hierarchy

```
```java
public interface Enchantment {
void onActivate();
void apply();
Expand Down Expand Up @@ -155,7 +155,7 @@ public class SoulEatingEnchantment implements Enchantment {

And both the hierarchies in action

```
```java
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
enchantedSword.wield();
enchantedSword.swing();
Expand Down
8 changes: 4 additions & 4 deletions builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Wikipedia says
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:

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

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

```
```java
public final class Hero {
private final Profession profession;
private final String name;
Expand All @@ -64,7 +64,7 @@ public final class Hero {

And then we have the builder

```
```java
public static class Builder {
private final Profession profession;
private final String name;
Expand Down Expand Up @@ -109,7 +109,7 @@ And then we have the builder

And then it can be used as:

```
```java
Hero mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
```

Expand Down
20 changes: 12 additions & 8 deletions chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Wikipedia says

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

```
```java
public class Request {

private final RequestType requestType;
Expand Down Expand Up @@ -64,7 +64,7 @@ public enum RequestType {

Then the request handler hierarchy

```
```java
public abstract class RequestHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
private RequestHandler next;
Expand Down Expand Up @@ -114,7 +114,7 @@ public class OrcCommander extends RequestHandler {

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

```
```java
public class OrcKing {
RequestHandler chain;

Expand All @@ -134,11 +134,15 @@ public class OrcKing {

Then it is used as follows

```
OrcKing king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
```java
public class Main() {
public static void main(String[] args) {
OrcKing king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
}
}
```

## Applicability
Expand Down
6 changes: 3 additions & 3 deletions composite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Wikipedia says

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

```
```java
public abstract class LetterComposite {
private List<LetterComposite> children = new ArrayList<>();
public void add(LetterComposite letter) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public class Sentence extends LetterComposite {

Then we have a messenger to carry messages

```
```java
public class Messenger {
LetterComposite messageFromOrcs() {
List<Word> words = new ArrayList<>();
Expand Down Expand Up @@ -122,7 +122,7 @@ public class Messenger {

And then it can be used as

```
```java
LetterComposite orcMessage = new Messenger().messageFromOrcs();
orcMessage.print(); // Where there is a whip there is a way.
LetterComposite elfMessage = new Messenger().messageFromElves();
Expand Down
6 changes: 3 additions & 3 deletions decorator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Wikipedia says

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

```
```java
public interface Troll {
void attack();
int getAttackPower();
Expand Down Expand Up @@ -66,7 +66,7 @@ public class SimpleTroll implements Troll {

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

```
```java
public class ClubbedTroll implements Troll {

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

Here's the troll in action

```
```java
// simple troll
Troll troll = new SimpleTroll();
troll.attack(); // The troll tries to grab you!
Expand Down
6 changes: 3 additions & 3 deletions facade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Wikipedia says

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

```
```java
public abstract class DwarvenMineWorker {

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

To operate all these goldmine workers we have the facade

```
```java
public class DwarvenGoldmineFacade {

private final List<DwarvenMineWorker> workers;
Expand Down Expand Up @@ -175,7 +175,7 @@ public class DwarvenGoldmineFacade {

Now to use the facade

```
```java
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
facade.startNewDay();
// Dwarf gold digger wakes up.
Expand Down
4 changes: 2 additions & 2 deletions factory-method/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Wikipedia says

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

```
```java
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
Expand All @@ -55,7 +55,7 @@ public class OrcBlacksmith implements Blacksmith {

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

```
```java
Blacksmith blacksmith = new ElfBlacksmith();
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
Expand Down
6 changes: 3 additions & 3 deletions flyweight/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Wikipedia says

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

```
```java
public interface Potion {
void drink();
}
Expand Down Expand Up @@ -64,7 +64,7 @@ public class InvisibilityPotion implements Potion {

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

```
```java
public class PotionFactory {

private final Map<PotionType, Potion> potions;
Expand Down Expand Up @@ -100,7 +100,7 @@ public class PotionFactory {

And it can be used as below

```
```java
PotionFactory factory = new PotionFactory();
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
Expand Down
4 changes: 2 additions & 2 deletions prototype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ In short, it allows you to create a copy of an existing object and modify it to

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

```
```java
class Sheep implements Cloneable {
private String name;
public Sheep(String name) { this.name = name; }
Expand All @@ -48,7 +48,7 @@ class Sheep implements Cloneable {

Then it can be cloned like below

```
```java
Sheep original = new Sheep("Jolly");
System.out.println(original.getName()); // Jolly

Expand Down
8 changes: 4 additions & 4 deletions proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Wikipedia says

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

```
```java
public interface WizardTower {

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

Then a simple wizard class

```
```java
public class Wizard {

private final String name;
Expand All @@ -71,7 +71,7 @@ public class Wizard {

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

```
```java
public class WizardTowerProxy implements WizardTower {

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

And here is tower entering scenario

```
```java
WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
proxy.enter(new Wizard("Red wizard")); // Red wizard enters the tower.
proxy.enter(new Wizard("White wizard")); // White wizard enters the tower.
Expand Down
4 changes: 2 additions & 2 deletions singleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ Joshua Bloch, Effective Java 2nd Edition p.18

> A single-element enum type is the best way to implement a singleton
```
```java
public enum EnumIvoryTower {
INSTANCE;
}
```

Then in order to use

```
```java
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
Expand Down

0 comments on commit 86ee59c

Please sign in to comment.