学习:抽象工厂模式

抽象工厂模式

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

介绍

  • 主要解决接口选择的问题。
  • 在一个产品族里面,定义多个产品。
  • 在一个工厂里聚合多个同类产品。

实现

  • 为形状创建一个接口。
public interface Shape {
    void draw();
}
  • 创建实现形状接口的实体类。
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("shape : Circle");
    }
}
public class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("Shape : Rectangle");
    }
}
public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Shape : Square");
    }
}
  • 为颜色创建一个接口。
public interface Color {
    void fill();
}
  • 创建实现颜色接口的实体类。
public class Blue implements Color {
    @Override
    public void fill() {
        System.out.println("color: blue");
    }
}
public class Green implements Color {
    @Override
    public void fill() {
        System.out.println("color: green");
    }
}
public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("color: red");
    }
}
  • 为 Color 和 Shape 对象创建抽象类来获取工厂。
public abstract class AbstractFactory {

    abstract Color getColor(String color);

    abstract Shape getShape(String shape);
    
}
  • 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
public class ShapeFactory extends AbstractFactory {
    @Override
    Color getColor(String color) {
        return null;
    }

    @Override
    Shape getShape(String shape) {
        if(shape == null){
            return null;
        }
        if(shape.equalsIgnoreCase("Circle")){
            return new Circle();
        } else if(shape.equalsIgnoreCase("Rectangle")){
            return new Rectangle();
        } else if(shape.equalsIgnoreCase("Square")){
            return new Square();
        }

        return null;
    }
}
public class ColorFactory extends AbstractFactory {
    @Override
    Color getColor(String color) {
        if(color == null){
            return null;
        }
        if(color.equalsIgnoreCase("red")){
            return new Red();
        } else if(color.equalsIgnoreCase("green")){
            return new Green();
        } else if(color.equalsIgnoreCase("blue")){
            return new Blue();
        }
        return null;
    }

    @Override
    Shape getShape(String shape) {
        return null;
    }
}
  • 创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
public class FactoryProducer {

    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("shape")){
            return new ShapeFactory();
        } else if(choice.equalsIgnoreCase("color")){
            return new ColorFactory();
        }
        return null;
    }
}
  • 使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。
public class FactoryDemo {
    public static void main(String[] args) {

        //获取形状工厂
        AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");

        //获取形状为 Circle 的对象
        Shape shape1 = shapeFactory.getShape("Circle");

        //调用 Circle 的 draw 方法
        shape1.draw();

        //获取形状为 Rectangle 的对象
        Shape shape2 = shapeFactory.getShape("Rectangle");

        //调用 Rectangle 的 draw 方法
        shape2.draw();

        //获取形状为 Square 的对象
        Shape shape3 = shapeFactory.getShape("Square");

        //调用 Square 的 draw 方法
        shape3.draw();

        //获取颜色工厂
        AbstractFactory colorFactory = FactoryProducer.getFactory("color");

        //获取颜色为 Red 的对象
        Color color1 = colorFactory.getColor("red");

        //调用 Red 的 fill 方法
        color1.fill();

        //获取颜色为 Green 的对象
        Color color2 = colorFactory.getColor("green");

        //调用 Green 的 fill 方法
        color2.fill();

        //获取颜色为 Blue 的对象
        Color color3 = colorFactory.getColor("blue");

        //调用 Blue 的 fill 方法
        color3.fill();
    }
}
  • 执行结果。
shape : Circle
Shape : Rectangle
Shape : Square
color: red
color: green
color: blue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值