抽象工厂模式的主要角色:
(1)抽象工厂(Abstract Factory):提供了创建产品的接口,它包含多个创建产品的方法newProduct(),可以创建多个不同等级的产品。
(2)具体工厂(Concrete Factory):主要实现抽象工厂中的多个抽象方法,完成具体产品的创建。
(3)抽象产品(Product):定义产品的规范,描述产品主要特性和功能,抽象工厂模式有多个抽象产品。
(4)具体产品(Concrete Product):实现抽象产品角色定义的接口,由具体工厂创建,同具体工厂之间是多对一的关系。
抽象产品:动物类
interface Animal {
public void show();
}
具体产品:马类
class Horse implements Animal {
JScrollPane sp;
JFrame jf = new JFrame("抽象工厂模式");
public Horse() {
Container contentPane = jf.getContentPane();
JPanel p1 = new JPanel();// 创建面板对象
p1.setLayout(new GridLayout(1, 1));// 设置面板布局(矩形框)
p1.setBorder(BorderFactory.createTitledBorder("动物:马"));// 设置边框(边框上增加标题)
sp = new JScrollPane(p1);// 滚动窗格
contentPane.add(sp, BorderLayout.CENTER);// 边界布局
JLabel l1 = new JLabel(new ImageIcon(""));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
jf.setVisible(true);
}
}
具体产品:牛类
class Cattle implements Animal {
JScrollPane sp;
JFrame jf = new JFrame("抽象工厂模式");
public Cattle() {
Container contentPane = jf.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.setBorder(BorderFactory.createTitledBorder("动物:牛"));
sp = new JScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1 = new JLabel(new ImageIcon("src\\study_rjsj_shiyan6_1\\2.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
jf.setVisible(true);
}
}
抽象产品:植物类
interface Plant {
public void show();
}
具体产品:水果类
class Fruitage implements Plant {
JScrollPane sp;
JFrame jf = new JFrame("抽象工厂模式");
public Fruitage() {
Container contentPane = jf.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.setBorder(BorderFactory.createTitledBorder("植物:水果"));
sp = new JScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1 = new JLabel(new ImageIcon(""));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
jf.setVisible(true);
}
}
具体产品:蔬菜类
class Vegetables implements Plant {
JScrollPane sp;
JFrame jf = new JFrame("抽象工厂模式");
public Vegetables() {
Container contentPane = jf.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
p1.setBorder(BorderFactory.createTitledBorder("植物:蔬菜"));
sp = new JScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1 = new JLabel(new ImageIcon("src\\study_rjsj_shiyan6_1\\3.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void show() {
jf.setVisible(true);
}
}
抽象工厂:农场类
interface Farm {
public Animal newAnimal();
public Plant newPlant();
}
具体工厂:韶关农场类
class SGfarm implements Farm {
public Animal newAnimal() {
System.out.println("新牛出生!");
return new Cattle();
}
public Plant newPlant() {
System.out.println("蔬菜长成!");
return new Vegetables();
}
}
具体工厂:上饶农场类
class SRfarm implements Farm {
public Animal newAnimal() {
System.out.println("新马出生!");
return new Horse();
}
public Plant newPlant() {
System.out.println("水果长成!");
return new Fruitage();
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<className>SGfarm</className>
</config>
对象生成器:Read1XML类
public class Read1XML {
public static Object getObject() {
try {
// 文档创建对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
// 获取节点
doc = builder.parse(new File("src/study_rjsj_shiyan6_1/a.xml"));
NodeList nodeList = doc.getElementsByTagName("className");
Node classNode = nodeList.item(0).getFirstChild();
String cName = classNode.getNodeValue();
System.out.println("新类名:" + cName);
// 返回对象
Class<?> c = Class.forName("study_rjsj_shiyan6_1." + cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
运行控制台输出:
窗口弹出: