Java Abstract Factory Pattern(抽象工厂模式)

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

实现步骤:

  1. 创建一个形状接口
/**
 * 1. 创建一个形状接口
 * @author mazaiting
 */
public interface Shape {
    /**
     * 画图方法
     */
    void draw();
}
  1. 创建实现接口的实现类
/**
 * 2. 创建实现接口的实现类--矩形
 * @author mazaiting
 */
public class Square implements Shape{

    public void draw() {
        System.out.println("矩形");
    }

}

/**
 * 2. 创建实现接口的实现类--圆角矩形
 * @author mazaiting
 */
public class Rectangle implements Shape{

    public void draw() {
        System.out.println("圆角矩形");
    }

}

/**
 * 2. 创建实现接口的实现类--圆形
 * @author mazaiting
 */
public class Circle implements Shape{

    public void draw() {
        System.out.println("圆形");
    }

}
  1. 创建一个颜色接口
/**
 * 3. 创建颜色接口
 * @author mazaiting
 */
public interface Color {
    /**
     * 填充颜色
     */
    void fill();
}
  1. 创建实现颜色接口的实体类
/**
 * 4. 创建实现颜色接口的实体类--蓝色
 * @author mazaiting
 */
public class Blue implements Color{

    public void fill() {
        System.out.println("蓝色");
    }

}

/**
 * 4. 创建实现颜色接口的实体类--绿色
 * @author mazaiting
 */
public class Green implements Color{

    public void fill() {
        System.out.println("绿色");
    }

}

/**
 * 4. 创建实现颜色接口的实体类--红色
 * @author mazaiting
 */
public class Red implements Color{

    public void fill() {
        System.out.println("红色");
    }

}
  1. 为颜色和形状创建抽象类
/**
 * 5. 为颜色和形状创建抽象类来获取工厂
 * @author mazaiting
 */
public abstract class AbstractFactory {
    /**形状工厂类(包名+类名)--com.mazaiting.factory.ShapeFactory*/
    public static final String SHAPEFACTORY = ShapeFactory.class.getName();
    /**颜色工厂类(包名+类名)--com.mazaiting.factory.ColorFactory*/
    public static final String COLORFACTORY = ColorFactory.class.getName();
    
    /**圆角矩形类名称(包名+类名)--com.mazaiting.shape.Rectangle*/
    public static final String RECTANGLE = Rectangle.class.getName();
    /**矩形类名称(包名+类名)--com.mazaiting.shape.Square*/
    public static final String SQUARE = Square.class.getName();
    /**圆形类名称(包名+类名)--com.mazaiting.shape.Circle*/
    public static final String CIRCLE = Circle.class.getName();
    /**红色类名称(包名+类名)--com.mazaiting.color.Red*/
    public static final String RED = Red.class.getName();
    /**绿色类名称(包名+类名)--com.mazaiting.color.Green*/
    public static final String GREEN = Green.class.getName();
    /**蓝色类名称(包名+类名)--com.mazaiting.color.Blue*/
    public static final String BLUE = Blue.class.getName();
    
    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}
  1. 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
/**
 * 6. 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
 * @author mazaiting
 */
public class ShapeFactory extends AbstractFactory{
    
    /**
     * 单例。如有不懂,请移步 单例模式(http://www.jianshu.com/p/fb952d0140ec)
     */
    private static ShapeFactory sShapeFactory; 
    private ShapeFactory(){}
    public static ShapeFactory getInstace() {
        if (null == sShapeFactory) {
            synchronized (ShapeFactory.class) {
                if (null == sShapeFactory) {
                    sShapeFactory = new ShapeFactory();
                }
            }
        }
        return sShapeFactory;
    }
    
    /**
     * 获取形状类型对象
     * 使用反射创建具体类的实例。如有不懂,请移步 反射机制详解(http://www.jianshu.com/p/990910bd600e)
     * @param type 类型
     * @return 返回形状类型对象
     */
    @Override
    public Shape getShape(String shape) {
        try {
            Class<?> clazz = Class.forName(shape);
            return (Shape) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    public Color getColor(String color) {
        return null;
    }
    
}


/**
 * 6. 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
 * @author mazaiting
 */
public class ColorFactory extends AbstractFactory{
    
    /**
     * 单例。如有不懂,请移步 单例模式(http://www.jianshu.com/p/fb952d0140ec)
     */
    private static ColorFactory sColorFactory; 
    private ColorFactory(){}
    public static ColorFactory getInstace() {
        if (null == sColorFactory) {
            synchronized (ColorFactory.class) {
                if (null == sColorFactory) {
                    sColorFactory = new ColorFactory();
                }
            }
        }
        return sColorFactory;
    }
    
    @Override
    public Shape getShape(String shape) {
        return null;
    }
    
    /**
     * 获取形状类型对象
     * 使用反射创建具体类的实例。如有不懂,请移步 反射机制详解(http://www.jianshu.com/p/990910bd600e)
     * @param type 类型
     * @return 返回形状类型对象
     */
    @Override
    public Color getColor(String color) {
        try {
            Class<?> clazz = Class.forName(color);
            return (Color) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
  1. 创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
/**
 * 7. 创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
 * @author mazaiting
 */
public class FactoryProducer {
    
    private static AbstractFactory abstractFactory = null;

    public static AbstractFactory getFactory(String factory) {
        if (null == factory) {
            throw new RuntimeException("Param can't null");
        }

        if (factory.equalsIgnoreCase(AbstractFactory.SHAPEFACTORY)) {
            // 获取形状工厂实例
            abstractFactory = ShapeFactory.getInstace();
        } else if (factory.equalsIgnoreCase(AbstractFactory.COLORFACTORY)) {
            // 获取颜色工厂实例
            abstractFactory = ColorFactory.getInstace();
        }
        return abstractFactory;
    }
    
}   
  1. 主函数
public class Client {
    public static void main(String[] args) {
        // 获取具体的工厂
        AbstractFactory factory = FactoryProducer.getFactory(AbstractFactory.COLORFACTORY);
        // 获取具体的颜色类
        Color color = factory.getColor(AbstractFactory.BLUE);
        // 填充颜色
        color.fill();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值