设计模式-创造(1)-抽象工厂模式

设计模式-创造-抽象工厂模式

  • 简述
  • 代码
      • Shape
      • Circle
      • Rectangle
      • 工厂类ShapeFactory
  • 测试
      • 代码
      • 结果

简述

意图:提供一个接口,无需指定他们具体的类,这个接口可以创建一系列相关或相互依赖对象。

优点

  • 创建对象方便。根据关键字(比如一个字符串)就可以创建一个对象
  • 扩展性高。增加一个具体类即增加一个产品
  • 封装性。屏蔽产品实现

缺点

  • 每增加一个产品,都需要增加一个具体类和修改工厂工厂,类数不断增加,在一定程度上增加了系统的复杂度

代码

Shape

package com.ydfind.gof.factory;

/**
 * 图形 接口
 * @author ydfind
 * @date 2019.10.14
 */
public interface Shape {

    /**
     * 绘制
     */
    void draw();
}

Circle

package com.ydfind.gof.factory;

/**
 * 图
 * @author ydfind
 * @date 2019.10.14
 */
public class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("this is a circle.");
    }
}

Rectangle

package com.ydfind.gof.factory;

/**
 * 矩形
 * @author ydfind
 * @date 2019.10.14
 */
public class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("this is a rectangle.");
    }
}

工厂类ShapeFactory

package com.ydfind.gof.factory;

/**
 * 图形工厂类
 * @author ydfind
 * @date 2019.10.14
 */
public class ShapeFactory {

    public static final String SHAPE_CIRCLE = "circle";

    public static final String SHAPE_RECTANGLE = "rectangle";

    /*******************************简单-工厂*********************************************/
    /**
     * 根据关键字创建对象
     */
    public Shape getShape(String shapeType){
        Shape shape;
        switch (shapeType){
            case SHAPE_CIRCLE:
                shape = new Circle();
                break;
            case SHAPE_RECTANGLE:
                shape = new Rectangle();
                break;
            default:
                shape = null;
        }
        return shape;
    }

    /*****************************多方法-工厂***********************************************/
    public Shape getCircle(){
        return new Circle();
    }

    public Shape getRectangle(){
        return new Rectangle();
    }

    /*****************************静态-工厂***********************************************/
    public static Shape getStaticCircle(){
        return new Circle();
    }

    public static Shape getStaticRectangle(){
        return new Rectangle();
    }
}

测试

代码


        
            junit
            junit
            4.12
            test
        
import com.ydfind.gof.factory.Shape;
import com.ydfind.gof.factory.ShapeFactory;
import org.junit.Test;

/**
 * 23种设计模式测试类
 * @author ydfind
 * @date 2019.10.14
 */
public class GofPatternTest {

    /**
     * 工厂方法测试类
     */
    @Test
    public void testFactory(){
        ShapeFactory shapeFactory = new ShapeFactory();
        Shape shape;
        // 简单工厂
        shape = shapeFactory.getShape(ShapeFactory.SHAPE_CIRCLE);
        shape.draw();
        shape = shapeFactory.getShape(ShapeFactory.SHAPE_RECTANGLE);
        shape.draw();
        // 多方法工厂
        shape = shapeFactory.getCircle();
        shape.draw();
        shape = shapeFactory.getRectangle();
        shape.draw();
        // 静态工厂
        shape = ShapeFactory.getStaticCircle();
        shape.draw();
        shape = ShapeFactory.getStaticRectangle();
        shape.draw();
    }

}

结果

设计模式-创造(1)-抽象工厂模式_第1张图片

你可能感兴趣的:(设计模式)