Java implementation of the factory approach (Factory method) pattern [00 Original]__java

Source: Internet
Author: User
This example is about 3 kinds of fruit in an orchard, and industrious gardeners take care of them and give them when they need fruit.

1. Factory Method Example Class diagram


2. Java Implementation Code
Package Cn.edu.ynu.sei.factoryMethod;

/**
* Orchard Gardener Interface
*
* @author 88250
* @version 1.0.0, 2007-8-12
* @uml. Dependency supplier= "Cn.edu.ynu.sei.factoryMethod.Fruit"
*/
public interface Fruitgardener
{
/**
* Factory method
*
* @return Fruit crops
*/
Public abstract Fruit Factory ();

/**
* @uml. Property name= "Client"
* @uml. AssociationEnd inverse= "FruitGardener:cn.edu.ynu.sei.factoryMethod.Client"
*/
Public client client = NULL;
}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Apple Garden Gardener
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Applegardener implements Fruitgardener
{

/**
* Factory method to return an example of an apple crop
*/
@Override
Public Fruit Factory ()
{
Return to New Apple ();
}

}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Strawberry Garden Gardener
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Strawberrygardener implements Fruitgardener
{

/**
* Factory method, return strawberry Crop Example
*/
@Override
Public Fruit Factory ()
{
return new Strawberry ();
}

}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Vineyard Gardeners
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Grapegardener implements Fruitgardener
{
/**
* Factory method, return grape crop object
*/
@Override
Public Fruit Factory ()
{
return new Grape ();
}

}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Fruit Crop interface
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public interface Fruit
{

/**
* Crop growth
*/
public abstract void Grow ();

/**
* Harvesting crops
*/
public abstract void Harvest ();

/**
* Growing crops
*/
public abstract void plant ();

}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Apple crop
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Apple implements Fruit
{
/**
* Age
*/
private int treeage;

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#grow ()
*/
@Override
public void Grow ()
{
System.out.println ("Apple is growing ....") " );
}

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#harvest ()
*/
@Override
public void Harvest ()
{
System.out.println ("Apple has been harvested.");
}

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#plant ()
*/
@Override
public void Plant ()
{
System.out.println ("Apple has been planted.");
}

/**
* Return to Age
*
* @return Age
*/
public int Gettreeage ()
{
return treeage;
}

/**
* Set Age
*
* @param treeage
* Age
*/
public void settreeage (int treeage)
{
this. TreeAge = TreeAge;
}
}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Strawberry Crop
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Strawberry Implements Fruit
{

@Override
public void Grow ()
{
System.out.println ("Strawberry is growing .....") " );
}

@Override
public void Harvest ()
{
SYSTEM.OUT.PRINTLN ("Strawberry has been harvested.");
}

@Override
public void Plant ()
{
SYSTEM.OUT.PRINTLN ("Strawberry has been planted.");
}

}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Grape crop
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Grape implements Fruit
{
/**
* Seedless grapes?
*/
Private Boolean seedless;

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#grow ()
*/
@Override
public void Grow ()
{
System.out.println ("Grape is growing .....") " );
}

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#harvest ()
*/
@Override
public void Harvest ()
{
System.out.println ("Grape has been harvested.");
}

/*
* (Non-javadoc)
*
* @see Cn.edu.ynu.sei.factorymethod.fruit#plant ()
*/
@Override
public void Plant ()
{
System.out.println ("Grape has been planted.");
}

/**
* Is there no seed?
* @return
*/
public boolean getseedless ()
{
return seedless;
}

/**
* Set whether no seeds
* @param seedless
*/
public void Setseedless (Boolean seedless)
{
this. seedless = seedless;
}
}


Package Cn.edu.ynu.sei.factoryMethod;

/**
* Customers of Orchard
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Client
{
/**
* Main program Entry point
*
* @param args
* <code>null</code>
*/
public static void Main (string[] args)
{
Fruit Apple, strawberry, grape;

Fruitgardener = new Applegardener ();

Apple = Fruitgardener.factory ();
Apple.plant ();

Fruitgardener =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.