抽象工厂(AbstractFactory)模式的定义:是一种为访问类提供一个创建一组相关或相互依赖对象的接口,且访问类无须指定所要产品的具体类就能得到同族的不同等级的产品的模式结构。
抽象工厂模式是工厂方法模式的升级版本,工厂方法模式只生产一个等级的产品,而抽象工厂模式可生产多个等级的产品。也就是说:工厂方法模式只考虑生产同等级的产品,但是在现实生活中许多工厂是综合型的工厂,能生产多等级(种类) 的产品,如农场里既养动物又种植物,电器厂既生产电视机又生产洗衣机或空调,大学既有软件专业又有生物专业等。比如抽象工厂模式将考虑多等级产品的生产,将同一个具体工厂所生产的位于不同等级的一组产品称为一个产品族,图 1 所示的是海尔工厂和 TCL 工厂所生产的电视机与空调对应的关系图。
使用抽象工厂模式一般要满足以下条件。
抽象工厂模式除了具有工厂方法模式的优点外,其他主要优点如下。
其缺点是:当产品族中需要增加一个新的产品时,所有的工厂类都需要进行修改。
抽象工厂模式同工厂方法模式一样,也是由抽象工厂、具体工厂、抽象产品和具体产品等 4 个要素构成,但抽象工厂中方法个数不同,抽象产品的个数也不同。现在我们来分析其基本结构和实现方法.
1. 模式的结构
抽象工厂模式的主要角色如下。
抽象工厂模式的结构图如图 2 所示。
2. 模式的实现
从图 2 可以看出抽象工厂模式的结构同工厂方法模式的结构相似,不同的是其产品的种类不止一个,所以创建产品的方法也不止一个。下面给出抽象工厂和具体工厂的代码。
(1)抽象工厂:提供了产品的生成方法。
interface AbstractFactory
{
public Product1 newProduct1();
public Product2 newProduct2();
}
(2) 具体工厂:实现了产品的生成方法。
class ConcreteFactory1 implements AbstractFactory
{
public Product1 newProduct1()
{
System.out.println("具体工厂 1 生成-->具体产品 11...");
return new ConcreteProduct11();
}
public Product2 newProduct2()
{
System.out.println("具体工厂 1 生成-->具体产品 21...");
return new ConcreteProduct21();
}
}
用抽象工厂模式设计农场类。
分析:农场中除了像畜牧场一样可以养动物,还可以培养植物,如养马、养牛、种菜、种水果等,所以本实例比前面介绍的畜牧场类复杂,必须用抽象工厂模式来实现。
本例用抽象工厂模式来设计两个农场,一个是韶关农场用于养牛和种菜,一个是上饶农场用于养马和种水果,可以在以上两个农场中定义一个生成动物的方法 newAnimal() 和一个培养植物的方法 newPlant()。
对马类、牛类、蔬菜类和水果类等具体产品类,由于要显示它们的图像,所以它们的构造函数中用JPanel、JLabel 和 ImageIcon 等组件,并定义一个 show() 方法来显示它们。其结构图如图 3 所示。
抽象产品:动物类
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();
}
}
抽象工厂模式最早的应用是用于创建属于不同操作系统的视窗构件。如 java 的 AWT 中的 Button 和 Text 等构件在 Windows 和 UNIX 中的本地实现是不同的。
抽象工厂模式通常适用于以下场景:
抽象工厂模式的扩展有一定的“开闭原则”倾斜性:
另一方面,当系统中只存在一个等级结构的产品时,抽象工厂模式将退化到工厂方法模式。
原文:https://www.cnblogs.com/L-xuan/p/12001617.html