Java Reference
In-Depth Information
1. public class USAddressFactory implements AddressFactory{
2. public Address createAddress(){
3. return new USAddress();
4. }
5.
6. public PhoneNumber createPhoneNumber(){
7. return new USPhoneNumber();
8. }
9. }
Example A.5 USAddress.java
1. public class USAddress extends Address{
2. private static final String COUNTRY = "UNITED STATES";
3. private static final String COMMA = ",";
4.
5. public String getCountry(){ return COUNTRY; }
6.
7. public String getFullAddress(){
8. return getStreet() + EOL_STRING +
9. getCity() + COMMA + SPACE + getRegion() +
10. SPACE + getPostalCode() + EOL_STRING +
11. COUNTRY + EOL_STRING;
12. }
13. }
Example A.6 USPhoneNumber.java
1. public class USPhoneNumber extends PhoneNumber{
2. private static final String COUNTRY_CODE = "01";
3. private static final int NUMBER_LENGTH = 10;
4.
5. public String getCountryCode(){ return COUNTRY_CODE; }
6.
7. public void setPhoneNumber(String newNumber){
8. if (newNumber.length() == NUMBER_LENGTH){
9. super.setPhoneNumber(newNumber);
10. }
11. }
12. }
The generic framework from AddressFactory , Address , and PhoneNumber makes it easy to extend the system to
support additional countries. With each additional country, define an additional Concrete Factory class and a
matching Concrete Product class. These are files for French address information.
Example A.7 FrenchAddressFactory.java
1. public class FrenchAddressFactory implements AddressFactory{
2. public Address createAddress(){
3. return new FrenchAddress();
4. }
5.
6. public PhoneNumber createPhoneNumber(){
7. return new FrenchPhoneNumber();
8. }
9. }
Example A.8 FrenchAddress.java
1. public class FrenchAddress extends Address{
2. private static final String COUNTRY = "FRANCE";
3.
4. public String getCountry(){ return COUNTRY; }
5.
6. public String getFullAddress(){
7. return getStreet() + EOL_STRING +
8. getPostalCode() + SPACE + getCity() +
9. EOL_STRING + COUNTRY + EOL_STRING;
10. }
11. }
Example A.9 FrenchPhoneNumber.java
1. public class FrenchPhoneNumber extends PhoneNumber{
2. private static final String COUNTRY_CODE = "33";
3. private static final int NUMBER_LENGTH = 9;
4.
5. public String getCountryCode(){ return COUNTRY_CODE; }
Search WWH ::




Custom Search