mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 22:16:03 +00:00
added abstract-factory sample
This commit is contained in:
1 parent
54d54097eb
commit
ce5700c1b7
15 files changed
+184
No files matched your search
@@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>abstract-factory</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>abstract-factory</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public interface Army {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public interface Castle {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class ElfArmy implements Army {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Elven Army!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class ElfCastle implements Castle {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Elven castle!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class ElfKing implements King {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Elven king!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public interface King {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public interface KingdomFactory {
|
||||||
|
|
||||||
|
Castle createCastle();
|
||||||
|
King createKing();
|
||||||
|
Army createArmy();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class OrcArmy implements Army {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Orcish Army!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class OrcCastle implements Castle {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Orcish castle!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class OrcKing implements King {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "This is the Orc king!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<site>
|
||||||
|
<id>website</id>
|
||||||
|
<url>scp://webhost.company.com/www/website</url>
|
||||||
|
</site>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<modules>
|
||||||
|
<module>abstract-factory</module>
|
||||||
|
</modules>
|
||||||
|
</project>
|
||||||
Reference in new issue
Block a user