Abstract Factory
design pattern is one of the Creational Design Patterns
. it's almost similar to Factory Design Pattern
except it's more like a factory of factories.
In the Factory Design
pattern, it's a single Factory
class that returns a different type of object based on the input provided, whereas, In the Abstract Factory
pattern, a separate Factory
class is present for each type of object.
Abstract Factory
defines the interface to create an individual object but leaves the actual object creation to the concrete factory
class. each Concrete Factory
class is responsible to create individual objects.
package org.wesome.design.patterns;
public abstract class Apple {
public abstract String getName();
public abstract String getTaste();
}
package org.wesome.design.patterns;
public interface AppleAbstractFactory {
Apple createApple();
}
package org.wesome.design.patterns;
public class AppleFactory {
public static Apple getApple(AppleAbstractFactory factory) {
return factory.createApple();
}
}
package org.wesome.design.patterns;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
@AllArgsConstructor
public class Fuji extends Apple {
private String name;
private String taste;
}
package org.wesome.design.patterns;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class FujiFactory implements AppleAbstractFactory {
private String name;
private String taste;
@Override
public Apple createApple() {
return new Fuji(name, taste);
}
}
package org.wesome.design.patterns;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
@AllArgsConstructor
public class Macintosh extends Apple {
private String name;
private String taste;
}
package org.wesome.design.patterns;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class MacintoshFactory implements AppleAbstractFactory {
private String name;
private String taste;
@Override
public Apple createApple() {
return new Macintosh(name, taste);
}
}
package org.wesome.design.patterns;
public class AbstractFactory {
public static void main(String[] args) {
Apple macintosh = AppleFactory.getApple(new MacintoshFactory("Macintosh", "sweet"));
Apple fuji = AppleFactory.getApple(new FujiFactory("Fuji", "sour"));
System.out.println("Macintosh " + macintosh);
System.out.println("Fuji " + fuji);
}
}
package org.wesome.design.patterns;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class AbstractFactoryTest {
@Test
void testMacintosh() {
Apple macintosh = AppleFactory.getApple(new MacintoshFactory("Macintosh", "sweet"));
Assertions.assertTrue(macintosh instanceof Apple);
Assertions.assertTrue(macintosh instanceof Macintosh);
}
@Test
void testFuji() {
Apple fuji = AppleFactory.getApple(new FujiFactory("Fuji", "sour"));
Assertions.assertTrue(fuji instanceof Apple);
Assertions.assertTrue(fuji instanceof Fuji);
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}