Merge pull request #730 from Deathnerd/master

Add Java annotation to code blocks in README files #728
This commit is contained in:
Ilkka Seppälä 2018-03-31 09:50:28 +03:00 committed by GitHub
commit 7d4ccc6490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 41 additions and 41 deletions

View File

@ -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();
}
@ -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();
@ -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();
@ -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 {

View File

@ -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();
}
@ -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;
@ -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);
@ -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();
```

View File

@ -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();
@ -109,7 +109,7 @@ public class Hammer implements Weapon {
And the separate enchantment hierarchy
```
```java
public interface Enchantment {
void onActivate();
void apply();
@ -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();

View File

@ -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) {
}
```
@ -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;
@ -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;
@ -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();
```

View File

@ -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;
@ -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;
@ -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;
@ -134,7 +134,7 @@ public class OrcKing {
Then it is used as follows
```
```java
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"

View File

@ -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) {
@ -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<>();
@ -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();

View File

@ -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();
@ -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);
@ -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!

View File

@ -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);
@ -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;
@ -175,7 +175,7 @@ public class DwarvenGoldmineFacade {
Now to use the facade
```
```java
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
facade.startNewDay();
// Dwarf gold digger wakes up.

View File

@ -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);
}
@ -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);

View File

@ -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();
}
@ -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;
@ -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)

View File

@ -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; }
@ -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

View File

@ -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);
@ -53,7 +53,7 @@ public class IvoryTower implements WizardTower {
Then a simple wizard class
```
```java
public class Wizard {
private final String name;
@ -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);
@ -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.

View File

@ -34,7 +34,7 @@ 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;
}
@ -42,7 +42,7 @@ public enum EnumIvoryTower {
Then in order to use
```
```java
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
assertEquals(enumIvoryTower1, enumIvoryTower2); // true