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

转载于:https://www.cnblogs.com/1202zhyl/p/5726864.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值