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();
}
}
这样客户端就可以修改配置文件进行配置自己想要的结果