抽象工厂模式包含如下角色:
电器工厂:实例说明
电器工厂:参考类图
电器工厂:参考代码
代码结构
Television接口
package abstractfactory;
public interface Television {
public void play();
}
HaierTelevision类
package abstractfactory;
public class HaierTelevision implements Television {
@Override
public void play() {
System.out.println("海尔电视机播放中···");
}
}
TCLTelevision类
package abstractfactory;
public class TCLTelevision implements Television {
@Override
public void play() {
System.out.println("TCL电视机播放中···");
}
}
AirConditioner接口
package abstractfactory;
public interface AirConditioner {
public void changeTemperature();
}
HaierAirConditioner类
package abstractfactory;
public class HaierAirConditioner implements AirConditioner {
@Override
public void changeTemperature() {
System.out.println("海尔空调温度改变中···");
}
}
TCLAirConditioner类
package abstractfactory;
public class TCLAirConditioner implements AirConditioner {
@Override
public void changeTemperature() {
System.out.println("TCL空调温度改变中···");
}
}
EFactory接口
package abstractfactory;
public interface EFactory {
public Television produceTelevision();
public AirConditioner produceAirConditioner();
}
HaierFactory类
package abstractfactory;
public class HaierFactory implements EFactory {
@Override
public Television produceTelevision() {
return new HaierTelevision();
}
@Override
public AirConditioner produceAirConditioner() {
return new HaierAirConditioner();
}
}
TCLFactory类
package abstractfactory;
public class TCLFactory implements EFactory {
@Override
public Television produceTelevision() {
return new TCLTelevision();
}
@Override
public AirConditioner produceAirConditioner() {
return new TCLAirConditioner();
}
}
AbstractFactoryconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<className>abstractfactory.HaierFactory</className>
</config>
XMLUtil类
package abstractfactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XMLUtil {
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean(){
try {
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("D:\\MyNewWorld\\Study_JAVA\\MyHome\\Write_java\\My_Maven\\src\\main\\resources\\AbstractFactoryconfig.xml"));
//获取包含类名的文本节点
NodeList n1 = doc.getElementsByTagName("className");
Node classNode = n1.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;
}
}
}
Client类
package abstractfactory;
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());
}
}
}
运行结果
抽象工厂模式优点:
抽象工厂模式缺点:
在以下情况下可以使用抽象工厂模式: