3.简单工厂模式工厂方法模式与抽象工厂模式

Posted zhihaospace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.简单工厂模式工厂方法模式与抽象工厂模式相关的知识,希望对你有一定的参考价值。

一.简单工厂模式

1.介绍

  • 一个工厂类只能生产某一种产品,产品单一:例如简单工厂类只能生产圆形或矩形,不能生产2D的圆形或3D的圆形等子类型

技术图片

 

 

 2.UML图

技术图片

3.代码实现

Shape.java

public interface Shape {
   void draw();
}

Rectangel.java

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Rectangle");
    }
}

Circle.java

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Circle");
    }
}

ShapeFactory.java

public class ShapeFactory {
    
   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      }return null;
   }
}

SimpleFactoryDemo.java

public class SimpleFactoryDemo {

    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();

        //获取 Circle 的对象,并调用它的 draw 方法
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //调用 Circle 的 draw 方法
        shape1.draw();

        //获取 Rectangle 的对象,并调用它的 draw 方法
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //调用 Rectangle 的 draw 方法
        shape2.draw();
    }
}

二.工厂方法模式

1.介绍

  • 一个抽象工厂只能生产一种产品多种类型:例如Shape抽象工厂只能生产Shape类型产品,虽然可以生产子类型但是不能扩展更多类型

  • 定制化一种产品类型

技术图片

2.UML图

技术图片

 

 

 3.代码

Shape.java

public interface Shape {
    void draw();
}

Circle2D.java

public class Circle2D implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Circle2D");
    }
}

Circle3D.java

public class Circle3D implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Circle3D");
    }
}

Rectangle2D.java

public class Rectangle2D implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Rectangle2D");
    }
}

Rectangle3D.java

public class Rectangle3D implements Shape {
    @Override
    public void draw() {
        System.out.println("draw Rectangle3D");
    }
}

ShapeFactory.java

public abstract class ShapeFactory {

    public abstract Shape getShape(String shapeType);
}

ShapeFactory2D.java

public class ShapeFactory2D extends ShapeFactory {
    @Override
    public Shape getShape(String shapeType) {
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE2D")){
            return new Circle2D();
        } else if(shapeType.equalsIgnoreCase("RECTANGLE2D")){
            return new Rectangle2D();
        }

        return null;
    }
}

ShapeFactory3D.java

public class ShapeFactory3D extends ShapeFactory {
    @Override
    public Shape getShape(String shapeType) {
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE3D")){
            return new Circle3D();
        } else if(shapeType.equalsIgnoreCase("RECTANGLE3D")){
            return new Rectangle3D();
        }

        return null;
    }
}

FactoryMethodDemo.java

public class FactoryMethodDemo {

    public static void main(String[] args) {

        ShapeFactory shapeFactory = new ShapeFactory2D();

        Shape shape1 = shapeFactory.getShape("CIRCLE2D");
        shape1.draw();

        Shape shape2 = shapeFactory.getShape("Rectangle2D");
        shape2.draw();

        shapeFactory = new ShapeFactory3D();

        Shape shape3 = shapeFactory.getShape("CIRCLE3D");
        shape3.draw();

        Shape shape4 = shapeFactory.getShape("Rectangle3D");
        shape4.draw();

    }
}

三.抽象工厂模式

1.介绍

  • 一个抽象工厂能生产多种类型的多种型号

  • 定制化多种产品的多种类型

2.UML类图

技术图片

 

 

 

3.代码

AbstractFactory.java

public abstract class AbstractFactory {

    public abstract Shape getShape(String shapeType);

    public abstract Color getColor(String colorType);
}

Graphics2DFactory.java

public class Graphics2DFactory extends AbstractFactory {


    @Override
    public Shape getShape(String shapeType) {
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE2D")){
            return new Circle2D();
        } else if(shapeType.equalsIgnoreCase("RECTANGLE2D")){
            return new Rectangle2D();
        }

        return null;
    }

    @Override
    public Color getColor(String colorType) {
        if(colorType == null){
            return null;
        }
        if(colorType.equalsIgnoreCase("RED")){
            return new Red();
        } else if(colorType.equalsIgnoreCase("BLUE")){
            return new Blue();
        }

        return null;
    }
}

Graphics3DFactory.java

public class Graphics3DFactory extends AbstractFactory {

    @Override
    public Shape getShape(String shapeType) {
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE3D")){
            return new Circle3D();
        } else if(shapeType.equalsIgnoreCase("RECTANGLE3D")){
            return new Rectangle3D();
        }

        return null;
    }

    @Override
    public Color getColor(String colorType) {
        if(colorType == null){
            return null;
        }
        if(colorType.equalsIgnoreCase("RED")){
            return new Red();
        } else if(colorType.equalsIgnoreCase("BLUE")){
            return new Blue();
        }

        return null;
    }
}

AbstractFactoryDemo.java

public class AbstractFactoryDemo {
    public static void main(String[] args) {
        AbstractFactory abstractFactory = new Graphics2DFactory();

        Shape shape = abstractFactory.getShape("Circle2D");
        Color color = abstractFactory.getColor("Red");

        shape.draw();
        color.fill();

    }
}

Color.java

public interface Color {

    void fill();
}

Red.java

public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("Red");
    }
}

Blue.java

public class Blue implements Color {
    @Override
    public void fill() {
        System.out.println("Blue");
    }
}

四.总结

技术图片

 

以上是关于3.简单工厂模式工厂方法模式与抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

深入理解设计模式-简单工厂模式vs工厂方法模式vs抽象工厂模式对比讲解

深入理解设计模式-简单工厂模式vs工厂方法模式vs抽象工厂模式对比讲解

工厂模式:简单工厂工厂方法抽象工厂

设计模式:简单工厂工厂方法抽象工厂之小结与区别

设计模式笔记-简单工厂模式,工厂方法模式,抽象工厂模式

设计模式笔记-简单工厂模式,工厂方法模式,抽象工厂模式