forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just formatting App classes to be like the other class files on the p…
…roject
- Loading branch information
Showing
24 changed files
with
465 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* The essence of the Abstract Factory pattern is a factory interface (KingdomFactory) | ||
* and its implementations (ElfKingdomFactory, OrcKingdomFactory). | ||
* | ||
* The example uses both concrete implementations to create a king, a castle and an | ||
* army. | ||
* | ||
* The essence of the Abstract Factory pattern is a factory interface | ||
* (KingdomFactory) and its implementations (ElfKingdomFactory, | ||
* OrcKingdomFactory). | ||
* | ||
* The example uses both concrete implementations to create a king, a castle and | ||
* an army. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
createKingdom(new ElfKingdomFactory()); | ||
createKingdom(new OrcKingdomFactory()); | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
createKingdom(new ElfKingdomFactory()); | ||
createKingdom(new OrcKingdomFactory()); | ||
} | ||
|
||
public static void createKingdom(KingdomFactory factory) { | ||
King king = factory.createKing(); | ||
Castle castle = factory.createCastle(); | ||
Army army = factory.createArmy(); | ||
System.out.println("The kingdom was created."); | ||
System.out.println(king); | ||
System.out.println(castle); | ||
System.out.println(army); | ||
King king = factory.createKing(); | ||
Castle castle = factory.createCastle(); | ||
Army army = factory.createArmy(); | ||
System.out.println("The kingdom was created."); | ||
System.out.println(king); | ||
System.out.println(castle); | ||
System.out.println(army); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* There are two variations of the Adapter pattern: The | ||
* class adapter implements the adaptee's interface whereas | ||
* the object adapter uses composition to contain the adaptee | ||
* in the adapter object. This example uses the object adapter | ||
* approach. | ||
* | ||
* The Adapter (GnomeEngineer) converts the interface of the | ||
* target class (GoblinGlider) into a suitable one expected | ||
* by the client (GnomeEngineeringManager). | ||
* | ||
* There are two variations of the Adapter pattern: The class adapter implements | ||
* the adaptee's interface whereas the object adapter uses composition to | ||
* contain the adaptee in the adapter object. This example uses the object | ||
* adapter approach. | ||
* | ||
* The Adapter (GnomeEngineer) converts the interface of the target class | ||
* (GoblinGlider) into a suitable one expected by the client | ||
* (GnomeEngineeringManager). | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
GnomeEngineeringManager manager = new GnomeEngineeringManager(); | ||
manager.operateDevice(); | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
GnomeEngineeringManager manager = new GnomeEngineeringManager(); | ||
manager.operateDevice(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,32 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* In Bridge pattern both abstraction (MagicWeapon) | ||
* and implementation (MagicWeaponImp) have their | ||
* own class hierarchies. The interface of the | ||
* implementations can be changed without affecting | ||
* the clients. | ||
* | ||
* In Bridge pattern both abstraction (MagicWeapon) and implementation | ||
* (MagicWeaponImp) have their own class hierarchies. The interface of the | ||
* implementations can be changed without affecting the clients. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur()); | ||
blindingMagicWeapon.wield(); | ||
blindingMagicWeapon.blind(); | ||
blindingMagicWeapon.swing(); | ||
blindingMagicWeapon.unwield(); | ||
|
||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir()); | ||
flyingMagicWeapon.wield(); | ||
flyingMagicWeapon.fly(); | ||
flyingMagicWeapon.swing(); | ||
flyingMagicWeapon.unwield(); | ||
|
||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer()); | ||
soulEatingMagicWeapon.wield(); | ||
soulEatingMagicWeapon.swing(); | ||
soulEatingMagicWeapon.eatSoul(); | ||
soulEatingMagicWeapon.unwield(); | ||
|
||
public class App { | ||
|
||
public static void main(String[] args) { | ||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur()); | ||
blindingMagicWeapon.wield(); | ||
blindingMagicWeapon.blind(); | ||
blindingMagicWeapon.swing(); | ||
blindingMagicWeapon.unwield(); | ||
|
||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir()); | ||
flyingMagicWeapon.wield(); | ||
flyingMagicWeapon.fly(); | ||
flyingMagicWeapon.swing(); | ||
flyingMagicWeapon.unwield(); | ||
|
||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer()); | ||
soulEatingMagicWeapon.wield(); | ||
soulEatingMagicWeapon.swing(); | ||
soulEatingMagicWeapon.eatSoul(); | ||
soulEatingMagicWeapon.unwield(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* 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 this example the king (OrcKing) makes requests and the military | ||
* orcs (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain. | ||
* | ||
* 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 | ||
* this example the king (OrcKing) makes requests and the military orcs | ||
* (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
public class App { | ||
|
||
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")); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* 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. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
Wizard wizard = new Wizard(); | ||
Goblin goblin = new Goblin(); | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
Wizard wizard = new Wizard(); | ||
Goblin goblin = new Goblin(); | ||
|
||
goblin.printStatus(); | ||
|
||
wizard.castSpell(new ShrinkSpell(), goblin); | ||
goblin.printStatus(); | ||
|
||
goblin.printStatus(); | ||
|
||
wizard.castSpell(new ShrinkSpell(), goblin); | ||
goblin.printStatus(); | ||
|
||
wizard.castSpell(new InvisibilitySpell(), goblin); | ||
goblin.printStatus(); | ||
wizard.undoLastSpell(); | ||
goblin.printStatus(); | ||
wizard.castSpell(new InvisibilitySpell(), goblin); | ||
goblin.printStatus(); | ||
wizard.undoLastSpell(); | ||
goblin.printStatus(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* With Composite we can treat tree hierarchies of objects | ||
* with uniform interface (LetterComposite). In this example | ||
* we have sentences composed of words composed of letters. | ||
* | ||
* With Composite we can treat tree hierarchies of objects with uniform | ||
* interface (LetterComposite). In this example we have sentences composed of | ||
* words composed of letters. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println("Message from the orcs: "); | ||
|
||
LetterComposite orcMessage = new Messenger().messageFromOrcs(); | ||
orcMessage.print(); | ||
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"); | ||
|
||
System.out.println("Message from the elves: "); | ||
|
||
System.out.println("\n"); | ||
|
||
System.out.println("Message from the elves: "); | ||
|
||
LetterComposite elfMessage = new Messenger().messageFromElves(); | ||
elfMessage.print(); | ||
LetterComposite elfMessage = new Messenger().messageFromElves(); | ||
elfMessage.print(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
package com.iluwatar; | ||
|
||
/** | ||
* | ||
* Decorator pattern is more flexible alternative to | ||
* subclassing. The decorator class implements the same | ||
* interface as the target and uses composition to | ||
* | ||
* Decorator pattern is more flexible alternative to subclassing. The decorator | ||
* class implements the same interface as the target and uses composition to | ||
* "decorate" calls to the target. | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
|
||
System.out.println("A simple looking troll approaches."); | ||
Hostile troll = new Troll(); | ||
troll.attack(); | ||
troll.fleeBattle(); | ||
|
||
System.out.println("\nA smart looking troll surprises you."); | ||
Hostile smart = new SmartTroll(new Troll()); | ||
smart.attack(); | ||
smart.fleeBattle(); | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
|
||
System.out.println("A simple looking troll approaches."); | ||
Hostile troll = new Troll(); | ||
troll.attack(); | ||
troll.fleeBattle(); | ||
|
||
System.out.println("\nA smart looking troll surprises you."); | ||
Hostile smart = new SmartTroll(new Troll()); | ||
smart.attack(); | ||
smart.fleeBattle(); | ||
} | ||
} |
Oops, something went wrong.