Notes on "Java Design Patterns"-Chapter 4 Simple Factory Pattern

statement:

This blog is my notes after studying "Java Design Patterns". It is intended to facilitate review and review, and is not for commercial use.

This blog has indicated the source, if there is any infringement, please inform and delete it immediately.

1. Simple factory explanation

  • Definition: A factory object determines which product class instance is created
  • Type: Creation, but not a GOF23 design pattern
  • Applicable scene
    • The factory class is responsible for creating fewer objects
    • The client (application layer) only knows the parameters passed into the factory class and does not care about how to create the object (logic)
  • Advantages: only need to pass in a correct parameter, you can get the object you need without knowing the details of its creation
  • Disadvantages: The responsibilities of the factory category are relatively heavy. Adding new products requires modifying the judgment logic of the factory category, which violates the principle of opening and closing.

2. Simple Factory Coding

  1. Create a course video abstract class

    public abstract class Video {
          
          
        public abstract void product();
    }
    
  2. Create two course videos to inherit it

    public class JavaVideo extends Video {
          
          
        @Override
        public void product() {
          
          
            System.out.println("录制Java课程视频");
        }
    }
    
    public class PythonVideo extends Video {
          
          
        @Override
        public void product() {
          
          
            System.out.println("录制Python的课程视频");
        }
    }
    
  3. Create a simple factory class

    public class VideoFactory {
          
          
        public Video getVideo(String type) {
          
          
            if ("Java".equals(type)) {
          
          
                return new JavaVideo();
            } else if ("Python".equals(type)) {
          
          
                return new Python();
            }
            return null;
        }
    }
    
  4. Test class

    public class Test {
          
          
        public static void main(String[] args) {
          
          
            VideoFactory videoFactory = new VideoFactory();
            Video video = videoFactory.getVideo("Java");
            if (video == null) {
          
          
                return;
            }
            video.product();
        }
    }
    

    operation result:

    录制Java课程视频
    

The class diagram now looks like this:

Insert picture description here

However, if we want to record a FE front-end course, then we have to extend the methods in the factory class, so this does not conform to the principle of opening and closing.

Below we use reflection to transform the simple factory class:

  1. Modify the simple factory class

    public class VideoFactory {
          
          
        public Video getVideo(Class clazz) {
          
          
            Video video = null;
            try {
          
          
                video = (Video) Class.forName(clazz.getName()).newInstance();
            } catch (InstantiationException e) {
          
          
                e.printStackTrace();
            } catch (IllegalAccessException e) {
          
          
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
          
          
                e.printStackTrace();
            }
            return video;
        }
    }
    
  2. Test class

    public class Test {
          
          
        public static void main(String[] args) {
          
          
            VideoFactory videoFactory = new VideoFactory();
            Video video = videoFactory.getVideo(JavaVideo.class);
            if (video == null) {
          
          
                return;
            }
            video.product();
        }
    }
    

    operation result:

    录制Java课程视频
    

    Analysis: After using reflection to transform the simple factory, it conforms to the principle of opening and closing. After adding the course video, we only need to pass in new parameters, without modifying the simple factory class.

Guess you like

Origin blog.csdn.net/bm1998/article/details/113077636