added abstract-factory sample

This commit is contained in:
Ilkka Seppala
2014-08-09 20:38:00 +03:00
parent 54d54097eb
commit ce5700c1b7
15 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.iluwatar;
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);
}
}

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface Army {
}

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface Castle {
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class ElfArmy implements Army {
@Override
public String toString() {
return "This is the Elven Army!";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class ElfCastle implements Castle {
@Override
public String toString() {
return "This is the Elven castle!";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class ElfKing implements King {
@Override
public String toString() {
return "This is the Elven king!";
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar;
public class ElfKingdomFactory implements KingdomFactory {
public Castle createCastle() {
return new ElfCastle();
}
public King createKing() {
return new ElfKing();
}
public Army createArmy() {
return new ElfArmy();
}
}

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface King {
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public interface KingdomFactory {
Castle createCastle();
King createKing();
Army createArmy();
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class OrcArmy implements Army {
@Override
public String toString() {
return "This is the Orcish Army!";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class OrcCastle implements Castle {
@Override
public String toString() {
return "This is the Orcish castle!";
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class OrcKing implements King {
@Override
public String toString() {
return "This is the Orc king!";
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar;
public class OrcKingdomFactory implements KingdomFactory {
public Castle createCastle() {
return new OrcCastle();
}
public King createKing() {
return new OrcKing();
}
public Army createArmy() {
return new OrcArmy();
}
}