Abstract factory pattern

  • Definition: The abstract factory pattern provides an interface to create a series of related or interdependent objects
  • There is no need to specify their specific class
  • Type: Create type
  • Applicable scenario

    • The client (application layer) does not depend on the details of how the product class instance is created, implemented, etc.
    • Emphasize that creating objects using a series of related product objects (belonging to the same product family) together requires a lot of repetitive code
    • Provide a library of product classes, all of which appear in the same interface, so that the client is not dependent on the implementation
  • Advantages:

    • The specific product is isolated in the application layer code, without worrying about the creation details
    • To create a unified product family of a series
  • Disadvantages:

    • Specifies the set of all possible products that can be created, the difficulty of extending new products in the product family, and the need to modify the interface of the abstract factory
    • It increases the abstractness and difficulty of understanding the system
  • Product class structure and product family

Code sample

public interface CourseFactory {
    Video getVideo();
    Article getArticle();
}
public abstract class Video {
    public abstract void produce();
}
public abstract class Article {
    public abstract void produce();
}
public class JavaCourseFactory implements CourseFactory{ @Override public Video getVideo() { return new JavaVideo(); } @Override public Article getArticle() { return new JavaArticle(); }}
Public class javaIdeo extends Video{@Override public void produce() {System.out.println();} public class javaIdeo extends Video{public void produce() {System.out.println(); }}
Public class JavaArticle extends Article{@Override public void produce() {System.out.println(); public class JavaArticle extends Article{@Override public void produce() {System.out.println(); }}
public class PythonCourseFactory implements CourseFactory{
    @Override
    public Video getVideo() {
        return new PythonVideo();
    }
    @Override
    public Article getArticle() {
        return new PythonArticle();
    }
}
Public class PythonVideo extends Video{public void produce() {System.out.println(); public class PythonVideo extends Video{public void produce() {System.out.println(); }}
Public class PythonArticle extends Article{@Override public void produce() {System.out.println(); public class PythonArticle extends Article{@Override public void produce() {System.out.println(); }}
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(); }}
Application scenarios

The JDK:

  • java.sql.Connection

    • Statment
    • PrepareStatement
  • java.sql.Statament
  • SqlSessionFactory (Mybatis)