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

  • Definition: The abstract factory pattern provides an interface to create a series of related or interdependent objects
  • No need to specify their specific class
  • Type: Creation
  • Applicable scene
    • The client (application layer) does not depend on the details of how the product class instance is created and implemented
    • Emphasize that a series of related product objects (belonging to the same product family) are used together to create objects that require a lot of repetitive code
    • Provide a product class library, all products appear with the same interface, so that the client does not depend on the specific implementation
  • advantage
    • Specific products are isolated in the application layer code, no need to care about the creation details
    • Unify a series of product families together to create
  • Disadvantage
    • It specifies all the product sets that may be created. It is difficult to extend new products in the product family, and it is necessary to modify the interface of the abstract factory
    • Increased the abstraction and difficulty of understanding the system

The difference between product hierarchy and product family:

Insert picture description here

The factory method pattern is aimed at the product hierarchy, while the abstract method pattern is aimed at the product family.

In theory: when a factory can create all objects in a product family that belong to different product hierarchies, then at this time, the abstract factory pattern is simpler and more efficient than the factory method pattern.

Insert picture description here

2. Abstract Factory Coding

The business scenario is the same as the factory method in the previous chapter, and now a new requirement is put forward: each course must have not only a video, but also a corresponding note.

If you use the factory method to expand, at this time, you must have both the Java handwriting class, the Python handwriting class, and the handwriting abstract class, the handwriting factory, the Java handwriting factory, and the Python handwriting factory. At this time, the factory in FE's handbook is prone to explosion-like phenomena.

Now we use the abstract factory to create the same product family products together:

  1. Create abstract classes for courses and notes

    public abstract class Video {
          
          
        public abstract void produce();
    }
    
    public abstract class Article {
          
          
        public abstract void produce();
    }
    
  2. Create a course factory interface

    public interface CourseFactory {
          
          
        Video getVideo();
    
        Article getArticle();
    }
    
  3. Create Java course classes and note classes

    public class JavaVideo extends Video {
          
          
        @Override
        public void produce() {
          
          
            System.out.println("录制Java课程视频");
        }
    }
    
    public class JavaArticle extends Article {
          
          
        @Override
        public void produce() {
          
          
            System.out.println("编写Java课程手记");
        }
    }
    
  4. Course factory for creating Java product family

    public class JavaCourseFactory implements CourseFactory {
          
          
        @Override
        public Video getVideo() {
          
          
            return new JavaVideo();
        }
    
        @Override
        public Article getArticle() {
          
          
            return new JavaArticle();
        }
    }
    
  5. Test class

    public class Test {
          
          
        public static void main(String[] args) {
          
          
            CourseFactory courseFactory = new JavaCourseFactory();
            Video video = courseFactory.getVideo();
            Article article = courseFactory.getArticle();
            video.produce();
            article.produce();
        }
    }
    

    operation result:

    录制Java课程视频
    编写Java课程手记
    
  6. Now expand a Python product family

    public class PythonVedio extends Video {
          
          
        @Override
        public void produce() {
          
          
            System.out.println("录制Python课程视频");
        }
    }
    
    public class PythonArticle extends Article {
          
          
        @Override
        public void produce() {
          
          
            System.out.println("编写Python课程手记");
        }
    }
    
    public class PythonCourseFactory implements CourseFactory {
          
          
        @Override
        public Video getVideo() {
          
          
            return new PythonVedio();
        }
    
        @Override
        public Article getArticle() {
          
          
            return new PythonArticle();
        }
    }
    

The class diagram now looks like this:

Insert picture description here

Comparison of abstract factory and factory method:

  • Advantages: The abstract factory model unifies a series of product families to create, which is simpler and more efficient than the factory method model.
  • Disadvantages: It is difficult to extend new products in the product family, and the interface of the abstract factory needs to be modified. For example, if you want to add a source code product now, you must not only modify the CourseFactory interface, but also modify all implementation classes, which violates the open and closed principle.

Guess you like

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