抽象工厂(Abstract Factory)角色:担任这个角色的是工厂方法模式的核心,它是与应用系统商业逻辑无关的。
具体工厂(Concrete Factory)角色:这个角色直接在客户端的调用下创建产品的实例。这个角色含有选择合适的产品对象的逻辑,而这个逻辑是与应用系统的商业逻辑紧密相关的。
抽象产品(Abstract Product)角色:担任这个角色的类是工厂方法模式所创建的对象的父类,或它们共同拥有的接口。
具体产品(Concrete Product)角色:抽象工厂模式所创建的任何产品对象都是某一个具体产品类的实例。这是客户端最终需要的东西,其内部一定充满了应用系统的商业逻辑。
public interface EFactory { public Television produceTelevision();//生产电视 public AirConditioner produceAirConditioner();//生产空调 }具体工厂
public class HaierFactory implements EFactory { public Television produceTelevision() { return new HaierTelevision(); } public AirConditioner produceAirConditioner() { return new HairAirConditioner(); } }
public class TCLFactory implements EFactory { public Television produceTelevision() { return new TCLTelevision(); } public AirConditioner produceAirConditioner() { return new TCLAirConditioner(); } }抽象产品
public interface Television { public void play(); }
public interface AirConditioner { public void changeTemperature(); }具体产品
public class HaierTelevision implements Television { public void play() { System.out.println("海尔电视机播放中......"); } }
public class HairAirConditioner implements AirConditioner { public void changeTemperature() { System.out.println("海尔空调温度改变中......"); }
public class TCLTelevision implements Television { public void play() { System.out.println("TCL电视机播放中......"); } }
public class TCLAirConditioner implements AirConditioner { public void changeTemperature() { System.out.println("TCL空调温度改变中......"); } }XML配置文件
<?xml version="1.0"?> <config> <className>HaierFactory</className> </config>读取XML工具
import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException; import java.io.*; public class XMLUtil { //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 public static Object getBean() { try { //创建文档对象 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File("config.xml")); //获取包含类名的文本节点 NodeList nl = doc.getElementsByTagName("className"); Node classNode=nl.item(0).getFirstChild(); String cName=classNode.getNodeValue(); //通过类名生成实例对象并将其返回 Class c=Class.forName(cName); Object obj=c.newInstance(); return obj; } catch(Exception e) { e.printStackTrace(); return null; } } }测试类
public class Client
{
public static void main(String args[])
{
try
{
EFactory factory;
Television tv;
AirConditioner ac;
factory=(EFactory)XMLUtil.getBean();
tv=factory.produceTelevision();
tv.play();
ac=factory.produceAirConditioner();
ac.changeTemperature();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
java.util.Calendar#getInstance()
java.util.Arrays#asList()
java.util.ResourceBundle#getBundle()
java.net.URL#openConnection()
java.sql.DriverManager#getConnection()
java.sql.Connection#createStatement()
java.sql.Statement#executeQuery()
java.text.NumberFormat#getInstance()
java.lang.management.ManagementFactory
(all getXXX()
methods)java.nio.charset.Charset#forName()
javax.xml.parsers.DocumentBuilderFactory#newInstance()
javax.xml.transform.TransformerFactory#newInstance()
javax.xml.xpath.XPathFactory#newInstance()
java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
(Returns singleton object per protocol)