Formatted all files to the same standard
This commit is contained in:
parent
53a2a8b150
commit
3da9ad5469
@ -1,29 +1,29 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The essence of the Abstract Factory pattern is a factory interface
|
* The essence of the Abstract Factory pattern is a factory interface
|
||||||
* (KingdomFactory) and its implementations (ElfKingdomFactory,
|
* (KingdomFactory) and its implementations (ElfKingdomFactory,
|
||||||
* OrcKingdomFactory).
|
* OrcKingdomFactory).
|
||||||
*
|
*
|
||||||
* The example uses both concrete implementations to create a king, a castle and
|
* The example uses both concrete implementations to create a king, a castle and
|
||||||
* an army.
|
* an army.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
createKingdom(new ElfKingdomFactory());
|
createKingdom(new ElfKingdomFactory());
|
||||||
createKingdom(new OrcKingdomFactory());
|
createKingdom(new OrcKingdomFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createKingdom(KingdomFactory factory) {
|
public static void createKingdom(KingdomFactory factory) {
|
||||||
King king = factory.createKing();
|
King king = factory.createKing();
|
||||||
Castle castle = factory.createCastle();
|
Castle castle = factory.createCastle();
|
||||||
Army army = factory.createArmy();
|
Army army = factory.createArmy();
|
||||||
System.out.println("The kingdom was created.");
|
System.out.println("The kingdom was created.");
|
||||||
System.out.println(king);
|
System.out.println(king);
|
||||||
System.out.println(castle);
|
System.out.println(castle);
|
||||||
System.out.println(army);
|
System.out.println(army);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class ElfArmy implements Army {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Elven Army!";
|
return "This is the Elven Army!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class ElfCastle implements Castle {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Elven castle!";
|
return "This is the Elven castle!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class ElfKing implements King {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Elven king!";
|
return "This is the Elven king!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Concrete factory.
|
* Concrete factory.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ElfKingdomFactory implements KingdomFactory {
|
public class ElfKingdomFactory implements KingdomFactory {
|
||||||
|
|
||||||
|
@ -3,12 +3,14 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The factory interface.
|
* The factory interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface KingdomFactory {
|
public interface KingdomFactory {
|
||||||
|
|
||||||
Castle createCastle();
|
Castle createCastle();
|
||||||
|
|
||||||
King createKing();
|
King createKing();
|
||||||
|
|
||||||
Army createArmy();
|
Army createArmy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class OrcArmy implements Army {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Orcish Army!";
|
return "This is the Orcish Army!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class OrcCastle implements Castle {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Orcish castle!";
|
return "This is the Orcish castle!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public class OrcKing implements King {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "This is the Orc king!";
|
return "This is the Orc king!";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Concrete factory.
|
* Concrete factory.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class OrcKingdomFactory implements KingdomFactory {
|
public class OrcKingdomFactory implements KingdomFactory {
|
||||||
|
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* There are two variations of the Adapter pattern: The class adapter implements
|
* There are two variations of the Adapter pattern: The class adapter implements
|
||||||
* the adaptee's interface whereas the object adapter uses composition to
|
* the adaptee's interface whereas the object adapter uses composition to
|
||||||
* contain the adaptee in the adapter object. This example uses the object
|
* contain the adaptee in the adapter object. This example uses the object
|
||||||
* adapter approach.
|
* adapter approach.
|
||||||
*
|
*
|
||||||
* The Adapter (GnomeEngineer) converts the interface of the target class
|
* The Adapter (GnomeEngineer) converts the interface of the target class
|
||||||
* (GoblinGlider) into a suitable one expected by the client
|
* (GoblinGlider) into a suitable one expected by the client
|
||||||
* (GnomeEngineeringManager).
|
* (GnomeEngineeringManager).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
GnomeEngineeringManager manager = new GnomeEngineeringManager();
|
GnomeEngineeringManager manager = new GnomeEngineeringManager();
|
||||||
manager.operateDevice();
|
manager.operateDevice();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Engineers can operate devices.
|
* Engineers can operate devices.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Engineer {
|
public interface Engineer {
|
||||||
|
|
||||||
void operateDevice();
|
void operateDevice();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,14 @@ package com.iluwatar;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Adapter class. Adapts the interface of the device
|
* Adapter class. Adapts the interface of the device (GoblinGlider) into
|
||||||
* (GoblinGlider) into Engineer interface expected
|
* Engineer interface expected by the client (GnomeEngineeringManager).
|
||||||
* by the client (GnomeEngineeringManager).
|
*
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class GnomeEngineer implements Engineer {
|
public class GnomeEngineer implements Engineer {
|
||||||
|
|
||||||
private GoblinGlider glider;
|
private GoblinGlider glider;
|
||||||
|
|
||||||
public GnomeEngineer() {
|
public GnomeEngineer() {
|
||||||
glider = new GoblinGlider();
|
glider = new GoblinGlider();
|
||||||
}
|
}
|
||||||
@ -21,5 +20,5 @@ public class GnomeEngineer implements Engineer {
|
|||||||
glider.gainSpeed();
|
glider.gainSpeed();
|
||||||
glider.takeOff();
|
glider.takeOff();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,17 @@ package com.iluwatar;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* GnomeEngineering manager uses Engineer to
|
* GnomeEngineering manager uses Engineer to operate devices.
|
||||||
* operate devices.
|
*
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class GnomeEngineeringManager implements Engineer {
|
public class GnomeEngineeringManager implements Engineer {
|
||||||
|
|
||||||
private Engineer engineer;
|
private Engineer engineer;
|
||||||
|
|
||||||
public GnomeEngineeringManager() {
|
public GnomeEngineeringManager() {
|
||||||
engineer = new GnomeEngineer();
|
engineer = new GnomeEngineer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void operateDevice() {
|
public void operateDevice() {
|
||||||
engineer.operateDevice();
|
engineer.operateDevice();
|
||||||
|
@ -3,18 +3,18 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Device class (adaptee in the pattern).
|
* Device class (adaptee in the pattern).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GoblinGlider {
|
public class GoblinGlider {
|
||||||
|
|
||||||
public void attachGlider() {
|
public void attachGlider() {
|
||||||
System.out.println("Glider attached.");
|
System.out.println("Glider attached.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gainSpeed() {
|
public void gainSpeed() {
|
||||||
System.out.println("Gaining speed.");
|
System.out.println("Gaining speed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void takeOff() {
|
public void takeOff() {
|
||||||
System.out.println("Lift-off!");
|
System.out.println("Lift-off!");
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,35 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Bridge pattern both abstraction (MagicWeapon) and implementation
|
* In Bridge pattern both abstraction (MagicWeapon) and implementation
|
||||||
* (MagicWeaponImp) have their own class hierarchies. The interface of the
|
* (MagicWeaponImp) have their own class hierarchies. The interface of the
|
||||||
* implementations can be changed without affecting the clients.
|
* implementations can be changed without affecting the clients.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
|
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(
|
||||||
blindingMagicWeapon.wield();
|
new Excalibur());
|
||||||
blindingMagicWeapon.blind();
|
blindingMagicWeapon.wield();
|
||||||
blindingMagicWeapon.swing();
|
blindingMagicWeapon.blind();
|
||||||
blindingMagicWeapon.unwield();
|
blindingMagicWeapon.swing();
|
||||||
|
blindingMagicWeapon.unwield();
|
||||||
|
|
||||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||||
flyingMagicWeapon.wield();
|
new Mjollnir());
|
||||||
flyingMagicWeapon.fly();
|
flyingMagicWeapon.wield();
|
||||||
flyingMagicWeapon.swing();
|
flyingMagicWeapon.fly();
|
||||||
flyingMagicWeapon.unwield();
|
flyingMagicWeapon.swing();
|
||||||
|
flyingMagicWeapon.unwield();
|
||||||
|
|
||||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||||
soulEatingMagicWeapon.wield();
|
new Stormbringer());
|
||||||
soulEatingMagicWeapon.swing();
|
soulEatingMagicWeapon.wield();
|
||||||
soulEatingMagicWeapon.eatSoul();
|
soulEatingMagicWeapon.swing();
|
||||||
soulEatingMagicWeapon.unwield();
|
soulEatingMagicWeapon.eatSoul();
|
||||||
|
soulEatingMagicWeapon.unwield();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public class BlindingMagicWeapon extends MagicWeapon {
|
|||||||
public BlindingMagicWeaponImp getImp() {
|
public BlindingMagicWeaponImp getImp() {
|
||||||
return (BlindingMagicWeaponImp) imp;
|
return (BlindingMagicWeaponImp) imp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void wield() {
|
public void wield() {
|
||||||
getImp().wieldImp();
|
getImp().wieldImp();
|
||||||
@ -29,5 +29,5 @@ public class BlindingMagicWeapon extends MagicWeapon {
|
|||||||
public void blind() {
|
public void blind() {
|
||||||
getImp().blindImp();
|
getImp().blindImp();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ public class Excalibur extends BlindingMagicWeaponImp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void blindImp() {
|
public void blindImp() {
|
||||||
System.out.println("bright light streams from Excalibur blinding the enemy");
|
System.out
|
||||||
|
.println("bright light streams from Excalibur blinding the enemy");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@ public class FlyingMagicWeapon extends MagicWeapon {
|
|||||||
public FlyingMagicWeapon(FlyingMagicWeaponImp imp) {
|
public FlyingMagicWeapon(FlyingMagicWeaponImp imp) {
|
||||||
super(imp);
|
super(imp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlyingMagicWeaponImp getImp() {
|
public FlyingMagicWeaponImp getImp() {
|
||||||
return (FlyingMagicWeaponImp) imp;
|
return (FlyingMagicWeaponImp) imp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void wield() {
|
public void wield() {
|
||||||
getImp().wieldImp();
|
getImp().wieldImp();
|
||||||
@ -28,5 +28,5 @@ public class FlyingMagicWeapon extends MagicWeapon {
|
|||||||
public void fly() {
|
public void fly() {
|
||||||
getImp().flyImp();
|
getImp().flyImp();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,24 +3,24 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Abstraction interface.
|
* Abstraction interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class MagicWeapon {
|
public abstract class MagicWeapon {
|
||||||
|
|
||||||
protected MagicWeaponImp imp;
|
protected MagicWeaponImp imp;
|
||||||
|
|
||||||
public MagicWeapon(MagicWeaponImp imp) {
|
public MagicWeapon(MagicWeaponImp imp) {
|
||||||
this.imp = imp;
|
this.imp = imp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void wield();
|
public abstract void wield();
|
||||||
|
|
||||||
public abstract void swing();
|
public abstract void swing();
|
||||||
|
|
||||||
public abstract void unwield();
|
public abstract void unwield();
|
||||||
|
|
||||||
public MagicWeaponImp getImp() {
|
public MagicWeaponImp getImp() {
|
||||||
return imp;
|
return imp;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,14 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Implementation interface.
|
* Implementation interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class MagicWeaponImp {
|
public abstract class MagicWeaponImp {
|
||||||
|
|
||||||
public abstract void wieldImp();
|
public abstract void wieldImp();
|
||||||
|
|
||||||
public abstract void swingImp();
|
public abstract void swingImp();
|
||||||
|
|
||||||
public abstract void unwieldImp();
|
public abstract void unwieldImp();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ public class Mjollnir extends FlyingMagicWeaponImp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flyImp() {
|
public void flyImp() {
|
||||||
System.out.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
System.out
|
||||||
|
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,12 @@ public class SoulEatingMagicWeapon extends MagicWeapon {
|
|||||||
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImp imp) {
|
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImp imp) {
|
||||||
super(imp);
|
super(imp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SoulEatingMagicWeaponImp getImp() {
|
public SoulEatingMagicWeaponImp getImp() {
|
||||||
return (SoulEatingMagicWeaponImp) imp;
|
return (SoulEatingMagicWeaponImp) imp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void wield() {
|
public void wield() {
|
||||||
getImp().wieldImp();
|
getImp().wieldImp();
|
||||||
@ -29,5 +29,5 @@ public class SoulEatingMagicWeapon extends MagicWeapon {
|
|||||||
public void eatSoul() {
|
public void eatSoul() {
|
||||||
getImp().eatSoulImp();
|
getImp().eatSoulImp();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,5 @@ package com.iluwatar;
|
|||||||
public abstract class SoulEatingMagicWeaponImp extends MagicWeaponImp {
|
public abstract class SoulEatingMagicWeaponImp extends MagicWeaponImp {
|
||||||
|
|
||||||
public abstract void eatSoulImp();
|
public abstract void eatSoulImp();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,41 +3,36 @@ package com.iluwatar;
|
|||||||
import com.iluwatar.Hero.HeroBuilder;
|
import com.iluwatar.Hero.HeroBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* This is the Builder pattern variation as described by Joshua Bloch in
|
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||||
* Effective Java 2nd Edition.
|
* Effective Java 2nd Edition.
|
||||||
*
|
*
|
||||||
* We want to build Hero objects, but its construction is complex because of the
|
* We want to build Hero objects, but its construction is complex because of the
|
||||||
* many parameters needed. To aid the user we introduce HeroBuilder class.
|
* many parameters needed. To aid the user we introduce HeroBuilder class.
|
||||||
* HeroBuilder takes the minimum parameters to build Hero object in its
|
* HeroBuilder takes the minimum parameters to build Hero object in its
|
||||||
* constructor. After that additional configuration for the Hero object can be
|
* constructor. After that additional configuration for the Hero object can be
|
||||||
* done using the fluent HeroBuilder interface. When configuration is ready the
|
* done using the fluent HeroBuilder interface. When configuration is ready the
|
||||||
* build method is called to receive the final Hero object.
|
* build method is called to receive the final Hero object.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||||
.withHairColor(HairColor.BLACK)
|
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||||
.withWeapon(Weapon.DAGGER)
|
.build();
|
||||||
.build();
|
System.out.println(mage);
|
||||||
System.out.println(mage);
|
|
||||||
|
|
||||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||||
.withHairColor(HairColor.BLOND)
|
.withHairColor(HairColor.BLOND)
|
||||||
.withHairType(HairType.LONG_CURLY)
|
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||||
.withArmor(Armor.CHAIN_MAIL)
|
.withWeapon(Weapon.SWORD).build();
|
||||||
.withWeapon(Weapon.SWORD)
|
System.out.println(warrior);
|
||||||
.build();
|
|
||||||
System.out.println(warrior);
|
|
||||||
|
|
||||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||||
.withHairType(HairType.BALD)
|
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||||
.withWeapon(Weapon.BOW)
|
System.out.println(thief);
|
||||||
.build();
|
|
||||||
System.out.println(thief);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,17 +3,25 @@ package com.iluwatar;
|
|||||||
public enum Armor {
|
public enum Armor {
|
||||||
|
|
||||||
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL;
|
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case CLOTHES: s = "clothes"; break;
|
case CLOTHES:
|
||||||
case LEATHER: s = "leather armor"; break;
|
s = "clothes";
|
||||||
case CHAIN_MAIL: s = "chain mail"; break;
|
break;
|
||||||
case PLATE_MAIL: s = "plate mail"; break;
|
case LEATHER:
|
||||||
|
s = "leather armor";
|
||||||
|
break;
|
||||||
|
case CHAIN_MAIL:
|
||||||
|
s = "chain mail";
|
||||||
|
break;
|
||||||
|
case PLATE_MAIL:
|
||||||
|
s = "plate mail";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,28 @@ package com.iluwatar;
|
|||||||
public enum HairColor {
|
public enum HairColor {
|
||||||
|
|
||||||
WHITE, BLOND, RED, BROWN, BLACK;
|
WHITE, BLOND, RED, BROWN, BLACK;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case WHITE: s = "white"; break;
|
case WHITE:
|
||||||
case BLOND: s = "blond"; break;
|
s = "white";
|
||||||
case RED: s = "red"; break;
|
break;
|
||||||
case BROWN: s = "brown"; break;
|
case BLOND:
|
||||||
case BLACK: s = "black"; break;
|
s = "blond";
|
||||||
|
break;
|
||||||
|
case RED:
|
||||||
|
s = "red";
|
||||||
|
break;
|
||||||
|
case BROWN:
|
||||||
|
s = "brown";
|
||||||
|
break;
|
||||||
|
case BLACK:
|
||||||
|
s = "black";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,24 @@ public enum HairType {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case BALD: s = "bold"; break;
|
case BALD:
|
||||||
case SHORT: s = "short"; break;
|
s = "bold";
|
||||||
case CURLY: s = "curly"; break;
|
break;
|
||||||
case LONG_STRAIGHT: s = "long straight"; break;
|
case SHORT:
|
||||||
case LONG_CURLY: s = "long curly"; break;
|
s = "short";
|
||||||
|
break;
|
||||||
|
case CURLY:
|
||||||
|
s = "curly";
|
||||||
|
break;
|
||||||
|
case LONG_STRAIGHT:
|
||||||
|
s = "long straight";
|
||||||
|
break;
|
||||||
|
case LONG_CURLY:
|
||||||
|
s = "long curly";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The class with many parameters.
|
* The class with many parameters.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Hero {
|
public class Hero {
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ public class Hero {
|
|||||||
private final HairColor hairColor;
|
private final HairColor hairColor;
|
||||||
private final Armor armor;
|
private final Armor armor;
|
||||||
private final Weapon weapon;
|
private final Weapon weapon;
|
||||||
|
|
||||||
public Profession getProfession() {
|
public Profession getProfession() {
|
||||||
return profession;
|
return profession;
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ public class Hero {
|
|||||||
public Weapon getWeapon() {
|
public Weapon getWeapon() {
|
||||||
return weapon;
|
return weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ public class Hero {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The builder class.
|
* The builder class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static class HeroBuilder {
|
public static class HeroBuilder {
|
||||||
|
|
||||||
@ -91,20 +91,21 @@ public class Hero {
|
|||||||
private HairColor hairColor;
|
private HairColor hairColor;
|
||||||
private Armor armor;
|
private Armor armor;
|
||||||
private Weapon weapon;
|
private Weapon weapon;
|
||||||
|
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public HeroBuilder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new NullPointerException("profession and name can not be null");
|
throw new NullPointerException(
|
||||||
|
"profession and name can not be null");
|
||||||
}
|
}
|
||||||
this.profession = profession;
|
this.profession = profession;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairType(HairType hairType) {
|
public HeroBuilder withHairType(HairType hairType) {
|
||||||
this.hairType = hairType;
|
this.hairType = hairType;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairColor(HairColor hairColor) {
|
public HeroBuilder withHairColor(HairColor hairColor) {
|
||||||
this.hairColor = hairColor;
|
this.hairColor = hairColor;
|
||||||
return this;
|
return this;
|
||||||
@ -114,12 +115,12 @@ public class Hero {
|
|||||||
this.armor = armor;
|
this.armor = armor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withWeapon(Weapon weapon) {
|
public HeroBuilder withWeapon(Weapon weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Hero build() {
|
public Hero build() {
|
||||||
return new Hero(this);
|
return new Hero(this);
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,27 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
public enum Profession {
|
public enum Profession {
|
||||||
|
|
||||||
WARRIOR, THIEF, MAGE, PRIEST;
|
WARRIOR, THIEF, MAGE, PRIEST;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case WARRIOR: s = "Warrior"; break;
|
case WARRIOR:
|
||||||
case THIEF: s = "Thief"; break;
|
s = "Warrior";
|
||||||
case MAGE: s = "Mage"; break;
|
break;
|
||||||
case PRIEST: s = "Priest"; break;
|
case THIEF:
|
||||||
|
s = "Thief";
|
||||||
|
break;
|
||||||
|
case MAGE:
|
||||||
|
s = "Mage";
|
||||||
|
break;
|
||||||
|
case PRIEST:
|
||||||
|
s = "Priest";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,28 @@ package com.iluwatar;
|
|||||||
public enum Weapon {
|
public enum Weapon {
|
||||||
|
|
||||||
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case DAGGER: s = "dagger"; break;
|
case DAGGER:
|
||||||
case SWORD: s = "sword"; break;
|
s = "dagger";
|
||||||
case AXE: s = "axe"; break;
|
break;
|
||||||
case WARHAMMER: s = "warhammer"; break;
|
case SWORD:
|
||||||
case BOW: s = "bow"; break;
|
s = "sword";
|
||||||
|
break;
|
||||||
|
case AXE:
|
||||||
|
s = "axe";
|
||||||
|
break;
|
||||||
|
case WARHAMMER:
|
||||||
|
s = "warhammer";
|
||||||
|
break;
|
||||||
|
case BOW:
|
||||||
|
s = "bow";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,22 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Chain of Responsibility organizes request handlers (RequestHandler) into a
|
* Chain of Responsibility organizes request handlers (RequestHandler) into a
|
||||||
* chain where each handler has a chance to act on the request on its turn. In
|
* chain where each handler has a chance to act on the request on its turn. In
|
||||||
* this example the king (OrcKing) makes requests and the military orcs
|
* this example the king (OrcKing) makes requests and the military orcs
|
||||||
* (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain.
|
* (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
OrcKing king = new OrcKing();
|
OrcKing king = new OrcKing();
|
||||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
||||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
"torture prisoner"));
|
||||||
|
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ public class OrcCommander extends RequestHandler {
|
|||||||
public OrcCommander(RequestHandler handler) {
|
public OrcCommander(RequestHandler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
||||||
|
@ -3,22 +3,22 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Makes requests that are handled by the chain.
|
* Makes requests that are handled by the chain.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class OrcKing {
|
public class OrcKing {
|
||||||
|
|
||||||
RequestHandler chain;
|
RequestHandler chain;
|
||||||
|
|
||||||
public OrcKing() {
|
public OrcKing() {
|
||||||
buildChain();
|
buildChain();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildChain() {
|
private void buildChain() {
|
||||||
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void makeRequest(Request req) {
|
public void makeRequest(Request req) {
|
||||||
chain.handleRequest(req);
|
chain.handleRequest(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ public class OrcOfficer extends RequestHandler {
|
|||||||
public OrcOfficer(RequestHandler handler) {
|
public OrcOfficer(RequestHandler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
public abstract class RequestHandler {
|
public abstract class RequestHandler {
|
||||||
|
|
||||||
private RequestHandler next;
|
private RequestHandler next;
|
||||||
|
|
||||||
public RequestHandler(RequestHandler next) {
|
public RequestHandler(RequestHandler next) {
|
||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleRequest(Request req) {
|
public void handleRequest(Request req) {
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
next.handleRequest(req);
|
next.handleRequest(req);
|
||||||
|
@ -2,8 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum RequestType {
|
public enum RequestType {
|
||||||
|
|
||||||
DEFEND_CASTLE,
|
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||||
TORTURE_PRISONER,
|
|
||||||
COLLECT_TAX
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Command pattern actions are objects that can be executed and undone. The
|
* In Command pattern actions are objects that can be executed and undone. The
|
||||||
* commands in this example are spells cast by the wizard on the goblin.
|
* commands in this example are spells cast by the wizard on the goblin.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Wizard wizard = new Wizard();
|
Wizard wizard = new Wizard();
|
||||||
Goblin goblin = new Goblin();
|
Goblin goblin = new Goblin();
|
||||||
|
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
|
|
||||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
wizard.undoLastSpell();
|
wizard.undoLastSpell();
|
||||||
goblin.printStatus();
|
goblin.printStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,15 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Interface for spells.
|
* Interface for spells.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
|
|
||||||
public abstract void execute(Target target);
|
public abstract void execute(Target target);
|
||||||
|
|
||||||
public abstract void undo();
|
public abstract void undo();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ public class Goblin extends Target {
|
|||||||
this.setSize(Size.NORMAL);
|
this.setSize(Size.NORMAL);
|
||||||
this.setVisibility(Visibility.VISIBLE);
|
this.setVisibility(Visibility.VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Goblin";
|
return "Goblin";
|
||||||
|
@ -3,11 +3,11 @@ package com.iluwatar;
|
|||||||
public class InvisibilitySpell extends Command {
|
public class InvisibilitySpell extends Command {
|
||||||
|
|
||||||
private Target target;
|
private Target target;
|
||||||
|
|
||||||
public InvisibilitySpell() {
|
public InvisibilitySpell() {
|
||||||
target = null;
|
target = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Target target) {
|
public void execute(Target target) {
|
||||||
target.setVisibility(Visibility.INVISIBLE);
|
target.setVisibility(Visibility.INVISIBLE);
|
||||||
|
@ -3,14 +3,14 @@ package com.iluwatar;
|
|||||||
public class ShrinkSpell extends Command {
|
public class ShrinkSpell extends Command {
|
||||||
|
|
||||||
private Size oldSize;
|
private Size oldSize;
|
||||||
|
|
||||||
private Target target;
|
private Target target;
|
||||||
|
|
||||||
public ShrinkSpell() {
|
public ShrinkSpell() {
|
||||||
oldSize = null;
|
oldSize = null;
|
||||||
target = null;
|
target = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Target target) {
|
public void execute(Target target) {
|
||||||
oldSize = target.getSize();
|
oldSize = target.getSize();
|
||||||
|
@ -2,15 +2,13 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum Size {
|
public enum Size {
|
||||||
|
|
||||||
SMALL,
|
SMALL, NORMAL, LARGE;
|
||||||
NORMAL,
|
|
||||||
LARGE;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
String s = "";
|
String s = "";
|
||||||
|
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case LARGE:
|
case LARGE:
|
||||||
s = "large";
|
s = "large";
|
||||||
@ -27,5 +25,4 @@ public enum Size {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ public abstract class Target {
|
|||||||
private Size size;
|
private Size size;
|
||||||
|
|
||||||
private Visibility visibility;
|
private Visibility visibility;
|
||||||
|
|
||||||
public Size getSize() {
|
public Size getSize() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -24,8 +24,9 @@ public abstract class Target {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
public void printStatus() {
|
public void printStatus() {
|
||||||
System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility()));
|
System.out.println(String.format("%s, size=%s visibility=%s", this,
|
||||||
|
getSize(), getVisibility()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,13 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum Visibility {
|
public enum Visibility {
|
||||||
|
|
||||||
VISIBLE,
|
VISIBLE, INVISIBLE;
|
||||||
INVISIBLE;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
String s = "";
|
String s = "";
|
||||||
|
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case INVISIBLE:
|
case INVISIBLE:
|
||||||
s = "invisible";
|
s = "invisible";
|
||||||
@ -19,7 +18,7 @@ public enum Visibility {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,13 @@ package com.iluwatar;
|
|||||||
public class Wizard extends Target {
|
public class Wizard extends Target {
|
||||||
|
|
||||||
private Command previousSpell;
|
private Command previousSpell;
|
||||||
|
|
||||||
public Wizard() {
|
public Wizard() {
|
||||||
this.setSize(Size.NORMAL);
|
this.setSize(Size.NORMAL);
|
||||||
this.setVisibility(Visibility.VISIBLE);
|
this.setVisibility(Visibility.VISIBLE);
|
||||||
previousSpell = null;
|
previousSpell = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void castSpell(Command command, Target target) {
|
public void castSpell(Command command, Target target) {
|
||||||
System.out.println(this + " casts " + command + " at " + target);
|
System.out.println(this + " casts " + command + " at " + target);
|
||||||
command.execute(target);
|
command.execute(target);
|
||||||
@ -22,10 +22,10 @@ public class Wizard extends Target {
|
|||||||
previousSpell.undo();
|
previousSpell.undo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Wizard";
|
return "Wizard";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* With Composite we can treat tree hierarchies of objects with uniform
|
* With Composite we can treat tree hierarchies of objects with uniform
|
||||||
* interface (LetterComposite). In this example we have sentences composed of
|
* interface (LetterComposite). In this example we have sentences composed of
|
||||||
* words composed of letters.
|
* words composed of letters.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Message from the orcs: ");
|
System.out.println("Message from the orcs: ");
|
||||||
|
|
||||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||||
orcMessage.print();
|
orcMessage.print();
|
||||||
|
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
|
|
||||||
System.out.println("Message from the elves: ");
|
System.out.println("Message from the elves: ");
|
||||||
|
|
||||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||||
elfMessage.print();
|
elfMessage.print();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Composite interface.
|
* Composite interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class LetterComposite {
|
public abstract class LetterComposite {
|
||||||
|
|
||||||
@ -15,18 +15,18 @@ public abstract class LetterComposite {
|
|||||||
public void add(LetterComposite letter) {
|
public void add(LetterComposite letter) {
|
||||||
children.add(letter);
|
children.add(letter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int count() {
|
public int count() {
|
||||||
return children.size();
|
return children.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void printThisBefore();
|
protected abstract void printThisBefore();
|
||||||
|
|
||||||
protected abstract void printThisAfter();
|
protected abstract void printThisAfter();
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
printThisBefore();
|
printThisBefore();
|
||||||
for (LetterComposite letter: children) {
|
for (LetterComposite letter : children) {
|
||||||
letter.print();
|
letter.print();
|
||||||
}
|
}
|
||||||
printThisAfter();
|
printThisAfter();
|
||||||
|
@ -7,36 +7,47 @@ import java.util.List;
|
|||||||
public class Messenger {
|
public class Messenger {
|
||||||
|
|
||||||
LetterComposite messageFromOrcs() {
|
LetterComposite messageFromOrcs() {
|
||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||||
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter('p'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
|
new Letter('i'), new Letter('p'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||||
|
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'),
|
||||||
|
new Letter('y'))));
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LetterComposite messageFromElves() {
|
LetterComposite messageFromElves() {
|
||||||
|
|
||||||
List<Word> words = new ArrayList<Word>();
|
List<Word> words = new ArrayList<Word>();
|
||||||
|
|
||||||
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))));
|
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))));
|
new Letter('c'), new Letter('h'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'), new Letter('s'))));
|
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))));
|
new Letter('n'), new Letter('d'))));
|
||||||
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))));
|
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'),
|
||||||
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'), new Letter('h'))));
|
new Letter('u'), new Letter('r'), new Letter('s'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'),
|
||||||
|
new Letter('o'), new Letter('m'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'),
|
||||||
|
new Letter('u'), new Letter('r'))));
|
||||||
|
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'),
|
||||||
|
new Letter('u'), new Letter('t'), new Letter('h'))));
|
||||||
|
|
||||||
return new Sentence(words);
|
return new Sentence(words);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@ import java.util.List;
|
|||||||
public class Sentence extends LetterComposite {
|
public class Sentence extends LetterComposite {
|
||||||
|
|
||||||
public Sentence(List<Word> words) {
|
public Sentence(List<Word> words) {
|
||||||
for (Word w: words) {
|
for (Word w : words) {
|
||||||
this.add(w);
|
this.add(w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printThisBefore() {
|
protected void printThisBefore() {
|
||||||
// nop
|
// nop
|
||||||
@ -20,5 +20,4 @@ public class Sentence extends LetterComposite {
|
|||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@ import java.util.List;
|
|||||||
public class Word extends LetterComposite {
|
public class Word extends LetterComposite {
|
||||||
|
|
||||||
public Word(List<Letter> letters) {
|
public Word(List<Letter> letters) {
|
||||||
for (Letter l: letters) {
|
for (Letter l : letters) {
|
||||||
this.add(l);
|
this.add(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printThisBefore() {
|
protected void printThisBefore() {
|
||||||
System.out.print(" ");
|
System.out.print(" ");
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Decorator pattern is more flexible alternative to subclassing. The decorator
|
* Decorator pattern is more flexible alternative to subclassing. The decorator
|
||||||
* class implements the same interface as the target and uses composition to
|
* class implements the same interface as the target and uses composition to
|
||||||
* "decorate" calls to the target.
|
* "decorate" calls to the target.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
System.out.println("A simple looking troll approaches.");
|
System.out.println("A simple looking troll approaches.");
|
||||||
Hostile troll = new Troll();
|
Hostile troll = new Troll();
|
||||||
troll.attack();
|
troll.attack();
|
||||||
troll.fleeBattle();
|
troll.fleeBattle();
|
||||||
|
|
||||||
System.out.println("\nA smart looking troll surprises you.");
|
System.out.println("\nA smart looking troll surprises you.");
|
||||||
Hostile smart = new SmartTroll(new Troll());
|
Hostile smart = new SmartTroll(new Troll());
|
||||||
smart.attack();
|
smart.attack();
|
||||||
smart.fleeBattle();
|
smart.fleeBattle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.iluwatar;
|
|||||||
public interface Hostile {
|
public interface Hostile {
|
||||||
|
|
||||||
void attack();
|
void attack();
|
||||||
|
|
||||||
void fleeBattle();
|
void fleeBattle();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ public class SmartTroll implements Hostile {
|
|||||||
public SmartTroll(Hostile decorated) {
|
public SmartTroll(Hostile decorated) {
|
||||||
this.decorated = decorated;
|
this.decorated = decorated;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attack() {
|
public void attack() {
|
||||||
System.out.println("The troll throws a rock at you!");
|
System.out.println("The troll throws a rock at you!");
|
||||||
@ -19,5 +19,5 @@ public class SmartTroll implements Hostile {
|
|||||||
System.out.println("The troll calls for help!");
|
System.out.println("The troll calls for help!");
|
||||||
decorated.fleeBattle();
|
decorated.fleeBattle();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ public class Troll implements Hostile {
|
|||||||
public void attack() {
|
public void attack() {
|
||||||
System.out.println("The troll swings at you with a club!");
|
System.out.println("The troll swings at you with a club!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fleeBattle() {
|
public void fleeBattle() {
|
||||||
System.out.println("The troll shrieks in horror and runs away!");
|
System.out.println("The troll shrieks in horror and runs away!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import java.util.concurrent.ExecutorService;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Inventory we store the items with a given size. However, we do not store
|
* In Inventory we store the items with a given size. However, we do not store
|
||||||
* more items than the inventory size. To address concurrent access problems we
|
* more items than the inventory size. To address concurrent access problems we
|
||||||
* use double checked locking to add item to inventory. In this method, the
|
* use double checked locking to add item to inventory. In this method, the
|
||||||
@ -12,16 +12,17 @@ import java.util.concurrent.Executors;
|
|||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
final Inventory inventory = new Inventory(1000);
|
final Inventory inventory = new Inventory(1000);
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
executorService.execute(new Runnable() {
|
executorService.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (inventory.addItem(new Item()));
|
while (inventory.addItem(new Item()))
|
||||||
}
|
;
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,8 @@ import java.util.List;
|
|||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
|
||||||
public class Inventory {
|
public class Inventory {
|
||||||
|
|
||||||
private int inventorySize;
|
private int inventorySize;
|
||||||
private List<Item> items;
|
private List<Item> items;
|
||||||
private Lock lock = new ReentrantLock();
|
private Lock lock = new ReentrantLock();
|
||||||
@ -16,17 +15,17 @@ public class Inventory {
|
|||||||
this.inventorySize = inventorySize;
|
this.inventorySize = inventorySize;
|
||||||
this.items = new ArrayList<Item>(inventorySize);
|
this.items = new ArrayList<Item>(inventorySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addItem(Item item){
|
public boolean addItem(Item item) {
|
||||||
if(items.size()<inventorySize){
|
if (items.size() < inventorySize) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try{
|
try {
|
||||||
if(items.size()<inventorySize){
|
if (items.size() < inventorySize) {
|
||||||
items.add(item);
|
items.add(item);
|
||||||
System.out.println(Thread.currentThread());
|
System.out.println(Thread.currentThread());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}finally{
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Facade (DwarvenGoldmineFacade) provides simpler interface to subsystem.
|
* Facade (DwarvenGoldmineFacade) provides simpler interface to subsystem.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
|
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
|
||||||
facade.startNewDay();
|
facade.startNewDay();
|
||||||
facade.digOutGold();
|
facade.digOutGold();
|
||||||
facade.endDay();
|
facade.endDay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,32 +6,32 @@ import java.util.List;
|
|||||||
public class DwarvenGoldmineFacade {
|
public class DwarvenGoldmineFacade {
|
||||||
|
|
||||||
List<DwarvenMineWorker> workers;
|
List<DwarvenMineWorker> workers;
|
||||||
|
|
||||||
public DwarvenGoldmineFacade() {
|
public DwarvenGoldmineFacade() {
|
||||||
workers = new ArrayList<>();
|
workers = new ArrayList<>();
|
||||||
workers.add(new DwarvenGoldDigger());
|
workers.add(new DwarvenGoldDigger());
|
||||||
workers.add(new DwarvenCartOperator());
|
workers.add(new DwarvenCartOperator());
|
||||||
workers.add(new DwarvenTunnelDigger());
|
workers.add(new DwarvenTunnelDigger());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startNewDay() {
|
public void startNewDay() {
|
||||||
for (DwarvenMineWorker worker: workers) {
|
for (DwarvenMineWorker worker : workers) {
|
||||||
worker.wakeUp();
|
worker.wakeUp();
|
||||||
worker.goToMine();
|
worker.goToMine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void digOutGold() {
|
public void digOutGold() {
|
||||||
for (DwarvenMineWorker worker: workers) {
|
for (DwarvenMineWorker worker : workers) {
|
||||||
worker.work();
|
worker.work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endDay() {
|
public void endDay() {
|
||||||
for (DwarvenMineWorker worker: workers) {
|
for (DwarvenMineWorker worker : workers) {
|
||||||
worker.goHome();
|
worker.goHome();
|
||||||
worker.goToSleep();
|
worker.goToSleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,21 +5,21 @@ public abstract class DwarvenMineWorker {
|
|||||||
public void goToSleep() {
|
public void goToSleep() {
|
||||||
System.out.println(name() + " goes to sleep.");
|
System.out.println(name() + " goes to sleep.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void wakeUp() {
|
public void wakeUp() {
|
||||||
System.out.println(name() + " wakes up.");
|
System.out.println(name() + " wakes up.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goHome() {
|
public void goHome() {
|
||||||
System.out.println(name() + " goes home.");
|
System.out.println(name() + " goes home.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goToMine() {
|
public void goToMine() {
|
||||||
System.out.println(name() + " goes to the mine.");
|
System.out.println(name() + " goes to the mine.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void work();
|
public abstract void work();
|
||||||
|
|
||||||
public abstract String name();
|
public abstract String name();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Factory Method we have an interface (Blacksmith) with a method for
|
* In Factory Method we have an interface (Blacksmith) with a method for
|
||||||
* creating objects (manufactureWeapon). The concrete subclasses (OrcBlacksmith,
|
* creating objects (manufactureWeapon). The concrete subclasses (OrcBlacksmith,
|
||||||
* ElfBlacksmith) then override the method to produce objects of their liking.
|
* ElfBlacksmith) then override the method to produce objects of their liking.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Blacksmith blacksmith;
|
Blacksmith blacksmith;
|
||||||
Weapon weapon;
|
Weapon weapon;
|
||||||
|
|
||||||
blacksmith = new OrcBlacksmith();
|
blacksmith = new OrcBlacksmith();
|
||||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||||
System.out.println(weapon);
|
System.out.println(weapon);
|
||||||
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||||
System.out.println(weapon);
|
System.out.println(weapon);
|
||||||
|
|
||||||
blacksmith = new ElfBlacksmith();
|
blacksmith = new ElfBlacksmith();
|
||||||
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
||||||
System.out.println(weapon);
|
System.out.println(weapon);
|
||||||
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||||
System.out.println(weapon);
|
System.out.println(weapon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The interface containing method for producing objects.
|
* The interface containing method for producing objects.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Blacksmith {
|
public interface Blacksmith {
|
||||||
|
|
||||||
Weapon manufactureWeapon(WeaponType weaponType);
|
Weapon manufactureWeapon(WeaponType weaponType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Concrete subclass for creating new objects.
|
* Concrete subclass for creating new objects.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ElfBlacksmith implements Blacksmith {
|
public class ElfBlacksmith implements Blacksmith {
|
||||||
|
|
||||||
|
@ -12,5 +12,5 @@ public class ElfWeapon implements Weapon {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Elven " + weaponType;
|
return "Elven " + weaponType;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Concrete subclass for creating new objects.
|
* Concrete subclass for creating new objects.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class OrcBlacksmith implements Blacksmith {
|
public class OrcBlacksmith implements Blacksmith {
|
||||||
|
|
||||||
|
@ -12,5 +12,5 @@ public class OrcWeapon implements Weapon {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Orcish " + weaponType;
|
return "Orcish " + weaponType;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,18 @@ public enum WeaponType {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case SHORT_SWORD: s = "short sword"; break;
|
case SHORT_SWORD:
|
||||||
case SPEAR: s = "spear"; break;
|
s = "short sword";
|
||||||
case AXE: s = "axe"; break;
|
break;
|
||||||
|
case SPEAR:
|
||||||
|
s = "spear";
|
||||||
|
break;
|
||||||
|
case AXE:
|
||||||
|
s = "axe";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The class that needs many objects.
|
* The class that needs many objects.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AlchemistShop {
|
public class AlchemistShop {
|
||||||
|
|
||||||
List<Potion> topShelf;
|
List<Potion> topShelf;
|
||||||
List<Potion> bottomShelf;
|
List<Potion> bottomShelf;
|
||||||
|
|
||||||
public AlchemistShop() {
|
public AlchemistShop() {
|
||||||
topShelf = new ArrayList<>();
|
topShelf = new ArrayList<>();
|
||||||
bottomShelf = new ArrayList<>();
|
bottomShelf = new ArrayList<>();
|
||||||
@ -20,9 +20,9 @@ public class AlchemistShop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void fillShelves() {
|
private void fillShelves() {
|
||||||
|
|
||||||
PotionFactory factory = new PotionFactory();
|
PotionFactory factory = new PotionFactory();
|
||||||
|
|
||||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||||
@ -31,27 +31,27 @@ public class AlchemistShop {
|
|||||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||||
|
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enumerate() {
|
public void enumerate() {
|
||||||
|
|
||||||
System.out.println("Enumerating top shelf potions\n");
|
System.out.println("Enumerating top shelf potions\n");
|
||||||
|
|
||||||
for (Potion p: topShelf) {
|
for (Potion p : topShelf) {
|
||||||
p.drink();
|
p.drink();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||||
|
|
||||||
for (Potion p: bottomShelf) {
|
for (Potion p : bottomShelf) {
|
||||||
p.drink();
|
p.drink();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Flyweight (PotionFactory) is useful when there is plethora of objects
|
* Flyweight (PotionFactory) is useful when there is plethora of objects
|
||||||
* (Potion). It provides means to decrease resource usage by sharing object
|
* (Potion). It provides means to decrease resource usage by sharing object
|
||||||
* instances.
|
* instances.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AlchemistShop alchemistShop = new AlchemistShop();
|
AlchemistShop alchemistShop = new AlchemistShop();
|
||||||
alchemistShop.enumerate();
|
alchemistShop.enumerate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class HealingPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel healed. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class HolyWaterPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel blessed. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class InvisibilityPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You become invisible. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class PoisonPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("Urgh! This is poisonous. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Interface for objects.
|
* Interface for objects.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Potion {
|
public interface Potion {
|
||||||
|
|
||||||
public void drink();
|
public void drink();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,16 @@ import java.util.EnumMap;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Flyweight.
|
* Flyweight.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class PotionFactory {
|
public class PotionFactory {
|
||||||
|
|
||||||
private EnumMap<PotionType, Potion> potions;
|
private EnumMap<PotionType, Potion> potions;
|
||||||
|
|
||||||
public PotionFactory() {
|
public PotionFactory() {
|
||||||
potions = new EnumMap<>(PotionType.class);
|
potions = new EnumMap<>(PotionType.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
Potion createPotion(PotionType type) {
|
Potion createPotion(PotionType type) {
|
||||||
Potion potion = potions.get(type);
|
Potion potion = potions.get(type);
|
||||||
if (potion == null) {
|
if (potion == null) {
|
||||||
@ -45,5 +45,5 @@ public class PotionFactory {
|
|||||||
}
|
}
|
||||||
return potion;
|
return potion;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum PotionType {
|
public enum PotionType {
|
||||||
|
|
||||||
HEALING,
|
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;
|
||||||
INVISIBILITY,
|
|
||||||
STRENGTH,
|
|
||||||
HOLY_WATER,
|
|
||||||
POISON;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ public class StrengthPotion implements Potion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drink() {
|
public void drink() {
|
||||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
System.out.println("You feel strong. (Potion="
|
||||||
|
+ System.identityHashCode(this) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,64 +3,70 @@ package com.iluwatar;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Interpreter pattern breaks sentences into expressions (Expression) that can
|
* Interpreter pattern breaks sentences into expressions (Expression) that can
|
||||||
* be evaluated and as a whole form the result.
|
* be evaluated and as a whole form the result.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||||
* This sample uses postfix, where operator comes after the operands
|
* This sample uses postfix, where operator comes after the operands
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String tokenString = "4 3 2 - 1 + *";
|
String tokenString = "4 3 2 - 1 + *";
|
||||||
Stack<Expression> stack = new Stack<>();
|
Stack<Expression> stack = new Stack<>();
|
||||||
|
|
||||||
String[] tokenList = tokenString.split(" ");
|
String[] tokenList = tokenString.split(" ");
|
||||||
for (String s : tokenList) {
|
for (String s : tokenList) {
|
||||||
if (isOperator(s)) {
|
if (isOperator(s)) {
|
||||||
Expression rightExpression = stack.pop();
|
Expression rightExpression = stack.pop();
|
||||||
Expression leftExpression = stack.pop();
|
Expression leftExpression = stack.pop();
|
||||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
System.out
|
||||||
leftExpression.interpret(), rightExpression.interpret()));
|
.println(String.format(
|
||||||
Expression operator = getOperatorInstance(s, leftExpression,
|
"popped from stack left: %d right: %d",
|
||||||
rightExpression);
|
leftExpression.interpret(),
|
||||||
System.out.println(String.format("operator: %s", operator));
|
rightExpression.interpret()));
|
||||||
int result = operator.interpret();
|
Expression operator = getOperatorInstance(s, leftExpression,
|
||||||
NumberExpression resultExpression = new NumberExpression(result);
|
rightExpression);
|
||||||
stack.push(resultExpression);
|
System.out.println(String.format("operator: %s", operator));
|
||||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
int result = operator.interpret();
|
||||||
} else {
|
NumberExpression resultExpression = new NumberExpression(result);
|
||||||
Expression i = new NumberExpression(s);
|
stack.push(resultExpression);
|
||||||
stack.push(i);
|
System.out.println(String.format("push result to stack: %d",
|
||||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
resultExpression.interpret()));
|
||||||
}
|
} else {
|
||||||
}
|
Expression i = new NumberExpression(s);
|
||||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
stack.push(i);
|
||||||
}
|
System.out.println(String.format("push to stack: %d",
|
||||||
|
i.interpret()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out
|
||||||
|
.println(String.format("result: %d", stack.pop().interpret()));
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isOperator(String s) {
|
public static boolean isOperator(String s) {
|
||||||
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Expression getOperatorInstance(String s, Expression left,
|
public static Expression getOperatorInstance(String s, Expression left,
|
||||||
Expression right) {
|
Expression right) {
|
||||||
switch (s) {
|
switch (s) {
|
||||||
case "+":
|
case "+":
|
||||||
return new PlusExpression(left, right);
|
return new PlusExpression(left, right);
|
||||||
case "-":
|
case "-":
|
||||||
return new MinusExpression(left, right);
|
return new MinusExpression(left, right);
|
||||||
case "*":
|
case "*":
|
||||||
return new MultiplyExpression(left, right);
|
return new MultiplyExpression(left, right);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
public abstract class Expression {
|
public abstract class Expression {
|
||||||
|
|
||||||
public abstract int interpret();
|
public abstract int interpret();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ public class MinusExpression extends Expression {
|
|||||||
this.leftExpression = leftExpression;
|
this.leftExpression = leftExpression;
|
||||||
this.rightExpression = rightExpression;
|
this.rightExpression = rightExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int interpret() {
|
public int interpret() {
|
||||||
return leftExpression.interpret() - rightExpression.interpret();
|
return leftExpression.interpret() - rightExpression.interpret();
|
||||||
|
@ -5,11 +5,12 @@ public class MultiplyExpression extends Expression {
|
|||||||
private Expression leftExpression;
|
private Expression leftExpression;
|
||||||
private Expression rightExpression;
|
private Expression rightExpression;
|
||||||
|
|
||||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
public MultiplyExpression(Expression leftExpression,
|
||||||
|
Expression rightExpression) {
|
||||||
this.leftExpression = leftExpression;
|
this.leftExpression = leftExpression;
|
||||||
this.rightExpression = rightExpression;
|
this.rightExpression = rightExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int interpret() {
|
public int interpret() {
|
||||||
return leftExpression.interpret() * rightExpression.interpret();
|
return leftExpression.interpret() * rightExpression.interpret();
|
||||||
|
@ -7,11 +7,11 @@ public class NumberExpression extends Expression {
|
|||||||
public NumberExpression(int number) {
|
public NumberExpression(int number) {
|
||||||
this.number = number;
|
this.number = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NumberExpression(String s) {
|
public NumberExpression(String s) {
|
||||||
this.number = Integer.parseInt(s);
|
this.number = Integer.parseInt(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int interpret() {
|
public int interpret() {
|
||||||
return number;
|
return number;
|
||||||
|
@ -9,7 +9,7 @@ public class PlusExpression extends Expression {
|
|||||||
this.leftExpression = leftExpression;
|
this.leftExpression = leftExpression;
|
||||||
this.rightExpression = rightExpression;
|
this.rightExpression = rightExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int interpret() {
|
public int interpret() {
|
||||||
return leftExpression.interpret() + rightExpression.interpret();
|
return leftExpression.interpret() + rightExpression.interpret();
|
||||||
|
@ -1,41 +1,41 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Iterator (ItemIterator) adds abstraction layer on top of a collection
|
* Iterator (ItemIterator) adds abstraction layer on top of a collection
|
||||||
* (TreasureChest). This way the collection can change its internal
|
* (TreasureChest). This way the collection can change its internal
|
||||||
* implementation without affecting its clients.
|
* implementation without affecting its clients.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
TreasureChest chest = new TreasureChest();
|
TreasureChest chest = new TreasureChest();
|
||||||
|
|
||||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||||
while (ringIterator.hasNext()) {
|
while (ringIterator.hasNext()) {
|
||||||
System.out.println(ringIterator.next());
|
System.out.println(ringIterator.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("----------");
|
System.out.println("----------");
|
||||||
|
|
||||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||||
while (potionIterator.hasNext()) {
|
while (potionIterator.hasNext()) {
|
||||||
System.out.println(potionIterator.next());
|
System.out.println(potionIterator.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("----------");
|
System.out.println("----------");
|
||||||
|
|
||||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||||
while (weaponIterator.hasNext()) {
|
while (weaponIterator.hasNext()) {
|
||||||
System.out.println(weaponIterator.next());
|
System.out.println(weaponIterator.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("----------");
|
System.out.println("----------");
|
||||||
|
|
||||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
System.out.println(it.next());
|
System.out.println(it.next());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ public class Item {
|
|||||||
this.setType(type);
|
this.setType(type);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
@ -3,11 +3,11 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Iterator interface.
|
* Iterator interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface ItemIterator {
|
public interface ItemIterator {
|
||||||
|
|
||||||
boolean hasNext();
|
boolean hasNext();
|
||||||
|
|
||||||
Item next();
|
Item next();
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,6 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum ItemType {
|
public enum ItemType {
|
||||||
|
|
||||||
ANY,
|
ANY, WEAPON, RING, POTION
|
||||||
WEAPON,
|
|
||||||
RING,
|
|
||||||
POTION
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,12 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Collection class.
|
* Collection class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TreasureChest {
|
public class TreasureChest {
|
||||||
|
|
||||||
private List<Item> items;
|
private List<Item> items;
|
||||||
|
|
||||||
public TreasureChest() {
|
public TreasureChest() {
|
||||||
items = new ArrayList<>();
|
items = new ArrayList<>();
|
||||||
items.add(new Item(ItemType.POTION, "Potion of courage"));
|
items.add(new Item(ItemType.POTION, "Potion of courage"));
|
||||||
@ -25,7 +25,7 @@ public class TreasureChest {
|
|||||||
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
|
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
|
||||||
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemIterator Iterator(ItemType type) {
|
ItemIterator Iterator(ItemType type) {
|
||||||
return new TreasureChestItemIterator(this, type);
|
return new TreasureChestItemIterator(this, type);
|
||||||
}
|
}
|
||||||
@ -35,5 +35,5 @@ public class TreasureChest {
|
|||||||
list.addAll(items);
|
list.addAll(items);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ public class TreasureChestItemIterator implements ItemIterator {
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
this.idx = -1;
|
this.idx = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return findNextIdx() != -1;
|
return findNextIdx() != -1;
|
||||||
@ -27,9 +27,9 @@ public class TreasureChestItemIterator implements ItemIterator {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int findNextIdx() {
|
private int findNextIdx() {
|
||||||
|
|
||||||
List<Item> items = chest.getItems();
|
List<Item> items = chest.getItems();
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
int tempIdx = idx;
|
int tempIdx = idx;
|
||||||
@ -39,7 +39,8 @@ public class TreasureChestItemIterator implements ItemIterator {
|
|||||||
tempIdx = -1;
|
tempIdx = -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
|
if (type.equals(ItemType.ANY)
|
||||||
|
|| items.get(tempIdx).getType().equals(type)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ public enum Action {
|
|||||||
HUNT, TALE, GOLD, ENEMY;
|
HUNT, TALE, GOLD, ENEMY;
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
String s = "";
|
String s = "";
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case ENEMY:
|
case ENEMY:
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Mediator encapsulates how set of objects (PartyMember) interact. Instead of
|
* Mediator encapsulates how set of objects (PartyMember) interact. Instead of
|
||||||
* referring to each other directly they use the mediator (Party) interface.
|
* referring to each other directly they use the mediator (Party) interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Party party = new PartyImpl();
|
Party party = new PartyImpl();
|
||||||
Hobbit hobbit = new Hobbit();
|
Hobbit hobbit = new Hobbit();
|
||||||
Wizard wizard = new Wizard();
|
Wizard wizard = new Wizard();
|
||||||
Rogue rogue = new Rogue();
|
Rogue rogue = new Rogue();
|
||||||
Hunter hunter = new Hunter();
|
Hunter hunter = new Hunter();
|
||||||
|
|
||||||
party.addMember(hobbit);
|
party.addMember(hobbit);
|
||||||
party.addMember(wizard);
|
party.addMember(wizard);
|
||||||
party.addMember(rogue);
|
party.addMember(rogue);
|
||||||
party.addMember(hunter);
|
party.addMember(hunter);
|
||||||
|
|
||||||
hobbit.act(Action.ENEMY);
|
hobbit.act(Action.ENEMY);
|
||||||
wizard.act(Action.TALE);
|
wizard.act(Action.TALE);
|
||||||
rogue.act(Action.GOLD);
|
rogue.act(Action.GOLD);
|
||||||
hunter.act(Action.HUNT);
|
hunter.act(Action.HUNT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Mediator interface.
|
* Mediator interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Party {
|
public interface Party {
|
||||||
|
|
||||||
void addMember(PartyMember member);
|
void addMember(PartyMember member);
|
||||||
|
|
||||||
void act(PartyMember actor, Action action);
|
void act(PartyMember actor, Action action);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,14 @@ import java.util.List;
|
|||||||
public class PartyImpl implements Party {
|
public class PartyImpl implements Party {
|
||||||
|
|
||||||
private List<PartyMember> members;
|
private List<PartyMember> members;
|
||||||
|
|
||||||
public PartyImpl() {
|
public PartyImpl() {
|
||||||
members = new ArrayList<>();
|
members = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void act(PartyMember actor, Action action) {
|
public void act(PartyMember actor, Action action) {
|
||||||
for (PartyMember member: members) {
|
for (PartyMember member : members) {
|
||||||
if (member != actor) {
|
if (member != actor) {
|
||||||
member.partyAction(action);
|
member.partyAction(action);
|
||||||
}
|
}
|
||||||
@ -25,13 +25,13 @@ public class PartyImpl implements Party {
|
|||||||
members.add(member);
|
members.add(member);
|
||||||
member.joinedParty(this);
|
member.joinedParty(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// somebody hunts for food, call for dinner
|
// somebody hunts for food, call for dinner
|
||||||
|
|
||||||
// somebody spots enemy, alert everybody
|
// somebody spots enemy, alert everybody
|
||||||
|
|
||||||
// somebody finds gold, deal the gold with everybody
|
// somebody finds gold, deal the gold with everybody
|
||||||
|
|
||||||
// somebody tells a tale, call everybody to listen
|
// somebody tells a tale, call everybody to listen
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Interface for party members interacting with Party.
|
* Interface for party members interacting with Party.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface PartyMember {
|
public interface PartyMember {
|
||||||
|
|
||||||
void joinedParty(Party party);
|
void joinedParty(Party party);
|
||||||
|
|
||||||
void partyAction(Action action);
|
void partyAction(Action action);
|
||||||
|
|
||||||
void act(Action action);
|
void act(Action action);
|
||||||
|
@ -31,7 +31,7 @@ public abstract class PartyMemberBase implements PartyMember {
|
|||||||
}
|
}
|
||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void act(Action action) {
|
public void act(Action action) {
|
||||||
if (party != null) {
|
if (party != null) {
|
||||||
@ -42,5 +42,5 @@ public abstract class PartyMemberBase implements PartyMember {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,34 +3,34 @@ package com.iluwatar;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Memento pattern is for storing and restoring object state. The object (Star)
|
* Memento pattern is for storing and restoring object state. The object (Star)
|
||||||
* gives out a "memento" (StarMemento) that contains the state of the object.
|
* gives out a "memento" (StarMemento) that contains the state of the object.
|
||||||
* Later on the memento can be set back to the object restoring the state.
|
* Later on the memento can be set back to the object restoring the state.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Stack<StarMemento> states = new Stack<>();
|
Stack<StarMemento> states = new Stack<>();
|
||||||
|
|
||||||
Star star = new Star(StarType.SUN, 10000000, 500000);
|
Star star = new Star(StarType.SUN, 10000000, 500000);
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
states.add(star.getMemento());
|
states.add(star.getMemento());
|
||||||
star.timePasses();
|
star.timePasses();
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
states.add(star.getMemento());
|
states.add(star.getMemento());
|
||||||
star.timePasses();
|
star.timePasses();
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
states.add(star.getMemento());
|
states.add(star.getMemento());
|
||||||
star.timePasses();
|
star.timePasses();
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
states.add(star.getMemento());
|
states.add(star.getMemento());
|
||||||
star.timePasses();
|
star.timePasses();
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
while (states.size() > 0) {
|
while (states.size() > 0) {
|
||||||
star.setMemento(states.pop());
|
star.setMemento(states.pop());
|
||||||
System.out.println(star);
|
System.out.println(star);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,20 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Star uses "mementos" to store and restore state.
|
* Star uses "mementos" to store and restore state.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Star {
|
public class Star {
|
||||||
|
|
||||||
private StarType type;
|
private StarType type;
|
||||||
private int ageYears;
|
private int ageYears;
|
||||||
private int massTons;
|
private int massTons;
|
||||||
|
|
||||||
public Star(StarType startType, int startAge, int startMass) {
|
public Star(StarType startType, int startAge, int startMass) {
|
||||||
this.type = startType;
|
this.type = startType;
|
||||||
this.ageYears = startAge;
|
this.ageYears = startAge;
|
||||||
this.massTons = startMass;
|
this.massTons = startMass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timePasses() {
|
public void timePasses() {
|
||||||
ageYears *= 2;
|
ageYears *= 2;
|
||||||
massTons *= 8;
|
massTons *= 8;
|
||||||
@ -41,7 +41,7 @@ public class Star {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StarMemento getMemento() {
|
StarMemento getMemento() {
|
||||||
|
|
||||||
StarMementoInternal state = new StarMementoInternal();
|
StarMementoInternal state = new StarMementoInternal();
|
||||||
@ -49,20 +49,21 @@ public class Star {
|
|||||||
state.setMassTons(massTons);
|
state.setMassTons(massTons);
|
||||||
state.setType(type);
|
state.setType(type);
|
||||||
return state;
|
return state;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMemento(StarMemento memento) {
|
void setMemento(StarMemento memento) {
|
||||||
|
|
||||||
StarMementoInternal state = (StarMementoInternal) memento;
|
StarMementoInternal state = (StarMementoInternal) memento;
|
||||||
this.type = state.getType();
|
this.type = state.getType();
|
||||||
this.ageYears = state.getAgeYears();
|
this.ageYears = state.getAgeYears();
|
||||||
this.massTons = state.getMassTons();
|
this.massTons = state.getMassTons();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s age: %d years mass: %d tons", type.toString(), ageYears, massTons);
|
return String.format("%s age: %d years mass: %d tons", type.toString(),
|
||||||
|
ageYears, massTons);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* External interface to memento.
|
* External interface to memento.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface StarMemento {
|
public interface StarMemento {
|
||||||
|
|
||||||
|
@ -3,29 +3,34 @@ package com.iluwatar;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Internal interface to memento.
|
* Internal interface to memento.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class StarMementoInternal implements StarMemento {
|
public class StarMementoInternal implements StarMemento {
|
||||||
|
|
||||||
private StarType type;
|
private StarType type;
|
||||||
private int ageYears;
|
private int ageYears;
|
||||||
private int massTons;
|
private int massTons;
|
||||||
|
|
||||||
public StarType getType() {
|
public StarType getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(StarType type) {
|
public void setType(StarType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAgeYears() {
|
public int getAgeYears() {
|
||||||
return ageYears;
|
return ageYears;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAgeYears(int ageYears) {
|
public void setAgeYears(int ageYears) {
|
||||||
this.ageYears = ageYears;
|
this.ageYears = ageYears;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMassTons() {
|
public int getMassTons() {
|
||||||
return massTons;
|
return massTons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMassTons(int massTons) {
|
public void setMassTons(int massTons) {
|
||||||
this.massTons = massTons;
|
this.massTons = massTons;
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,7 @@ package com.iluwatar;
|
|||||||
|
|
||||||
public enum StarType {
|
public enum StarType {
|
||||||
|
|
||||||
SUN,
|
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD;
|
||||||
RED_GIANT,
|
|
||||||
WHITE_DWARF,
|
|
||||||
SUPERNOVA,
|
|
||||||
DEAD;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@ -32,5 +28,5 @@ public enum StarType {
|
|||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user