Tuesday, December 11, 2012

Design Patterns : Factory Pattern 1


Factory Pattern is another Design Pattern that has long interested me in
my work. Basically we use factory pattern when we want to leave the actual
invocation of a concrete implementation to a decider class called as the
factory class. The client class need not know anything about the
product class but would call it's the exposed API to do the work. The exposed
might be an interface which the factory would always honour.

Let's say we have an interface Child listed below which has a single
API which provides the age. The age needs to be below a certain limit which
would be decided by the Client code. But the Client doesn't want to know
the gender of the Child and nor it's parentage. All that is the headache of the
factory class which would provide the Child



package com.company.self.study.core.java;

/**
*
* Factory Pattern Example
*
*
* @author administrator
*
*/
public interface Child extends Father, Mother {

public int getAge();
}


The interface Child extends both Father and
Mother but would have an unique gender.


The Father and the Mother classes are listed below

package com.company.self.study.core.java;

/**
*
* Factory Pattern Example
*
* @author administrator
*
*/
public interface Father {

public boolean isMale();
}


In the same way the Mother class is also written

package com.company.self.study.core.java;

/**
*
* Factory Pattern Example
*
* @author administrator
*
*/
public interface Mother {

public boolean isFemale();
}


Next we implement two concrete class which have implemented the Child
class. Currently we are recognizing only two kinds of children. Male or female.
To support that we have two implementations Boy and
Girl
The below listing provides the concrete implementations.
package com.company.self.study.core.java;

/**
*
* Factory Pattern Example
*
*
* @author administrator
*
*/
@ClassTemplate(date="12/05/2012")
public class Boy implements Child {

private int age;


/**
*
*
*
* @param age
*/
public Boy(int age) {
this.age = age;
}


/**
*
*
*
*/
@Override
public int getAge() {
// TODO Auto-generated method stub
return 0;
}



/**
*
*
*
*/
@Override
public boolean isMale() {
// TODO Auto-generated method stub
return true;
}

/**
*
*
*
*/
@Override
public boolean isFemale() {
// TODO Auto-generated method stub
return false;
}

/**
*
*
*/
@Override
public String toString() {
return " Boy of " + age + " yrs ";
}
}



The next is the Girl class implementation.


package com.company.self.study.core.java;


/**
*
* Factory Pattern Example
*
*
* @author administrator
*
*/
@ClassTemplate(date="12/05/2012")
public class Girl implements Child {

private int age;


/**
*
*
*
* @param age
*/

public Girl(int age) {
this.age = age;
}


/**
*
*
*
*
*
*/
@Override
public int getAge() {
// TODO Auto-generated method stub
return 0;
}

/**
*
*
*
*
*/
@Override
public boolean isMale() {
// TODO Auto-generated method stub
return false;
}

/**
*
*
*
*
*
*/
@Override
public boolean isFemale() {
// TODO Auto-generated method stub
return true;
}


/**
*
*
*/
@Override
public String toString() {
return " Girl of " + age + " yrs ";
}
}


No comments:

Post a Comment