-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: LovedJohnySmith
Message 4 in Discussion



THE FACTORY PATTERN



        One
type of pattern that we see again and again in OO programs is

the Factory pattern or class. A Factory
pattern is one that returns an instance

of one of several possible classes depending
on the data provided to it.

Usually all of the classes it returns
have a common parent class and common

methods, but each of them performs a
task differently and is optimized for

different kinds of data.



How a Factory Works



        To
understand a Factory pattern, let�s look at the Factory diagram

below.

Factory



In this figure, x is a base class and
classes xy and xz are derived from

it. The Factory is a class that decides
which of these subclasses to return

depending on the arguments you give
it. On the right, we define a getClass

method to be one that passes in some
value abc, and that returns some

instance of the class x. Which one it
returns doesn't matter to the programmer

since they all have the same methods,
but different implementations. How it

decides which one to return is entirely
up to the factory. It could be some very

complex function but it is often quite
simple.



Sample Code



Let's consider a simple case where we
could use a Factory class.

Suppose we have an entry form and we
want to allow the user to enter his

name either as �firstname lastname�
or as �lastname, firstname�. We�ll make

the further simplifying assumption that
we will always be able to decide the

name order by whether there is a comma
between the last and first name.

This is a pretty simple sort of decision
to make, and you could make

it with a simple if statement in a single
class, but let�s use it here to illustrate

how a factory works and what it can
produce. We�ll start by defining a simple

base class that takes a String and splits
it (somehow) into two names:

class Namer {

//a simple class to take a string apart
into two names

protected String last; //store last
name here

protected String first; //store first
name here

public String getFirst() {

return first; //return first name

}

public String getLast() {

return last; //return last name

}

}

In this base class we don�t actually
do anything, but we do provide

implementations of the getFirst and
getLast methods. We�ll store the split

first and last names in the Strings
first and last, and, since the derived classes

will need access to these variables,
we�ll make them protected.

The Two Derived Classes

Now we can write two very simple derived
classes that split the name

into two parts in the constructor. In
the FirstFirst class, we assume that

everything before the last space is
part of the first name:

class FirstFirst extends Namer { //split
first last

public FirstFirst(String s) {

int i = s.lastIndexOf(" ");
//find sep space

if (i > 0) {

//left is first name

first = s.substring(0, i).trim();

//right is last name

last =s.substring(i+1).trim();

}

else {

first = ��; // put all in last name

last = s; // if no space

}

}



And, in the LastFirst class, we assume
that a comma delimits the last

name. In both classes, we also provide
error recovery in case the space or

comma does not exist.

class LastFirst extends Namer { //split
last, first

public LastFirst(String s) {

int i = s.indexOf(","); //find
comma

if (i > 0) {

//left is last name

last = s.substring(0, i).trim();

//right is first name

first = s.substring(i + 1).trim();

}

else {

last = s; // put all in last name

first = ""; // if no comma

}

}

}

Building the Factory

Now our Factory class is extremely simple.
We just test for the

existence of a comma and then return
an instance of one class or the other:

class NameFactory {

//returns an instance of LastFirst or
FirstFirst

//depending on whether a comma is found

public Namer getNamer(String entry)
{

int i = entry.indexOf(",");
//comma determines name

order

if (i>0)

return new LastFirst(entry); //return
one class

else

return new FirstFirst(entry); //or the
other

}

}





I hope this will quite clear and simpler,





thanx and regards,

smith

















-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDOTNET/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to