Java Design Patterns Abstract Factory Patterns Example 1

Introduction

Example for the Abstract Factory pattern:

The following code only uses the Address and PhoneNumber classes when writing almost all logics.

This allows you to write a very generic framework.

By plugging in Concrete Factories and Products we can specialize the behavior of the code.


interface AddressFactory {
   public Address createAddress();
   public PhoneNumber createPhoneNumber();
}

abstract class PhoneNumber {
   private String phoneNumber;

   public abstract String getCountryCode();

   public String getPhoneNumber() {
      return phoneNumber;
   }//from  w  ww  .j  av a  2  s .  co  m

   public void setPhoneNumber(String newNumber) {
      phoneNumber = newNumber;
   }
}

abstract class Address {
   private String street;
   private String city;
   private String postalCode;

   public String getStreet() {
      return street;
   }

   public String getCity() {
      return city;
   }

   public String getPostalCode() {
      return postalCode;
   }

   public abstract String getCountry();

   public String getFullAddress() {
      return street + "\n" + city + " " + postalCode + "\n";
   }

   public void setStreet(String newStreet) {
      street = newStreet;
   }

   public void setCity(String newCity) {
      city = newCity;
   }

   public void setPostalCode(String newPostalCode) {
      postalCode = newPostalCode;
   }
}

class FrenchAddress extends Address {
   public String getCountry() {
      return "FRANCE";
   }

   public String getFullAddress() {
      return getStreet() + "\n" + getPostalCode() + " " + getCity() + "\n" + getCountry() + "\n";
   }
}

class FrenchAddressFactory implements AddressFactory {
   public Address createAddress() {
      return new FrenchAddress();
   }

   public PhoneNumber createPhoneNumber() {
      return new FrenchPhoneNumber();
   }
}

class FrenchPhoneNumber extends PhoneNumber {
   public String getCountryCode() {
      return "003";
   }

   public void setPhoneNumber(String newNumber) {
       super.setPhoneNumber(newNumber);
   }
}

class USAddress extends Address {
   public String getCountry() {
      return "UNITED STATES";
   }

   public String getFullAddress() {
      return getStreet() + "\n" + getCity() + ", " + " " + getPostalCode() + "\n" + getCountry() + "\n";
   }
}

class USAddressFactory implements AddressFactory {
   public Address createAddress() {
      return new USAddress();
   }

   public PhoneNumber createPhoneNumber() {
      return new USPhoneNumber();
   }
}

class USPhoneNumber extends PhoneNumber {
   private static final String COUNTRY_CODE = "01";

   public String getCountryCode() {
      return COUNTRY_CODE;
   }

   public void setPhoneNumber(String newNumber) {
      super.setPhoneNumber(newNumber);
   }
}

public class Main {
   public static void main(String[] arguments) {
      System.out.println("Creating U.S. Address and Phone Number:");
      AddressFactory usAddressFactory = new USAddressFactory();
      Address usAddress = usAddressFactory.createAddress();
      PhoneNumber usPhone = usAddressFactory.createPhoneNumber();

      usAddress.setStreet("Main Street 1234");
      usAddress.setCity("New York");
      usAddress.setPostalCode("98765");
      usPhone.setPhoneNumber("222-234-2345");

      System.out.println("U.S. address:");
      System.out.println(usAddress.getFullAddress());
      System.out.println("U.S. phone number:");
      System.out.println(usPhone.getPhoneNumber());

      System.out.println("Creating French Address and Phone Number:");
      AddressFactory frenchAddressFactory = new FrenchAddressFactory();
      Address frenchAddress = frenchAddressFactory.createAddress();
      PhoneNumber frenchPhone = frenchAddressFactory.createPhoneNumber();

      frenchAddress.setStreet("21 Bourjour");
      frenchAddress.setCity("Paris");
      frenchAddress.setPostalCode("12345");
      frenchPhone.setPhoneNumber("12-123-12333");

      System.out.println("French address:");
      System.out.println(frenchAddress.getFullAddress());
      System.out.println("French phone number:");
      System.out.println(frenchPhone.getPhoneNumber());
   }
}



PreviousNext

Related