简单工厂的原理以及一个样例

1、简单工厂模式(Simple Factory Pattern):定义一个工厂类,它可以根据参数的不同返回不同类的实例,被创建的实例通常都具有共同的父类。因为在简单工厂模式中用于创建实例的方法是静态(static)方法,因此简单工厂模式又被称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。

我们经常使用的powerDesinger 创建类图

下面这个例子


package simplefactory;


public interface Fruit
{
    void grow();


    void harvest();


    void plant();
}

package simplefactory;


public class Apple implements Fruit
{
    private int treeAge;
    
    public int getTreeAge(){ return treeAge; }


    public void setTreeAge(int treeAge){ this.treeAge = treeAge; }
    
    public void grow()
    {
    System.out.println("Apple is growing...");
    }


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


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

package simplefactory;


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.");
    }


    public boolean getSeedless()
    {
        return seedless;
    }


    public void setSeedless(boolean seedless)
    {
        this.seedless = seedless;
    }


    private boolean seedless;
}


package simplefactory;




public class Strawberry implements Fruit
{
    
    public void grow()
    {
    System.out.println("Strawberry is growing...");
    }


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


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


}


package simplefactory;


public class BadFruitException extends Exception
{
    public BadFruitException(String msg)
    {
        super(msg);
    }
}


package simplefactory;


public class FruitGardener
{
    public static Fruit getFruit(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 simplefactory;


public class TestSimpleFactory {


/**
* @yy
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Fruit fruit=FruitGardener.getFruit("apple");
fruit.plant();
fruit.grow();
fruit.harvest();
} catch (BadFruitException e) {
e.printStackTrace();
}


}
}

这个程序的客户端有一个问题就是需要不断的修改参数,违反了开闭原则。

因此客户端需要使用配置文件,来增强程序的灵活性。

<?xml version="1.0" encoding="UTF-8"?>
<config>  
    <fruitType>apple</fruitType>  
</config>  


package util;




import java.io.File;
import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


xml解析我使用的是传统的jaxp解析。大家可以使用jdom 、dom4j

public class XMLUtil {


public  static String getFruitName() throws ParserConfigurationException, SAXException, IOException{
String rtvValue=null;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("src/simplefactory/fruit.xml"));

NodeList list=document.getElementsByTagName("fruitType");
rtvValue=list.item(0).getTextContent();
return rtvValue;
}

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{

System.out.println(XMLUtil.getFruitName());
}
}


package simplefactory;


import java.io.IOException;


import javax.xml.parsers.ParserConfigurationException;


import org.xml.sax.SAXException;


import util.XMLUtil;


public class TestSimpleFactory {


/**
* @throws IOException 
* @throws SAXException 
* @throws ParserConfigurationException 
* @throws BadFruitException 
* @yy
* 这个客户端是有缺陷的,每一次需要更换水果,都需要修改代码,因此我们需要使用配置文件
* 动态的让客户进行修改
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, BadFruitException {
// TODO Auto-generated method stub
// try {
// Fruit fruit=FruitGardener.getFruit("apple");
// fruit.plant();
// fruit.grow();
// fruit.harvest();
// } catch (BadFruitException e) {
// e.printStackTrace();
// }
String whichfruit=XMLUtil.getFruitName();
Fruit fruit=FruitGardener.getFruit(whichfruit);
fruit.plant();
fruit.grow();
fruit.harvest();


}


}

这样客户端就可以修改配置文件进行配置自己想要的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值