zoukankan      html  css  js  c++  java
  • 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");
        }
    }

    四.总结

  • 相关阅读:
    (转)悲催IT男的心酸恋爱史
    javascript递归函数问题
    《Professional Javascript For Web Developers 3rd Edition》读书笔记:javascript闭包中的this对象
    X509Store获取不到证书的问题
    [源码解析] PyTorch 分布式(7) DistributedDataParallel 之进程组
    [源码解析] PyTorch 分布式(8) DistributedDataParallel之论文篇
    [源码解析] PyTorch 分布式(10)DistributedDataParallel 之 Reducer静态架构
    [源码解析] PyTorch 分布式 Autograd (2) RPC基础
    [源码解析] PyTorch 分布式(9) DistributedDataParallel 之初始化
    [源码解析] PyTorch 分布式(12) DistributedDataParallel 之 前向传播
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12483527.html
Copyright © 2011-2022 走看看