抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
抽象工厂示例:
假设我们需要两种产品,分别是鼠标和键盘,鼠标由鼠标厂生产,键盘由键盘厂生产。工厂为两个品牌提供代工,分别是戴尔和惠普。如下:
首先创建两个接口,分别是Mouse接口和Keyboard接口,表示鼠标和键盘两个类族。
package AbstractFactory;
public interface Keyboard {
public void print();
}
package AbstractFactory;
public interface Mouse {
public void print();
}
然后创建产品类:产品为戴尔鼠标、戴尔键盘、惠普鼠标、惠普键盘
package AbstractFactory;
public class HPKeyboard implements Keyboard {
public void print(){
System.out.println("惠普键盘");
}
}
package AbstractFactory;
public class HPMouse implements Mouse {
public void print(){
System.out.println("惠普鼠标");
}
}
package AbstractFactory;
public class DellMouse implements Mouse {
public void print(){
System.out.println("戴尔鼠标");
}
}
package AbstractFactory;
public class DellKeyboard implements Keyboard{
public void print(){
System.out.println("戴尔键盘");
}
}
然后创建工厂,先创建抽象工厂:
package AbstractFactory;
public abstract class AbstractFactory {
private static Object lock = new Object();
private static KeyboardFactory keyboardFactory;
private static MouseFactory mouseFactory;
public static MouseFactory getMouseFactory() {
if (mouseFactory == null) {
synchronized (lock) {
if (mouseFactory == null) mouseFactory = new MouseFactory();
}
}
return mouseFactory;
}
public static KeyboardFactory getKeyboardFactory() {
if (keyboardFactory == null) {
synchronized (lock) {
if (keyboardFactory == null) keyboardFactory = new KeyboardFactory();
}
}
return keyboardFactory;
}
}
然后创建小工厂,继承抽象工厂,专注于生产某种产品,例如鼠标厂、键盘厂:
package AbstractFactory;
public class KeyboardFactory extends AbstractFactory {
public Keyboard getInstance(String name) {
switch (name) {
case "dell":
return new DellKeyboard();
case "hp":
return new HPKeyboard();
default:
return null;
}
}
}
package AbstractFactory;
public class MouseFactory extends AbstractFactory {
public Mouse getInstance(String name) {
switch (name) {
case "dell":
return new DellMouse();
case "hp":
return new HPMouse();
default:
return null;
}
}
}
然后就是创建超级工厂类:结合了单例模式,参考同系列 设计模式--单例模式部分
package AbstractFactory;
public class SuperFactory {
private static Object lock = new Object();
private static KeyboardFactory keyboardFactory;
private static MouseFactory mouseFactory;
public static MouseFactory getMouseFactory() {
if (mouseFactory == null) {
synchronized (lock) {
if (mouseFactory == null) mouseFactory = new MouseFactory();
}
}
return mouseFactory;
}
public static KeyboardFactory getKeyboardFactory() {
if (keyboardFactory == null) {
synchronized (lock) {
if (keyboardFactory == null) keyboardFactory = new KeyboardFactory();
}
}
return keyboardFactory;
}
}
整个抽象工厂已经创建完毕,现在进行测试:
假设要为惠普代工鼠标:
package AbstractFactory;
public class Main {
public static void main(String[] a){
Mouse mouse=SuperFactory.getMouseFactory().getInstance("hp");
mouse.print();
}
}
输出结果:
惠普鼠标
注:在创建小工厂的时候我使用了单例模式,因为每一个工厂都是一样的,都只能创建实例,所以只需要实例化一次即可
来源:CSDN
作者:hello师姐
链接:https://blog.csdn.net/nameFailed/article/details/104216346