Creational Patterns Part 2/5: Abstract Factory Pattern

Creational Patterns Part 2/5: Abstract Factory Pattern

目录


Definition

Abstract Factory相当于一个超级工厂,这个工厂可以创建工厂,真可谓工厂的工厂。

在Abstract Factory Pattern中,有多个方法,每个方法产生一个工厂,每个产生的工厂又可以创建多个对象(Factory Pattern)。

何时使用?当希望可以同时控制几种对象的创建的时候,而Factory Pattern只可以控制一种对象的创建

使用频率:Frequency of use High


UML Class Diagram

Abstract Factory Pattern


Implementation

在示例中Abstract Factory可以创建ShapeFactory和ColorFactory,然后再由两种工厂去创建各自的对象。用户根本不知道(不关心)对象是如何创建的。

// Shape.java
package designpatterns.creationalpatterns.abstractfactory;

public interface Shape {
    void draw();
}

// Color.java
package designpatterns.creationalpatterns.abstractfactory;

public interface Color {
    void fill();
}

// ShapeImpl.java
package designpatterns.creationalpatterns.abstractfactory;

class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("Circle::draw()");
    }

}

class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("Rectangle::draw()");
    }

}

class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("Shape::draw()");
    }

}

// ColorImpl.java
package designpatterns.creationalpatterns.abstractfactory;

class Red implements Color {

    @Override
    public void fill() {
        System.out.println("Red::fill()");
    }
}

class Green implements Color {

    @Override
    public void fill() {
        System.out.println("Green::fill()");
    }
}

class Blue implements Color {

    @Override
    public void fill() {
        System.out.println("Blue::fill()");
    }
}

// AbstracFactory.java
package designpatterns.creationalpatterns.abstractfactory;

abstract class AbstractFactory {
    abstract Shape getShape(String shape);
    abstract Color getColor(String color);
}

class ShapeFactory extends AbstractFactory {

    @Override
    Shape getShape(String shapeType) {
        if (shapeType == null)  return null;

        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        }
        return null;
    }

    @Override
    Color getColor(String color) {      
        return null;
    }
}

class ColorFactory extends AbstractFactory {

    @Override
    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;
    }
}

// AbstractFactoryDemo.java
package designpatterns.creationalpatterns.abstractfactory;

public class AbstractFactoryDemo {
    public static void main(String[] args) {
        AbstractFactory absf = FactoryProduct.getFactory("Shape");

        Shape shape = null;
        shape = absf.getShape("Circle");
        shape.draw();

        shape = absf.getShape("Square");
        shape.draw();

        shape = absf.getShape("Rectangle");
        shape.draw();

        System.out.println("=================");

        absf = FactoryProduct.getFactory("Color");
        Color color = null;
        color = absf.getColor("Red");
        color.fill();

        color = absf.getColor("Green");
        color.fill();

        color = absf.getColor("Blue");
        color.fill();
    }   
}

class FactoryProduct {

    public static AbstractFactory getFactory(String factory) {
        if(factory.equalsIgnoreCase("SHAPE")) {
            return new ShapeFactory();
        } else if(factory.equalsIgnoreCase("COLOR")) {
            return new ColorFactory();
        }

        return null;
    }
}

// output
Circle::draw()
Shape::draw()
Rectangle::draw()
=================
Red::fill()
Green::fill()
Blue::fill()
posted @ 2016-05-30 20:47  1202zhyl  阅读(155)  评论(0编辑  收藏  举报