Normal factory pattern:
- Abstract Factory (Abstract Factory): provides an interface to create a product, the caller access to the plant by its specific method newProduct factory () to create the product.
- Concrete Factory (ConcreteFactory): mainly the implementation of the abstract method in the abstract factory, complete the creation of specific products.
- Abstract product (Product): defines the product specification, describes the main features and functionality of the product.
- Specific products (ConcreteProduct): implements the interface defined roles abstract product, created by the concrete factory, which correspond with between concrete factory.
package FactoryMethod;
public class AbstractFactoryTest
{
public static void main(String[] args)
{
try
{
Product a;
AbstractFactory af;
af=(AbstractFactory) ReadXML1.getObject();
a=af.newProduct();
a.show();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
//抽象产品:提供了产品的接口
interface Product
{
public void show();
}
//具体产品1:实现抽象产品中的抽象方法
class ConcreteProduct1 implements Product
{
public void show()
{
System.out.println("具体产品1显示...");
}
}
//具体产品2:实现抽象产品中的抽象方法
class ConcreteProduct2 implements Product
{
public void show()
{
System.out.println("具体产品2显示...");
}
}
//抽象工厂:提供了厂品的生成方法
interface AbstractFactory
{
public Product newProduct();
}
//具体工厂1:实现了厂品的生成方法
class ConcreteFactory1 implements AbstractFactory
{
public Product newProduct()
{
System.out.println("具体工厂1生成-->具体产品1...");
return new ConcreteProduct1();
}
}
//具体工厂2:实现了厂品的生成方法
class ConcreteFactory2 implements AbstractFactory
{
public Product newProduct()
{
System.out.println("具体工厂2生成-->具体产品2...");
return new ConcreteProduct2();
}
}
Abstract factory class:
It is an upgraded version of the factory pattern, factory method pattern produced only one level of product, and abstract factory pattern can produce more grades of products.
package AbstractFactory;
import java.awt.*;
import javax.swing.*;
public class FarmTest
{
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());
}
}
}
//抽象产品:动物类
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("src/A_Horse.jpg"));
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/A_Cattle.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("src/P_Fruitage.jpg"));
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/P_Vegetables.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();
}
}