关闭

Creational Patterns Part 2/5: Abstract Factory Pattern

标签: 抽象工厂模式创造型模式设计模式
275人阅读 评论(0) 收藏 举报
分类:

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()
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17230次
    • 积分:924
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条