抽象工厂模式

一、抽象工厂模式简介

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
目的:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
主要解决:主要解决接口选择的问题。
优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。产品族难扩展,产品等级易扩展。
二、抽象工厂模式实现

1、创建接口

//创建一个形状接口
package com.factoryPattern;
public interface Shape {
    public void draw();
}
//为颜色创建一个接口。
package com.factoryPattern;
public interface Color {
    public void fill();
}

2、创建接口的实现类
以下为形状的实现类:

//形状的实现类
package com.factoryPattern;
/**
 * 创建一个接口实现类Square
 */
public class Square implements Shape{
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("inside in Square:draw a Square");
    }
}
package com.factoryPattern;
/**
 * 创建一个接口的实现类Circle
 */
public class Circle implements Shape{
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("inside Circle:draw a circle");
    }
}
package com.factoryPattern;
/**
 * 创建一个接口的实现类Rectangle
 */
public class Rectangle implements Shape{
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("inside in draw:draw a rectangle");
    }
}

以下为颜色的实现类:

//颜色的实现类
package com.factoryPattern;
/*
 * 创建实现接口的实体类。
 */
public class Blue implements Color{
    @Override
    public void fill() {
        // TODO Auto-generated method stub
        System.out.println("inside in Bule:fill blue");
    }
}
package com.factoryPattern;
/*
 * 创建实现接口的实体类。
 */
public class Green implements Color{
    @Override
    public void fill() {
        // TODO Auto-generated method stub
        System.out.println("inside in Green:fill green");
    }
}
package com.factoryPattern;
/*
 * 创建实现接口的实体类。
 */
public class Red implements Color{
    @Override
    public void fill() {
        // TODO Auto-generated method stub
        System.out.println("inside in Red:fill red");
    }
}

3、为 Color 和 Shape 对象创建抽象类来获取工厂

package com.factoryPattern;
/*
 * 为 Color 和 Shape 对象创建抽象类来获取工厂。
 */
public abstract class AbstractFactory {
    abstract Shape getShape(String shapeType);
    abstract Color getColor(String colorType);
}

4、创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象

package com.factoryPattern;
public class ColorAFactory extends AbstractFactory{
    @Override
    Shape getShape(String shapeType) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    Color getColor(String colorType) {
        // TODO Auto-generated method stub
        if(colorType.equalsIgnoreCase("RED")){
            return new Red();
        }else if(colorType.equalsIgnoreCase("BLUE")){
            return new Blue();
        }else if(colorType.equalsIgnoreCase("GREEN")){
            return new Green();
        }
        return null;
    }
}

package com.factoryPattern;
public class ShapeAFactory extends AbstractFactory{
    @Override
    Shape getShape(String shapeType) {
        // TODO Auto-generated method stub
        if(shapeType==null || "".equals(shapeType)){
            return null;
        }else 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 colorType) {
        // TODO Auto-generated method stub
        return null;
    }
}

5、创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂

package com.factoryPattern;
/*
 * 创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if("share".equals(choice)){
            return new ShapeAFactory();
        }else if("color".equals(choice)){
            return new ColorAFactory();
        }
        return null;
    }

}

7、使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。

//使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。
package com.factoryPattern;
public class AbstractFactoryPatternDemo {
    public static void main(String[] args) {
        //获取形状工厂
          AbstractFactory shapeFactory = FactoryProducer.getFactory("share");
          //获取形状为 Circle 的对象
          Shape shape1 = shapeFactory.getShape("CIRCLE");
          //调用 Circle 的 draw 方法
          shape1.draw();
          //获取形状为 Rectangle 的对象
          Shape shape2 = shapeFactory.getShape("RECTANGLE");
          //调用 Rectangle 的 draw 方法
          shape2.draw();
          //获取形状为 Square 的对象
          Shape shape3 = shapeFactory.getShape("SQUARE");
          //调用 Square 的 draw 方法
          shape3.draw();
          //获取颜色工厂
          AbstractFactory colorFactory = FactoryProducer.getFactory("color");
          //获取颜色为 Red 的对象
          Color color1 = colorFactory.getColor("RED");
          //调用 Red 的 fill 方法
          color1.fill();
          //获取颜色为 Green 的对象
          Color color2 = colorFactory.getColor("Green");
          //调用 Green 的 fill 方法
          color2.fill();
          //获取颜色为 Blue 的对象
          Color color3 = colorFactory.getColor("BLUE");
          //调用 Blue 的 fill 方法
          color3.fill();
       }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值