抽象工厂是对工厂方法的进一步抽象,它将工厂与产品之间的对应关系提升至m:n。
UML图如下:
与工厂方法一样,抽象工厂模式也有抽象产品、具体产品、抽象工厂和具体工厂四个组件构成。
这里的抽象工厂还是只有一个,但是对应的抽象产品确是有多个。
代码示例:
// 抽象产品:动物类
interface Animal {
public void show();
}
// 具体产品:马类
class Horse implements Animal {
JScrollPane sp;
JFrame jf = new JFrame("抽象工厂模式测试");
public Horse() {
Container contentPane = jf.getContentPane();
JPanel pl = new JPanel();
pl.setLayout(new GridLayout(1, 1));
pl.setBorder(BorderFactory.createTitleBorder("动物:马"));
sp = new JScrollPane(pl);
contentPane.add(sp, BorderLayout.CENTER);
JLabel ll = new JLabel(new ImageIcon("src/A_Horse.jpg"));
pl.add(ll);
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 pl = new JPanel();
p1.setLayout(new GridLayout(1, 1));
pl.setBorder(BorderFactory.createTitleBorder("动物:牛"));
sp = new JScrollPane(pl);
contentPane.add(sp, BorderLayout.CENTER);
JLabel ll = new JLabel(new ImageIcon("src/A_Cattle.jpg"));
p1.add(ll);
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 pl = new JPanel();
pl.setLayout(new GridLayout(1, 1));
p1.setBorder(BorderFactory.createTitleBorder("植物:水果"));
sp = new JScrollPane(pl);
contentPane.add(sp, BorderLayout.CENTER);
JLabel ll = new JLabel(new ImageIcon("src/P_Fruitage.jpg"));
pl.add(ll);
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 pl = new JPanel();
pl.setLayout(new GridLayout(1, 1));
pl.setBorder(BorderFactory.createTitleBorder("植物:蔬菜"));
sp = new JScrollPane(pl);
contentPane.add(sp, BorderLayout.CENTER);
JLabel ll = new JLabel(new ImageIcon("src/P_Vegetables.jpg"));
pl.add(ll);
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 new Animal() {
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("新马出生!");
}
public Plant newPlant() {
System.out.println("水果长成!");
}
}
// 测试
public class Client {
public static void Main(String[] args) {
try {
Farm f;
Animal a;
Plant p;
f = (Farm)ReadXML.getObject();
a = f.newAnimal();
p = f.newPlant();
a.show();
p.show();
} catch (Exception e) {
System.out.println(e.getMessage);
}
}
}