diff --git a/abstract-factory/README.md b/abstract-factory/README.md index 423988b58c76..0bf86dfa6a91 100644 --- a/abstract-factory/README.md +++ b/abstract-factory/README.md @@ -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 { diff --git a/adapter/README.md b/adapter/README.md index 3344490ff9a2..e943baba52fd 100644 --- a/adapter/README.md +++ b/adapter/README.md @@ -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(); ``` diff --git a/bridge/README.md b/bridge/README.md index bce723971cf6..cfbbcf0d95a4 100644 --- a/bridge/README.md +++ b/bridge/README.md @@ -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(); diff --git a/builder/README.md b/builder/README.md index 51495619bbaa..b775f1064a1e 100644 --- a/builder/README.md +++ b/builder/README.md @@ -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(); ``` diff --git a/chain/README.md b/chain/README.md index 6bca816109b1..ff08acdf7205 100644 --- a/chain/README.md +++ b/chain/README.md @@ -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,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 diff --git a/composite/README.md b/composite/README.md index e4f45ddd0856..05ee70cbb9b9 100644 --- a/composite/README.md +++ b/composite/README.md @@ -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 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 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(); diff --git a/decorator/README.md b/decorator/README.md index dcf49dbf57b5..9998b9938220 100644 --- a/decorator/README.md +++ b/decorator/README.md @@ -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! diff --git a/facade/README.md b/facade/README.md index 7caa89d94684..505ceee394bb 100644 --- a/facade/README.md +++ b/facade/README.md @@ -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 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. diff --git a/factory-method/README.md b/factory-method/README.md index ab3739ac32a9..087221fb9dfe 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -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); diff --git a/flyweight/README.md b/flyweight/README.md index d659957551e4..e008fc3465a4 100644 --- a/flyweight/README.md +++ b/flyweight/README.md @@ -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 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) diff --git a/prototype/README.md b/prototype/README.md index 4bfeebe19871..483a5e469274 100644 --- a/prototype/README.md +++ b/prototype/README.md @@ -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 diff --git a/proxy/README.md b/proxy/README.md index 80593c75e1c1..e0628344a931 100644 --- a/proxy/README.md +++ b/proxy/README.md @@ -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. diff --git a/singleton/README.md b/singleton/README.md index 90a8458462ef..52aca8e162e6 100644 --- a/singleton/README.md +++ b/singleton/README.md @@ -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