Factory Pattern

Characteristics of Factory

Examples of Factory

Java Implementaion

PhoneFactory.class

public class PhoneFactory {

  public static Phone getPhone(PhoneType phoneType){

    switch (phoneType) {
      case ANDOID:
        return new AndroidPhone();
      case IPHONE:
        return new IPhone();
        default:
          return null;
    }

  }
}

FactoryExample.class

public class FactoryExample {

  public static void main(String[] args) {
    Phone androidPhone = PhoneFactory.getPhone(PhoneType.ANDOID);
    Phone iPhone = PhoneFactory.getPhone(PhoneType.IPHONE);

    System.out.println(androidPhone);
    System.out.println(iPhone);
  }
}

Disadvantages of Factory

To access the full working code sample, click here

References

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...