JAVA-1597 Abstract Factory pattern example (#14733)
Upgraded example displaying a more complex use of the pattern
This commit is contained in:
parent
b4d796004d
commit
28d4771dfd
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class AbstractFactoryRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AnimalAbstractFactory().createAnimal(AnimalType.LAND);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public abstract class Animal {
|
||||
AnimalType type;
|
||||
AnimalEra era;
|
||||
String name;
|
||||
Animal(AnimalType type, AnimalEra era, String name) {
|
||||
this.type = type;
|
||||
this.era = era;
|
||||
this.name = name;
|
||||
}
|
||||
abstract void create();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class AnimalAbstractFactory {
|
||||
|
||||
Animal animal;
|
||||
Animal createAnimal(AnimalType type) {
|
||||
AnimalEra era = getFromConfiguration();
|
||||
switch (era) {
|
||||
case MESOZOIC:
|
||||
animal = new MesozoicAnimalFactory().createAnimal(type);
|
||||
break;
|
||||
case CENOZOIC:
|
||||
animal = new CenozoicAnimalFactory().createAnimal(type);
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
AnimalEra getFromConfiguration() {
|
||||
return AnimalEra.MESOZOIC;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public enum AnimalEra {
|
||||
MESOZOIC,
|
||||
CENOZOIC
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public enum AnimalType {
|
||||
LAND,
|
||||
SKY
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class CenozoicAnimalFactory implements EraAnimalFactory{
|
||||
@Override
|
||||
public LandAnimal makeLandAnimal() {
|
||||
return new LandAnimal(AnimalEra.CENOZOIC, "Mammoth");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SkyAnimal makeSkyAnimal() {
|
||||
return new SkyAnimal(AnimalEra.CENOZOIC, "Terror bird");
|
||||
}
|
||||
|
||||
Animal createAnimal(AnimalType type) {
|
||||
switch (type) {
|
||||
case LAND:
|
||||
return makeLandAnimal();
|
||||
case SKY:
|
||||
return makeSkyAnimal();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public interface EraAnimalFactory {
|
||||
|
||||
LandAnimal makeLandAnimal();
|
||||
SkyAnimal makeSkyAnimal();
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class LandAnimal extends Animal{
|
||||
|
||||
LandAnimal(AnimalEra era, String name) {
|
||||
super(AnimalType.LAND, era, name);
|
||||
create();
|
||||
}
|
||||
@Override
|
||||
void create() {
|
||||
System.out.println("Creating a " + type + " animal: " + name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class MesozoicAnimalFactory implements EraAnimalFactory{
|
||||
@Override
|
||||
public LandAnimal makeLandAnimal() {
|
||||
return new LandAnimal(AnimalEra.MESOZOIC, "Tyrannosaurus Rex");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SkyAnimal makeSkyAnimal() {
|
||||
return new SkyAnimal(AnimalEra.MESOZOIC, "Pterodactylus");
|
||||
}
|
||||
|
||||
Animal createAnimal(AnimalType type) {
|
||||
switch (type) {
|
||||
case LAND:
|
||||
return makeLandAnimal();
|
||||
case SKY:
|
||||
return makeSkyAnimal();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.creational.abstractfactory2;
|
||||
|
||||
public class SkyAnimal extends Animal{
|
||||
|
||||
SkyAnimal(AnimalEra era, String name) {
|
||||
super(AnimalType.SKY, era, name);
|
||||
create();
|
||||
}
|
||||
@Override
|
||||
void create() {
|
||||
System.out.println("Creating a " + type + " animal: " + name);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue