2011年5月28日星期六

  java设计模式1:simplefactory

PH`Y3AU]7FRXZ6GY{%BP$0S

simplefactory比较简单,即是根据不同的条件,创建不同的产品,这些产品有一个共同的基类,封装了相同的行为,上例中这些水果都有三种功能,并在基类中进行了封装。

相关代码:

package com.javapatterns.simplefactory;

/**
* 水果类,接口
* @author luhx
*/
public interface Fruit
{   
    /**
     * 栽种
     */
    void plant();
    /**
     * 成长
     */
    void grow();

    /**
     * 收割
     */
    void harvest();

}

 

 

package com.javapatterns.simplefactory;

/**
* 苹果类,继承于Fruit
* @author luhx
*
*/
public class Apple implements Fruit
{
    public void plant()
    {
        System.out.println("Apple has been planted.");
    }

    public void grow()
    {
        System.out.println("Apple is growing...");
    }

    public void harvest()
    {
        System.out.println("Apple has been harvested.");
    }

    /**
     * 取树龄
     * @return  :int
     * @uml.property  name="treeAge"
     */
    public int getTreeAge(){ return treeAge; }

    /**
     * 设树龄
     * @param  treeAge:int
     * @uml.property  name="treeAge"
     */
    public void setTreeAge(int treeAge){ this.treeAge = treeAge; }

    /**
     * 树年龄
     * @uml.property  name="treeAge"
     */
    private int treeAge;
}

 

 

package com.javapatterns.simplefactory;

/**
* 葡萄类,继承于Fruit
* @author luhx
*
*/
public class Grape implements Fruit
{
    public void grow()
    {
        System.out.println("Grape is growing...");
    }

    public void harvest()
    {
        System.out.println("Grape has been harvested.");
    }

    public void plant()
    {
        System.out.println("Grape has been planted.");
    }

    /**
     * 判断是否有籽
     * @return:boolean
     */
    public boolean getSeedless()
    {
        return seedless;
    }

    /**
     * 设置是否有籽标识
     * @param  seedless:boolean
     * @uml.property  name="seedless"
     */
    public void setSeedless(boolean seedless)
    {
        this.seedless = seedless;
    }

    /**
     * 是否有籽
     * @uml.property  name="seedless"
     */
    private boolean seedless;
}

 

package com.javapatterns.simplefactory;
/**
* 水果园艺工人类,creater
* @author luhx
*
*/
public class FruitGardener
{
    public static Fruit factory(String which) throws BadFruitException
    {
        if (which.equalsIgnoreCase("apple"))
        {
            return new Apple();
        }
        else if (which.equalsIgnoreCase("strawberry"))
        {
            return new Strawberry();
        }
        else if (which.equalsIgnoreCase("grape"))
        {
            return new Grape();
        }
        else
        {
             throw new BadFruitException("Bad fruit request");
        }
    }
}

 

package com.javapatterns.simplefactory;
/**
* 水果园艺工人类,creater
* @author luhx
*
*/
public class FruitGardener
{
    public static Fruit factory(String which) throws BadFruitException
    {
        if (which.equalsIgnoreCase("apple"))
        {
            return new Apple();
        }
        else if (which.equalsIgnoreCase("strawberry"))
        {
            return new Strawberry();
        }
        else if (which.equalsIgnoreCase("grape"))
        {
            return new Grape();
        }
        else
        {
             throw new BadFruitException("Bad fruit request");
        }
    }
}

 

package com.javapatterns.simplefactory;

/**
* 水果异常类
* @author luhx
*
*/
public class BadFruitException extends Exception
{
    public BadFruitException(String msg)
    {
        super(msg);
    }
}

没有评论:

发表评论