Just formatting App classes to be like the other class files on the project
This commit is contained in:
parent
52f0923df9
commit
bde5b343d0
@ -1,29 +1,29 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The essence of the Abstract Factory pattern is a factory interface (KingdomFactory)
|
* The essence of the Abstract Factory pattern is a factory interface
|
||||||
* and its implementations (ElfKingdomFactory, OrcKingdomFactory).
|
* (KingdomFactory) and its implementations (ElfKingdomFactory,
|
||||||
*
|
* OrcKingdomFactory).
|
||||||
* The example uses both concrete implementations to create a king, a castle and an
|
*
|
||||||
* army.
|
* The example uses both concrete implementations to create a king, a castle and
|
||||||
|
* 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,21 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* There are two variations of the Adapter pattern: The
|
* There are two variations of the Adapter pattern: The class adapter implements
|
||||||
* class adapter implements the adaptee's interface whereas
|
* the adaptee's interface whereas the object adapter uses composition to
|
||||||
* the object adapter uses composition to contain the adaptee
|
* contain the adaptee in the adapter object. This example uses the object
|
||||||
* in the adapter object. This example uses the object adapter
|
* adapter approach.
|
||||||
* approach.
|
*
|
||||||
*
|
* The Adapter (GnomeEngineer) converts the interface of the target class
|
||||||
* The Adapter (GnomeEngineer) converts the interface of the
|
* (GoblinGlider) into a suitable one expected by the client
|
||||||
* target class (GoblinGlider) into a suitable one expected
|
* (GnomeEngineeringManager).
|
||||||
* by the client (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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,32 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Bridge pattern both abstraction (MagicWeapon)
|
* In Bridge pattern both abstraction (MagicWeapon) and implementation
|
||||||
* and implementation (MagicWeaponImp) have their
|
* (MagicWeaponImp) have their own class hierarchies. The interface of the
|
||||||
* 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(new Excalibur());
|
blindingMagicWeapon.wield();
|
||||||
blindingMagicWeapon.wield();
|
blindingMagicWeapon.blind();
|
||||||
blindingMagicWeapon.blind();
|
blindingMagicWeapon.swing();
|
||||||
blindingMagicWeapon.swing();
|
blindingMagicWeapon.unwield();
|
||||||
blindingMagicWeapon.unwield();
|
|
||||||
|
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
||||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
flyingMagicWeapon.wield();
|
||||||
flyingMagicWeapon.wield();
|
flyingMagicWeapon.fly();
|
||||||
flyingMagicWeapon.fly();
|
flyingMagicWeapon.swing();
|
||||||
flyingMagicWeapon.swing();
|
flyingMagicWeapon.unwield();
|
||||||
flyingMagicWeapon.unwield();
|
|
||||||
|
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
||||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
soulEatingMagicWeapon.wield();
|
||||||
soulEatingMagicWeapon.wield();
|
soulEatingMagicWeapon.swing();
|
||||||
soulEatingMagicWeapon.swing();
|
soulEatingMagicWeapon.eatSoul();
|
||||||
soulEatingMagicWeapon.eatSoul();
|
soulEatingMagicWeapon.unwield();
|
||||||
soulEatingMagicWeapon.unwield();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,45 +3,41 @@ package com.iluwatar;
|
|||||||
import com.iluwatar.Hero.HeroBuilder;
|
import com.iluwatar.Hero.HeroBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* This is the Builder pattern variation as described by
|
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||||
* Joshua Bloch in Effective Java 2nd Edition.
|
* Effective Java 2nd Edition.
|
||||||
*
|
*
|
||||||
* We want to build Hero objects, but its construction
|
* We want to build Hero objects, but its construction is complex because of the
|
||||||
* is complex because of the many parameters needed. To
|
* many parameters needed. To aid the user we introduce HeroBuilder class.
|
||||||
* aid the user we introduce HeroBuilder class. HeroBuilder
|
* HeroBuilder takes the minimum parameters to build Hero object in its
|
||||||
* takes the minimum parameters to build Hero object in
|
* constructor. After that additional configuration for the Hero object can be
|
||||||
* its constructor. After that additional configuration
|
* done using the fluent HeroBuilder interface. When configuration is ready the
|
||||||
* for the Hero object can be done using the fluent
|
* build method is called to receive the final Hero object.
|
||||||
* HeroBuilder interface. When configuration is ready
|
|
||||||
* the build method is called to receive the final Hero
|
|
||||||
* object.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
|
||||||
{
|
|
||||||
|
|
||||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
|
||||||
.withHairColor(HairColor.BLACK)
|
|
||||||
.withWeapon(Weapon.DAGGER)
|
|
||||||
.build();
|
|
||||||
System.out.println(mage);
|
|
||||||
|
|
||||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
public static void main(String[] args) {
|
||||||
.withHairColor(HairColor.BLOND)
|
|
||||||
.withHairType(HairType.LONG_CURLY)
|
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||||
.withArmor(Armor.CHAIN_MAIL)
|
.withHairColor(HairColor.BLACK)
|
||||||
.withWeapon(Weapon.SWORD)
|
.withWeapon(Weapon.DAGGER)
|
||||||
.build();
|
.build();
|
||||||
System.out.println(warrior);
|
System.out.println(mage);
|
||||||
|
|
||||||
|
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||||
|
.withHairColor(HairColor.BLOND)
|
||||||
|
.withHairType(HairType.LONG_CURLY)
|
||||||
|
.withArmor(Armor.CHAIN_MAIL)
|
||||||
|
.withWeapon(Weapon.SWORD)
|
||||||
|
.build();
|
||||||
|
System.out.println(warrior);
|
||||||
|
|
||||||
|
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||||
|
.withHairType(HairType.BALD)
|
||||||
|
.withWeapon(Weapon.BOW)
|
||||||
|
.build();
|
||||||
|
System.out.println(thief);
|
||||||
|
|
||||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
|
||||||
.withHairType(HairType.BALD)
|
|
||||||
.withWeapon(Weapon.BOW)
|
|
||||||
.build();
|
|
||||||
System.out.println(thief);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Chain of Responsibility organizes request handlers (RequestHandler) into
|
* Chain of Responsibility organizes request handlers (RequestHandler) into a
|
||||||
* a chain where each handler has a chance to act on the request on its
|
* chain where each handler has a chance to act on the request on its turn. In
|
||||||
* turn. In this example the king (OrcKing) makes requests and the military
|
* this example the king (OrcKing) makes requests and the military orcs
|
||||||
* 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();
|
||||||
|
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||||
|
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
||||||
|
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||||
|
|
||||||
OrcKing king = new OrcKing();
|
|
||||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
|
||||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
|
|
||||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,25 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Command pattern actions are objects that can
|
* In Command pattern actions are objects that can be executed and undone. The
|
||||||
* be executed and undone. The commands in this example
|
* commands in this example are spells cast by the wizard on the goblin.
|
||||||
* are spells cast by the wizard on the goblin.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
|
||||||
{
|
|
||||||
Wizard wizard = new Wizard();
|
|
||||||
Goblin goblin = new Goblin();
|
|
||||||
|
|
||||||
goblin.printStatus();
|
public static void main(String[] args) {
|
||||||
|
Wizard wizard = new Wizard();
|
||||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
Goblin goblin = new Goblin();
|
||||||
goblin.printStatus();
|
|
||||||
|
goblin.printStatus();
|
||||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
|
||||||
goblin.printStatus();
|
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||||
wizard.undoLastSpell();
|
goblin.printStatus();
|
||||||
goblin.printStatus();
|
|
||||||
|
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||||
|
goblin.printStatus();
|
||||||
|
wizard.undoLastSpell();
|
||||||
|
goblin.printStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,25 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* With Composite we can treat tree hierarchies of objects
|
* With Composite we can treat tree hierarchies of objects with uniform
|
||||||
* with uniform interface (LetterComposite). In this example
|
* interface (LetterComposite). In this example we have sentences composed of
|
||||||
* 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 )
|
|
||||||
{
|
|
||||||
System.out.println("Message from the orcs: ");
|
|
||||||
|
|
||||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
|
||||||
orcMessage.print();
|
|
||||||
|
|
||||||
System.out.println("\n");
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Message from the orcs: ");
|
||||||
System.out.println("Message from the elves: ");
|
|
||||||
|
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
orcMessage.print();
|
||||||
elfMessage.print();
|
|
||||||
|
System.out.println("\n");
|
||||||
|
|
||||||
|
System.out.println("Message from the elves: ");
|
||||||
|
|
||||||
|
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||||
|
elfMessage.print();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,24 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Decorator pattern is more flexible alternative to
|
* Decorator pattern is more flexible alternative to subclassing. The decorator
|
||||||
* subclassing. The decorator class implements the same
|
* class implements the same interface as the target and uses composition to
|
||||||
* 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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,27 +4,24 @@ 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,
|
* In Inventory we store the items with a given size. However, we do not store
|
||||||
* we do not store more items than the inventory size. To address
|
* more items than the inventory size. To address concurrent access problems we
|
||||||
* concurrent access problems we use double checked locking to add
|
* use double checked locking to add item to inventory. In this method, the
|
||||||
* item to inventory. In this method, the thread which gets the lock
|
* thread which gets the lock first adds the item.
|
||||||
* first adds the item.
|
|
||||||
*/
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
public class App
|
public static void main(String[] args) {
|
||||||
{
|
final Inventory inventory = new Inventory(1000);
|
||||||
public static void main( String[] args )
|
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||||
{
|
for (int i = 0; i < 3; i++) {
|
||||||
final Inventory inventory = new Inventory(1000);
|
executorService.execute(new Runnable() {
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
@Override
|
||||||
for (int i = 0; i < 3; i++) {
|
public void run() {
|
||||||
executorService.execute(new Runnable() {
|
while (inventory.addItem(new Item()));
|
||||||
@Override
|
}
|
||||||
public void run() {
|
});
|
||||||
while(inventory.addItem(new Item()));
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Facade (DwarvenGoldmineFacade) provides simpler interface to
|
* Facade (DwarvenGoldmineFacade) provides simpler interface to subsystem.
|
||||||
* 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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,28 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Factory Method we have an interface (Blacksmith) with a
|
* In Factory Method we have an interface (Blacksmith) with a method for
|
||||||
* method for creating objects (manufactureWeapon). The concrete
|
* creating objects (manufactureWeapon). The concrete subclasses (OrcBlacksmith,
|
||||||
* subclasses (OrcBlacksmith, ElfBlacksmith) then override the
|
* ElfBlacksmith) then override the method to produce objects of their liking.
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Flyweight (PotionFactory) is useful when there is plethora of
|
* Flyweight (PotionFactory) is useful when there is plethora of objects
|
||||||
* objects (Potion). It provides means to decrease resource usage
|
* (Potion). It provides means to decrease resource usage by sharing object
|
||||||
* 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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,65 +3,64 @@ package com.iluwatar;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Interpreter pattern breaks sentences into expressions (Expression)
|
* Interpreter pattern breaks sentences into expressions (Expression) that can
|
||||||
* 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
|
|
||||||
* This sample uses postfix, where operator comes after the operands
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static void main( String[] args )
|
|
||||||
{
|
|
||||||
String tokenString = "4 3 2 - 1 + *";
|
|
||||||
Stack<Expression> stack = new Stack<>();
|
|
||||||
|
|
||||||
String[] tokenList = tokenString.split(" ");
|
/**
|
||||||
for (String s : tokenList) {
|
*
|
||||||
if (isOperator(s)) {
|
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||||
Expression rightExpression = stack.pop();
|
* This sample uses postfix, where operator comes after the operands
|
||||||
Expression leftExpression = stack.pop();
|
*
|
||||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
*/
|
||||||
leftExpression.interpret(), rightExpression.interpret()));
|
public static void main(String[] args) {
|
||||||
Expression operator = getOperatorInstance(s, leftExpression,
|
String tokenString = "4 3 2 - 1 + *";
|
||||||
rightExpression);
|
Stack<Expression> stack = new Stack<>();
|
||||||
System.out.println(String.format("operator: %s", operator));
|
|
||||||
int result = operator.interpret();
|
String[] tokenList = tokenString.split(" ");
|
||||||
NumberExpression resultExpression = new NumberExpression(result);
|
for (String s : tokenList) {
|
||||||
stack.push(resultExpression);
|
if (isOperator(s)) {
|
||||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
Expression rightExpression = stack.pop();
|
||||||
} else {
|
Expression leftExpression = stack.pop();
|
||||||
Expression i = new NumberExpression(s);
|
System.out.println(String.format("popped from stack left: %d right: %d",
|
||||||
stack.push(i);
|
leftExpression.interpret(), rightExpression.interpret()));
|
||||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
Expression operator = getOperatorInstance(s, leftExpression,
|
||||||
}
|
rightExpression);
|
||||||
}
|
System.out.println(String.format("operator: %s", operator));
|
||||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
int result = operator.interpret();
|
||||||
|
NumberExpression resultExpression = new NumberExpression(result);
|
||||||
|
stack.push(resultExpression);
|
||||||
|
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
||||||
|
} else {
|
||||||
|
Expression i = new NumberExpression(s);
|
||||||
|
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) {
|
|
||||||
if (s.equals("+") || s.equals("-") || s.equals("*"))
|
|
||||||
return true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Expression getOperatorInstance(String s, Expression left,
|
public static boolean isOperator(String s) {
|
||||||
Expression right) {
|
if (s.equals("+") || s.equals("-") || s.equals("*")) {
|
||||||
switch (s) {
|
return true;
|
||||||
case "+":
|
} else {
|
||||||
return new PlusExpression(left, right);
|
return false;
|
||||||
case "-":
|
}
|
||||||
return new MinusExpression(left, right);
|
}
|
||||||
case "*":
|
|
||||||
return new MultiplyExpression(left, right);
|
public static Expression getOperatorInstance(String s, Expression left,
|
||||||
}
|
Expression right) {
|
||||||
return null;
|
switch (s) {
|
||||||
}
|
case "+":
|
||||||
|
return new PlusExpression(left, right);
|
||||||
|
case "-":
|
||||||
|
return new MinusExpression(left, right);
|
||||||
|
case "*":
|
||||||
|
return new MultiplyExpression(left, right);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,42 +1,41 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Iterator (ItemIterator) adds abstraction layer on top of a
|
* Iterator (ItemIterator) adds abstraction layer on top of a collection
|
||||||
* collection (TreasureChest). This way the collection can change
|
* (TreasureChest). This way the collection can change its internal
|
||||||
* 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());
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,28 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Mediator encapsulates how set of objects (PartyMember) interact.
|
* Mediator encapsulates how set of objects (PartyMember) interact. Instead of
|
||||||
* Instead of referring to each other directly they
|
* referring to each other directly they use the mediator (Party) interface.
|
||||||
* 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,37 +3,34 @@ package com.iluwatar;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Memento pattern is for storing and restoring object
|
* Memento pattern is for storing and restoring object state. The object (Star)
|
||||||
* state. The object (Star) gives out a "memento"
|
* gives out a "memento" (StarMemento) that contains the state of the object.
|
||||||
* (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);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,23 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Observer pattern defines one-to-many relationship
|
* Observer pattern defines one-to-many relationship between objects. The target
|
||||||
* between objects. The target object sends change
|
* object sends change notifications to its registered observers.
|
||||||
* notifications to its registered observers.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
|
Weather weather = new Weather();
|
||||||
|
weather.addObserver(new Orcs());
|
||||||
|
weather.addObserver(new Hobbits());
|
||||||
|
|
||||||
|
weather.timePasses();
|
||||||
|
weather.timePasses();
|
||||||
|
weather.timePasses();
|
||||||
|
weather.timePasses();
|
||||||
|
|
||||||
Weather weather = new Weather();
|
|
||||||
weather.addObserver(new Orcs());
|
|
||||||
weather.addObserver(new Hobbits());
|
|
||||||
|
|
||||||
weather.timePasses();
|
|
||||||
weather.timePasses();
|
|
||||||
weather.timePasses();
|
|
||||||
weather.timePasses();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,34 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In Prototype we have a factory class (HeroFactoryImpl) producing
|
* In Prototype we have a factory class (HeroFactoryImpl) producing objects by
|
||||||
* objects by cloning existing ones. In this example the factory's
|
* cloning existing ones. In this example the factory's prototype objects are
|
||||||
* prototype objects are given as constructor parameters.
|
* given as constructor parameters.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
HeroFactory factory;
|
||||||
HeroFactory factory;
|
Mage mage;
|
||||||
Mage mage;
|
Warlord warlord;
|
||||||
Warlord warlord;
|
Beast beast;
|
||||||
Beast beast;
|
|
||||||
|
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
||||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
mage = factory.createMage();
|
||||||
mage = factory.createMage();
|
warlord = factory.createWarlord();
|
||||||
warlord = factory.createWarlord();
|
beast = factory.createBeast();
|
||||||
beast = factory.createBeast();
|
System.out.println(mage);
|
||||||
System.out.println(mage);
|
System.out.println(warlord);
|
||||||
System.out.println(warlord);
|
System.out.println(beast);
|
||||||
System.out.println(beast);
|
|
||||||
|
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
||||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
mage = factory.createMage();
|
||||||
mage = factory.createMage();
|
warlord = factory.createWarlord();
|
||||||
warlord = factory.createWarlord();
|
beast = factory.createBeast();
|
||||||
beast = factory.createBeast();
|
System.out.println(mage);
|
||||||
System.out.println(mage);
|
System.out.println(warlord);
|
||||||
System.out.println(warlord);
|
System.out.println(beast);
|
||||||
System.out.println(beast);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,20 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Proxy (WizardTowerProxy) controls access to the
|
* Proxy (WizardTowerProxy) controls access to the actual object (WizardTower).
|
||||||
* actual object (WizardTower).
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
|
WizardTowerProxy tower = new WizardTowerProxy();
|
||||||
|
tower.enter(new Wizard("Red wizard"));
|
||||||
|
tower.enter(new Wizard("White wizard"));
|
||||||
|
tower.enter(new Wizard("Black wizard"));
|
||||||
|
tower.enter(new Wizard("Green wizard"));
|
||||||
|
tower.enter(new Wizard("Brown wizard"));
|
||||||
|
|
||||||
WizardTowerProxy tower = new WizardTowerProxy();
|
|
||||||
tower.enter(new Wizard("Red wizard"));
|
|
||||||
tower.enter(new Wizard("White wizard"));
|
|
||||||
tower.enter(new Wizard("Black wizard"));
|
|
||||||
tower.enter(new Wizard("Green wizard"));
|
|
||||||
tower.enter(new Wizard("Brown wizard"));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,19 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Singleton pattern ensures that the class (IvoryTower) can have only
|
* Singleton pattern ensures that the class (IvoryTower) can have only one
|
||||||
* one existing instance and provides global access to that instance.
|
* existing instance and provides global access to that instance.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
|
IvoryTower ivoryTower1 = IvoryTower.getInstance();
|
||||||
IvoryTower ivoryTower1 = IvoryTower.getInstance();
|
IvoryTower ivoryTower2 = IvoryTower.getInstance();
|
||||||
IvoryTower ivoryTower2 = IvoryTower.getInstance();
|
System.out.println("ivoryTower1=" + ivoryTower1);
|
||||||
System.out.println("ivoryTower1=" + ivoryTower1);
|
System.out.println("ivoryTower2=" + ivoryTower2);
|
||||||
System.out.println("ivoryTower2=" + ivoryTower2);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,22 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* In State pattern the object (Mammoth) has internal
|
* In State pattern the object (Mammoth) has internal state object (State) that
|
||||||
* state object (State) that defines the current
|
* defines the current behavior. The state object can be changed to alter the
|
||||||
* behavior. The state object can be changed
|
* behavior.
|
||||||
* to alter the behavior.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
|
Mammoth mammoth = new Mammoth();
|
||||||
|
mammoth.observe();
|
||||||
|
mammoth.timePasses();
|
||||||
|
mammoth.observe();
|
||||||
|
mammoth.timePasses();
|
||||||
|
mammoth.observe();
|
||||||
|
|
||||||
Mammoth mammoth = new Mammoth();
|
|
||||||
mammoth.observe();
|
|
||||||
mammoth.timePasses();
|
|
||||||
mammoth.observe();
|
|
||||||
mammoth.timePasses();
|
|
||||||
mammoth.observe();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,22 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Strategy (DragonSlayingStrategy) encapsulates the algorithm to use.
|
* Strategy (DragonSlayingStrategy) encapsulates the algorithm to use. The
|
||||||
* The object (DragonSlayer) can alter its behavior by changing its strategy.
|
* object (DragonSlayer) can alter its behavior by changing its strategy.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
System.out.println("Green dragon spotted ahead!");
|
||||||
System.out.println("Green dragon spotted ahead!");
|
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
|
||||||
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
|
dragonSlayer.goToBattle();
|
||||||
dragonSlayer.goToBattle();
|
System.out.println("Red dragon emerges.");
|
||||||
System.out.println("Red dragon emerges.");
|
dragonSlayer.changeStrategy(new ProjectileStrategy());
|
||||||
dragonSlayer.changeStrategy(new ProjectileStrategy());
|
dragonSlayer.goToBattle();
|
||||||
dragonSlayer.goToBattle();
|
System.out.println("Black dragon lands before you.");
|
||||||
System.out.println("Black dragon lands before you.");
|
dragonSlayer.changeStrategy(new SpellStrategy());
|
||||||
dragonSlayer.changeStrategy(new SpellStrategy());
|
dragonSlayer.goToBattle();
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Template Method (StealingMethod) defines skeleton for the
|
* Template Method (StealingMethod) defines skeleton for the algorithm and
|
||||||
* algorithm and subclasses (HitAndRunMethod, SubtleMethod)
|
* subclasses (HitAndRunMethod, SubtleMethod) fill in the blanks.
|
||||||
* fill in the blanks.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
||||||
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
thief.steal();
|
||||||
thief.steal();
|
thief.changeMethod(new SubtleMethod());
|
||||||
thief.changeMethod(new SubtleMethod());
|
thief.steal();
|
||||||
thief.steal();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,22 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Visitor pattern defines mechanism to apply operations
|
* Visitor pattern defines mechanism to apply operations (UnitVisitor) on nodes
|
||||||
* (UnitVisitor) on nodes (Unit) in hierarchy. New operations
|
* (Unit) in hierarchy. New operations can be added without altering the node
|
||||||
* can be added without altering the node interface.
|
* interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App
|
public class App {
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
|
Commander commander = new Commander(
|
||||||
|
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
||||||
|
new Sergeant(new Soldier(), new Soldier(), new Soldier()));
|
||||||
|
commander.accept(new SoldierVisitor());
|
||||||
|
commander.accept(new SergeantVisitor());
|
||||||
|
commander.accept(new CommanderVisitor());
|
||||||
|
|
||||||
Commander commander = new Commander(
|
|
||||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
|
||||||
new Sergeant(new Soldier(), new Soldier(), new Soldier()));
|
|
||||||
commander.accept(new SoldierVisitor());
|
|
||||||
commander.accept(new SergeantVisitor());
|
|
||||||
commander.accept(new CommanderVisitor());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user