Abstract Factory Pattern

Characteristics of Abstract Factory

Examples of Abstract Factory

Java Implementaion

AbstractPhoneFactory.class

public abstract class AbstractPhoneFactory {

  public static OSFactory getFactory(OSType osType){

    switch (osType) {
      case ANDROID:
        return new AndroidFactory();
      case WINDOWS:
        return new WindowsFactory();
      default:
        return null;
    }
  }

}

AbstractFactoryExample.class

public class AbstractFactoryExample {

  public static void main(String[] args) {

    OSFactory factory = AbstractPhoneFactory.getFactory(OSType.ANDROID);
    Phone pixelPhone = factory.create(ManufacturerType.GOOGLE);
    pixelPhone.display();

    OSFactory wfactory = AbstractPhoneFactory.getFactory(OSType.WINDOWS);
    Phone nokiaPhone = wfactory.create(ManufacturerType.MICROSOFT);
    nokiaPhone.display();

  }
}

Disadvantages of Abstract Factory

To access the full working code sample, click here

References

Abstract Factory Design Pattern | Implementation and Disadvantages | Clean Code Series

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...