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.
- Loading branch information
Showing
252 changed files
with
3,737 additions
and
2,819 deletions.
There are no files selected for viewing
66 changes: 37 additions & 29 deletions
66
abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java
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,37 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* 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 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); | ||
} | ||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* The essence of the Abstract Factory pattern is a factory interface | ||
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory}, | ||
* {@link OrcKingdomFactory}). | ||
* <p> | ||
* The example uses both concrete implementations to create a king, a castle and | ||
* an army. | ||
* | ||
*/ | ||
public class App { | ||
|
||
/** | ||
* Program entry point | ||
* @param args command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
createKingdom(new ElfKingdomFactory()); | ||
createKingdom(new OrcKingdomFactory()); | ||
} | ||
|
||
/** | ||
* Creates kingdom | ||
* @param factory | ||
*/ | ||
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); | ||
} | ||
} |
15 changes: 10 additions & 5 deletions
15
abstract-factory/src/main/java/com/iluwatar/abstractfactory/Army.java
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,5 +1,10 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public interface Army { | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* Army interface | ||
* | ||
*/ | ||
public interface Army { | ||
|
||
} |
15 changes: 10 additions & 5 deletions
15
abstract-factory/src/main/java/com/iluwatar/abstractfactory/Castle.java
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,5 +1,10 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public interface Castle { | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* Castle interface | ||
* | ||
*/ | ||
public interface Castle { | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfArmy.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class ElfArmy implements Army { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven Army!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* ElfArmy | ||
* | ||
*/ | ||
public class ElfArmy implements Army { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven Army!"; | ||
} | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfCastle.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class ElfCastle implements Castle { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven castle!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* ElfCastle | ||
* | ||
*/ | ||
public class ElfCastle implements Castle { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven castle!"; | ||
} | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKing.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class ElfKing implements King { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven king!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* ElfKing | ||
* | ||
*/ | ||
public class ElfKing implements King { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Elven king!"; | ||
} | ||
|
||
} |
44 changes: 22 additions & 22 deletions
44
abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKingdomFactory.java
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,22 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* Concrete factory. | ||
* | ||
*/ | ||
public class ElfKingdomFactory implements KingdomFactory { | ||
|
||
public Castle createCastle() { | ||
return new ElfCastle(); | ||
} | ||
|
||
public King createKing() { | ||
return new ElfKing(); | ||
} | ||
|
||
public Army createArmy() { | ||
return new ElfArmy(); | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* ElfKingdomFactory concrete factory. | ||
* | ||
*/ | ||
public class ElfKingdomFactory implements KingdomFactory { | ||
|
||
public Castle createCastle() { | ||
return new ElfCastle(); | ||
} | ||
|
||
public King createKing() { | ||
return new ElfKing(); | ||
} | ||
|
||
public Army createArmy() { | ||
return new ElfArmy(); | ||
} | ||
|
||
} |
15 changes: 10 additions & 5 deletions
15
abstract-factory/src/main/java/com/iluwatar/abstractfactory/King.java
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,5 +1,10 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public interface King { | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* King interface | ||
* | ||
*/ | ||
public interface King { | ||
|
||
} |
32 changes: 16 additions & 16 deletions
32
abstract-factory/src/main/java/com/iluwatar/abstractfactory/KingdomFactory.java
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,16 +1,16 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* The factory interface. | ||
* | ||
*/ | ||
public interface KingdomFactory { | ||
|
||
Castle createCastle(); | ||
|
||
King createKing(); | ||
|
||
Army createArmy(); | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* KingdomFactory factory interface. | ||
* | ||
*/ | ||
public interface KingdomFactory { | ||
|
||
Castle createCastle(); | ||
|
||
King createKing(); | ||
|
||
Army createArmy(); | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcArmy.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class OrcArmy implements Army { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orcish Army!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* OrcArmy | ||
* | ||
*/ | ||
public class OrcArmy implements Army { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orcish Army!"; | ||
} | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcCastle.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class OrcCastle implements Castle { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orcish castle!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* OrcCastle | ||
* | ||
*/ | ||
public class OrcCastle implements Castle { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orcish castle!"; | ||
} | ||
|
||
} |
25 changes: 15 additions & 10 deletions
25
abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKing.java
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,10 +1,15 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
public class OrcKing implements King { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orc king!"; | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* OrcKing | ||
* | ||
*/ | ||
public class OrcKing implements King { | ||
|
||
@Override | ||
public String toString() { | ||
return "This is the Orc king!"; | ||
} | ||
|
||
} |
44 changes: 22 additions & 22 deletions
44
abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKingdomFactory.java
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,22 @@ | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* Concrete factory. | ||
* | ||
*/ | ||
public class OrcKingdomFactory implements KingdomFactory { | ||
|
||
public Castle createCastle() { | ||
return new OrcCastle(); | ||
} | ||
|
||
public King createKing() { | ||
return new OrcKing(); | ||
} | ||
|
||
public Army createArmy() { | ||
return new OrcArmy(); | ||
} | ||
|
||
} | ||
package com.iluwatar.abstractfactory; | ||
|
||
/** | ||
* | ||
* OrcKingdomFactory concrete factory. | ||
* | ||
*/ | ||
public class OrcKingdomFactory implements KingdomFactory { | ||
|
||
public Castle createCastle() { | ||
return new OrcCastle(); | ||
} | ||
|
||
public King createKing() { | ||
return new OrcKing(); | ||
} | ||
|
||
public Army createArmy() { | ||
return new OrcArmy(); | ||
} | ||
|
||
} |
Oops, something went wrong.