I implemented the Abstract Factory design pattern as follows.
Concrete Implementation
interface VideoExporter {
void exportVideo();
}
interface AudioExporter {
void exportAudio();
}
class HQVideoExporter implements VideoExporter {
@Override
public void exportVideo() {
System.out.println("Exporting HQ video");
}
}
class HQAudioExporter implements AudioExporter {
@Override
public void exportAudio() {
System.out.println("Exporting HQ Audio");
}
}
class LQVideoExporter implements VideoExporter {
@Override
public void exportVideo() {
System.out.println("Exporting LQ video");
}
}
class LQAudioExporter implements AudioExporter {
@Override
public void exportAudio() {
System.out.println("Exporting LQ Audio");
}
}
enum QUALITY_TYPE {
LOW, HIGH
}
Factory Class
class ExporterFactory {
static VideoExporter createVideoExporter(QUALITY_TYPE quality_type) {
return switch (quality_type) {
case LOW -> new LQVideoExporter();
case HIGH -> new HQVideoExporter();
};
}
static AudioExporter createAudioExporter(QUALITY_TYPE quality_type) {
return switch (quality_type) {
case LOW -> new LQAudioExporter();
case HIGH -> new HQAudioExporter();
};
}
}
Client code
VideoExporter videoExporter = ExporterFactory.createVideoExporter(QUALITY_TYPE.HIGH);
AudioExporter audioExporter = ExporterFactory.createAudioExporter(QUALITY_TYPE.LOW);
videoExporter.exportVideo();
audioExporter.exportAudio();
In the above implementation, createVideoExporter
and createAudioExporter
are exposed to the client code. I think this is not an abstract factory design pattern, it is just a factory design pattern. To make it an Abstract factory pattern do I need to create another interface with the concrete implementations returning similar kinds of codecs like only HQAudioExporter
and HQVideoExporter
can be created from one implementation, and LQVideoExporter
and LQAudioExporter
from another implementation, Am I correct?
enum QUALITY_TYPE
should beenum QualityType
to follow java-naming conventions \$\endgroup\$