Changed package naming across all examples.
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface Army {
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface Castle {
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven Army!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven castle!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven king!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface King {
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The factory interface.
|
||||
*
|
||||
*/
|
||||
public interface KingdomFactory {
|
||||
|
||||
Castle createCastle();
|
||||
|
||||
King createKing();
|
||||
|
||||
Army createArmy();
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish Army!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish castle!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orc king!";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +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();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user