Abstract Factory Pattern in Java 2 : Factory Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Design Pattern » Factory PatternScreenshots 
Abstract Factory Pattern in Java 2

//[C] 2002 Sun Microsystems, Inc.---

public class RunAbstractFactoryPattern {
    public static void main(String [] arguments){
        System.out.println("Example for the AbstractFactory pattern");
        System.out.println();
        System.out.println(" (take a look in the RunPattern code. Notice that you can");
        System.out.println("  use the Address and PhoneNumber classes when writing");
        System.out.println("  almost all of the code. This allows you to write a very");
        System.out.println("  generic framework, and plug in Concrete Factories");
        System.out.println("  and Products to specialize the behavior of your code)");
        System.out.println();
        
        System.out.println("Creating U.S. Address and Phone Number:");
        AddressFactory usAddressFactory = new USAddressFactory();
        Address usAddress = usAddressFactory.createAddress();
        PhoneNumber usPhone = usAddressFactory.createPhoneNumber();
        
        usAddress.setStreet("142 Lois Lane");
        usAddress.setCity("Metropolis");
        usAddress.setRegion("WY");
        usAddress.setPostalCode("54321");
        usPhone.setPhoneNumber("7039214722");
        
        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();
        System.out.println();
        
        System.out.println("Creating French Address and Phone Number:");
        AddressFactory frenchAddressFactory = new FrenchAddressFactory();
        Address frenchAddress = frenchAddressFactory.createAddress();
        PhoneNumber frenchPhone = frenchAddressFactory.createPhoneNumber();
        
        frenchAddress.setStreet("21 Rue Victor Hugo");
        frenchAddress.setCity("Courbevoie");
        frenchAddress.setPostalCode("40792");
        frenchPhone.setPhoneNumber("011324290");
        
        System.out.println("French address:");
        System.out.println(frenchAddress.getFullAddress());
        System.out.println("French phone number:");
        System.out.println(frenchPhone.getPhoneNumber());
    }
}

class FrenchAddressFactory implements AddressFactory{
    public Address createAddress(){
        return new FrenchAddress();
    }
    
    public PhoneNumber createPhoneNumber(){
        return new FrenchPhoneNumber();
    }
}
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";
    private static final int NUMBER_LENGTH = 10;
    
    public String getCountryCode(){ return COUNTRY_CODE; }
    
    public void setPhoneNumber(String newNumber){
        if (newNumber.length() == NUMBER_LENGTH){
            super.setPhoneNumber(newNumber);
        }
    }
}
interface AddressFactory{
    public Address createAddress();
    public PhoneNumber createPhoneNumber();
}
abstract class Address{
    private String street;
    private String city;
    private String region;
    private String postalCode;
    
    public static final String EOL_STRING =
        System.getProperty("line.separator");
    public static final String SPACE = " ";
    
    public String getStreet(){ return street; }
    public String getCity(){ return city; }
    public String getPostalCode(){ return postalCode; }
    public String getRegion(){ return region; }
    public abstract String getCountry();
    
    public String getFullAddress(){
        return street + EOL_STRING +
            city + SPACE + postalCode + EOL_STRING;
    }
    
    public void setStreet(String newStreet){ street = newStreet; }
    public void setCity(String newCity){ city = newCity; }
    public void setRegion(String newRegion){ region = newRegion; }
    public void setPostalCode(String newPostalCode){ postalCode = newPostalCode; }
}

abstract class PhoneNumber{
    private String phoneNumber;
    public abstract String getCountryCode();
    
    public String getPhoneNumber(){ return phoneNumber; }
    
    public void setPhoneNumber(String newNumber){
        try{
            Long.parseLong(newNumber);
            phoneNumber = newNumber;
        }
        catch (NumberFormatException exc){
        }
    }
}


class FrenchPhoneNumber extends PhoneNumber{
    private static final String COUNTRY_CODE = "33";
    private static final int NUMBER_LENGTH = 9;
    
    public String getCountryCode(){ return COUNTRY_CODE; }
    
    public void setPhoneNumber(String newNumber){
        if (newNumber.length() == NUMBER_LENGTH){
            super.setPhoneNumber(newNumber);
        }
    }
}

class FrenchAddress extends Address{
    private static final String COUNTRY = "FRANCE";
    
    public String getCountry(){ return COUNTRY; }
    
    public String getFullAddress(){
        return getStreet() + EOL_STRING +
            getPostalCode() + SPACE + getCity() +
            EOL_STRING + COUNTRY + EOL_STRING;
    }
}

class USAddress extends Address{
    private static final String COUNTRY = "UNITED STATES";
    private static final String COMMA = ",";
    
    public String getCountry(){ return COUNTRY; }
    
    public String getFullAddress(){
        return getStreet() + EOL_STRING +
            getCity() + COMMA + SPACE + getRegion() +
            SPACE + getPostalCode() + EOL_STRING +
            COUNTRY + EOL_STRING;
    }
}


           
       
Related examples in the same category
1.Abstract Factory Pattern- Example
2.Factory Method Pattern in Java
3.Illustrates use of Abstract Factory patternIllustrates use of Abstract Factory pattern
4.Fly weight FactoryFly weight Factory
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.