java设计模式03_抽象工厂模式

来源:互联网 发布:打印机正在向windows 编辑:程序博客网 时间:2024/05/22 05:16

抽象工厂模式

抽象工厂模式是一个超级工厂,用来创建其他工厂。 这个工厂也被称为工厂的工厂。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。
在抽象工厂模式中,接口负责创建相关对象的工厂,而不明确指定它们的类。 每个生成的工厂可以按照工厂模式提供对象。

实现

我们将创建一个Shape和Color接口并实现这些接口的具体类。在下一步中,将创建一个抽象工厂类AbstractFactory。在每个工厂类ShapeFactory和ColorFactory定义都是扩展自AbstractFactory。创建工厂创建者/生成器类 - FactoryProducer。

AbstractFactoryPatternDemo这是一个演示类,使用FactoryProducer来获取AbstractFactory对象。 它会将信息(CIRCLE / RECTANGLE / SQUARE)传递给AbstractFactory以获取所需的对象类型。 它还将信息(用于Color的 RED / GREEN / BLUE)传递给AbstractFactory以获取所需的对象类型

代码

package com.test;public class Test {    public static void main(String[] args) {        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");        Shape shape1 = shapeFactory.getShape("CIRCLE");        shape1.draw();        Shape shape2 = shapeFactory.getShape("RECTANGLE");        shape2.draw();        Shape shape3 = shapeFactory.getShape("SQUARE");        shape3.draw();        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");        Color color1 = colorFactory.getColor("RED");        color1.fill();        Color color2 = colorFactory.getColor("Green");        color2.fill();        Color color3 = colorFactory.getColor("BLUE");        color3.fill();    }}interface Shape {    void draw();}class Rectangle implements Shape {    @Override    public void draw() {        System.out.println("Inside Rectangle::draw() method.");    }}class Square implements Shape {    @Override    public void draw() {        System.out.println("Inside Square::draw() method.");    }}class Circle implements Shape {    @Override    public void draw() {        System.out.println("Inside Circle::draw() method.");    }}interface Color {    void fill();}class Red implements Color {    @Override    public void fill() {        System.out.println("Inside Red::fill() method.");    }}class Green implements Color {    @Override    public void fill() {        System.out.println("Inside Green::fill() method.");    }}class Blue implements Color {    @Override    public void fill() {        System.out.println("Inside Blue::fill() method.");    }}abstract class AbstractFactory {    abstract Color getColor(String color);    abstract Shape getShape(String shape);}class ShapeFactory extends AbstractFactory {    @Override    public Shape getShape(String shapeType) {        if (shapeType == null) {            return null;        }        if (shapeType.equalsIgnoreCase("CIRCLE")) {            return new Circle();        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {            return new Rectangle();        } else if (shapeType.equalsIgnoreCase("SQUARE")) {            return new Square();        }        return null;    }    @Override    Color getColor(String color) {        return null;    }}class ColorFactory extends AbstractFactory {    @Override    public Shape getShape(String shapeType) {        return null;    }    @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;    }}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;    }}

运行结果:

Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.Inside Red::fill() method.Inside Green::fill() method.Inside Blue::fill() method.
原创粉丝点击