Notes on "Java Design Patterns"-Chapter 5 Factory Method 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. Explanation of Factory Method

  • Definition: Define an interface for creating objects, but let the class that implements this interface decide which class to instantiate, and the factory method delays the instantiation of the class to the subclass .
  • Type: Creation type
  • Applicable scene
    • Creating objects requires a lot of repetitive code
    • The client (application layer) does not depend on the details of how the product class instance is created and implemented
    • A class specifies which object to create through its subclasses
  • advantage
    • The user only needs to care about the factory corresponding to the required product, and does not need to care about the creation details
    • Adding new products conforms to the principle of opening and closing, improving scalability
  • Disadvantage
    • The number of classes is easy to be too much, increasing complexity
    • Increased the abstraction and difficulty of understanding the system

2. Factory Method Coding

The business scenario is the same as the simple factory in the previous chapter, we directly modify the code in the simple factory

  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的课程视频");
        }
    }
    

    Note: Both the Java video and the Python video are of the same product level, and the factory method is to solve the problem of product level. A product of the same type, we call it a product level; and a product of a different type, the same manufacturer, we call it a product family. Product level and product family are important concepts that distinguish factory methods and abstract methods.

  3. Unlike the simple factory, our factory class has been changed to an interface. VideoFactory only defines a default contract, and specific objects created are implemented by specific subclass factories.

    public interface VideoFactory {
          
          
        Video getVideo();
    }
    
  4. Create a two-course video factory to realize it

    public class JavaVideoFactory implements VideoFactory {
          
          
        @Override
        public Video getVideo() {
          
          
            return new JavaVideo();
        }
    }
    
    public class PythonVideoFactory implements VideoFactory {
          
          
        @Override
        public Video getVideo() {
          
          
            return new PythonVideo();
        }
    }
    
  5. Test class

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

    operation result:

    录制Java课程视频
    

The class diagram now looks like this:

Insert picture description here

Comparison of factory method and simple factory:

  • Advantages: When we need to add new products, we only need to add a new subclass factory, without modifying the parent class factory interface, and follow the opening and closing principle.
  • Disadvantages: the number of classes is easy to be too much, increasing complexity

Guess you like

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