HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java GoF Creational Design Patterns"
Vol. 9, Issue 9, p. 38

	



Listing 1

package factory;

public abstract class PhoneNumber
{
	protected String m_AreaCode;
	protected String m_Number;

	public String getAreaCode()
	{
		return m_AreaCode;
	}
	public String getNumber()
	{
		return m_Number;
	}
}

package factory;

public class PhoneNumberFactory
{
	public PhoneNumber getPhoneNumber(String s_PhoneNumber)
	{
		if (s_PhoneNumber.trim().indexOf(" ") > 0)
		return new PhoneNumberWithSpaces(s_PhoneNumber);
		else
			return new PhoneNumberWithoutSpaces(s_PhoneNumber);
	}
}

package factory;

public class PhoneNumberWithoutSpaces extends PhoneNumber
{
	public PhoneNumberWithoutSpaces(String s_PhoneNumber)
	{
this.m_AreaCode = s_PhoneNumber.substring(0, 3);
this.m_Number = s_PhoneNumber.substring(3, s_PhoneNumber.length());
	}
}

package factory;

public class PhoneNumberWithSpaces extends PhoneNumber
{
	public PhoneNumberWithSpaces(String s_PhoneNumber)
	{
this.m_AreaCode = s_PhoneNumber.substring(0, 3);
this.m_Number = s_PhoneNumber.substring(4, s_PhoneNumber.length());
	}
}

Listing 2

package abstractfactory;

import factory.PhoneNumber;

public abstract class InternationalPhoneNumber
{
	protected String m_InternationalPhoneNumber;

	public abstract PhoneNumber getPhoneNumberWithSpaceFormat();
	public abstract PhoneNumber getPhoneNumberWithoutSpaceFormat();
}

package abstractfactory;

public class InternationalPhoneNumberFactory
{
public InternationalPhoneNumber getInternationalPhoneNumber(String s_Country,
	String s_InternationalPhoneNumber)
	{
		if (s_Country.equalsIgnoreCase("USA"))
		return new USAPhoneNumber(s_InternationalPhoneNumber);
		else
		return new SpanishPhoneNumber(s_InternationalPhoneNumber);
	}
}

package abstractfactory;

import factory.PhoneNumber;
import factory.PhoneNumberWithSpaces;
import factory.PhoneNumberWithoutSpaces;

public class SpanishPhoneNumber extends InternationalPhoneNumber
{
	public SpanishPhoneNumber(String s_SpanishPhoneNumber)
	{
		this.m_InternationalPhoneNumber = s_SpanishPhoneNumber;
	}

	public PhoneNumber getPhoneNumberWithSpaceFormat()
	{
	return new PhoneNumberWithSpaces(this.m_InternationalPhoneNumber);
	}

	public PhoneNumber getPhoneNumberWithoutSpaceFormat()
	{
	return new PhoneNumberWithoutSpaces(this.m_InternationalPhoneNumber);
	}
}

package abstractfactory;

import factory.PhoneNumber;
import factory.PhoneNumberWithSpaces;
import factory.PhoneNumberWithoutSpaces;

public class USAPhoneNumber extends InternationalPhoneNumber
{
	public USAPhoneNumber(String s_USAPhoneNumber)
	{
		this.m_InternationalPhoneNumber = s_USAPhoneNumber;
	}

	public PhoneNumber getPhoneNumberWithSpaceFormat()
	{
	return new PhoneNumberWithSpaces(this.m_InternationalPhoneNumber);
	}

	public PhoneNumber getPhoneNumberWithoutSpaceFormat()
	{
	return new PhoneNumberWithoutSpaces(this.m_InternationalPhoneNumber);
	}
}

Listing 3

public Object deepClone()
{
	try
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	ObjectOutputStream objectOutputStream = new
	ObjectOutputStream(byteArrayOutputStream);
	objectOutputStream.writeObject(this);

	ByteArrayInputStream byteArrayInputStream = new
	ByteArrayInputStream(byteArrayOutputStream.toByteArray());
	ObjectInputStream objectInputStream = new
	ObjectInputStream(byteArrayInputStream);
	return objectInputStream.readObject();
}

catch (Exception ex)
{
	System.out.println(̉Error in deep cloning: ̉ + ex.getMessage());
	return null;
}
}
  
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.