Creational Patterns Part 2/5: Abstract Factory Pattern

原创 2016年05月30日 20:47:22

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()
版权声明:本文为博主原创文章,未经博主允许不得转载。

Creational Patterns Part 1/5: Factory Pattern

Creational Patterns Part 1/5: Factory Pattern目录 Creational Patterns Part 15 Factory Pattern 目录 Defin...

Design Pattern - Creational Patterns - Abstract Factory Pattern

2007 Section 2, Chapter 2 Abstract Factory Pattern Concept Since...

Creational Patterns Part 5/5: Prototype Pattern

Creational Patterns Part 5/5: Prototype Pattern目录 Creational Patterns Part 55 Prototype Pattern 目录 D...

【C++ Desgin Pattern-Creational】Abstract Factory (抽象工厂)

------文章编辑参考机械工业出版社《设计模式 可复用面向对象软件的基础》 模型目的: 提供一个创建一系列相关或相互依赖的对象的接口,而无需指定其具体的类。 -----------------...
  • KG_Qt
  • KG_Qt
  • 2012年02月03日 20:45
  • 309

Design Pattern - Creational Patterns - Factory Pattern

Section 1, Chapter 1 Factory Pattern Intr: Factories use a base class type to create a class ...

Structural Patterns Part 2/7: Bridge Pattern

Structural Patterns Part 2/6: Bridge Pattern目录 Structural Patterns Part 26 Bridge Pattern 目录 Definit...

Behavioral Patterns Part 2/11: Command Pattern

Behavioral Patterns Part 2/11: Command Pattern目录 Behavioral Patterns Part 211 Command Pattern 目录 Def...

Creational模式之Abstract Factory模式

1.意图提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。2.别名Kit3.动机考虑一个支持多种视感(look-and-feel)标准的用户界面工具包。不同的视感风格为诸如滚动条、窗...

C++实现Creational - Abstract Factory模式

抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。   举例说明:Abstract Factory模式说的是抽象工厂,抽象工厂中继承出了两个实际工厂,这两个实际工厂开始生产对象,两...

《设计模式》之Creational模式:Abstract Factory

对象创建:Abstract Factory 目的 提供一个可以创建一族关联的或者依赖的对象,而不需要指定它们具体的类。 有时也可称为 Kit 驱动 假设有一个用户接口工具箱,它可以支持...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Creational Patterns Part 2/5: Abstract Factory Pattern
举报原因:
原因补充:

(最多只允许输入30个字)