public interfaceAnimal {public voidshow();
}public classCattle implements Animal {
JScrollPane sp;
JFrame jf= new JFrame("抽象工厂模式测试");publicCattle() {
Container contentPane=jf.getContentPane();
JPanel p1= newJPanel();
p1.setLayout(new GridLayout(1,1));
p1.setBorder(BorderFactory.createTitledBorder("动物:牛"));
sp= newJScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1= new JLabel(new ImageIcon("src/com/lynn/learning/designPattern/abstractFactory/A_Cattle.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
}public voidshow() {
jf.setVisible(true);
}
}public classHorse implements Animal {
JScrollPane sp;
JFrame jf= new JFrame("抽象工厂模式测试");publicHorse() {
Container contentPane=jf.getContentPane();
JPanel p1= newJPanel();
p1.setLayout(new GridLayout(1,1));
p1.setBorder(BorderFactory.createTitledBorder("动物:马"));
sp= newJScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1= new JLabel(new ImageIcon("src/com/lynn/learning/designPattern/abstractFactory/A_Horse.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
}public voidshow() {
jf.setVisible(true);
}
}public interfaceFarm {publicAnimal newAnimal();publicPlant newPlant();
}public classFruitage implements Plant {
JScrollPane sp;
JFrame jf= new JFrame("抽象工厂模式测试");publicFruitage() {
Container contentPane=jf.getContentPane();
JPanel p1= newJPanel();
p1.setLayout(new GridLayout(1,1));
p1.setBorder(BorderFactory.createTitledBorder("植物:水果"));
sp= newJScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1= new JLabel(new ImageIcon("src/com/lynn/learning/designPattern/abstractFactory/P_Vegetables.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
}public voidshow() {
jf.setVisible(true);
}
}public classVegetables implements Plant {
JScrollPane sp;
JFrame jf= new JFrame("抽象工厂模式测试");publicVegetables() {
Container contentPane=jf.getContentPane();
JPanel p1= newJPanel();
p1.setLayout(new GridLayout(1, 1));
p1.setBorder(BorderFactory.createTitledBorder("植物:蔬菜"));
sp= newJScrollPane(p1);
contentPane.add(sp, BorderLayout.CENTER);
JLabel l1= new JLabel(new ImageIcon("src/com/lynn/learning/designPattern/abstractFactory/P_Vegetables.jpg"));
p1.add(l1);
jf.pack();
jf.setVisible(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户点击窗口关闭
}public voidshow() {
jf.setVisible(true);
}
}public classSGfarm implements Farm {publicAnimal newAnimal() {
System.out.println("新牛出生!");return newCattle();
}publicPlant newPlant() {
System.out.println("蔬菜长成!");return newVegetables();
}
}public classSRfarm implements Farm {publicAnimal newAnimal() {
System.out.println("新马出生!");return newHorse();
}publicPlant newPlant() {
System.out.println("水果长成!");return newFruitage();
}
}public classFarmTest {public static voidmain(String[] args) {try{
Class> c = Class.forName("com.lynn.learning.designPattern.abstractFactory.SGfarm");
Farm f=(Farm) c.newInstance();
Animal a=f.newAnimal();
Plant p=f.newPlant();
a.show();
p.show();
}catch(Exception e) {
e.printStackTrace();
}
}
}