Abstract factory pattern

Abstract Factory Pattern is to create other factories around one super factory. This super factory is also called the factory of other factories. This type of design pattern is a creation pattern, which provides the best way to create objects.
In the abstract factory pattern, an interface is the factory that is responsible for creating a related object, and there is no need to explicitly specify their classes. Each generated factory can provide objects according to the factory pattern.

intention

Provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Realization

  • Create interface
    public interface Book{
        void BookName();
    }

    public interface Grade{
        void grade();
    }
  • Create entity class
    public static class MathBook implements Book {

        @Override
        public void BookName() {
            System.out.println(this.getClass().getName());
        }
    }

    public static class EnglishBook implements Book {

        @Override
        public void BookName() {
            System.out.println(this.getClass().getName());
        }
    }

    public static class Primary implements Grade {

        @Override
        public void grade() {
            System.out.println(this.getClass().getName());
        }
    }

    public static class Intermediate implements Grade {

        @Override
        public void grade() {
            System.out.println(this.getClass().getName());
        }
    }

    public static class Senior implements Grade {

        @Override
        public void grade() {
            System.out.println(this.getClass().getName());
        }
    }
  • Create an abstract factory
    public static abstract class AbstractFactory{
        abstract Book getBook(String name);
        abstract Grade getGrade(String level);
    }
  • Create factory
    public static class BookFactory extends AbstractFactory{

        @Override
        Book getBook(String name) {
            switch (name){
                case "Math":
                    return new MathBook();
                case "English":
                    return new EnglishBook();
            }
            return null;
        }

        @Override
        Grade getGrade(String level) {
            return null;
        }
    }


    public static class GradeFactory extends AbstractFactory{

        @Override
        Book getBook(String name) {
            return null;
        }

        @Override
        Grade getGrade(String level) {
            switch (level){
                case "Primary":
                    return new Primary();
                case "Intermediate":
                    return new Intermediate();
                case "Senior":
                    return new Senior();
            }
            return null;
        }
    }
  • Create abstract factory entity class
    public static class FactoryProducer{
        public static AbstractFactory getFactory(String factory){
            switch (factory){
                case "Book":
                    return new BookFactory();
                case "Grade":
                    return new GradeFactory();
            }
            return null;
        }
    }
  • Use
    public static void main(String... args) {
        AbstractFactory abstractFactory;
        Book book;
        Grade grade;

        abstractFactory = FactoryProducer.getFactory("Book");
        book = abstractFactory.getBook("Math");
        book.BookName();

        book = abstractFactory.getBook("Math");
        book.BookName();

        abstractFactory = FactoryProducer.getFactory("Grade");
        grade = abstractFactory.getGrade("Primary");
        grade.grade();

        grade = abstractFactory.getGrade("Intermediate");
        grade.grade();

        grade = abstractFactory.getGrade("Senior");
        grade.grade();
    }
  • Result
I/System.out: com.gjn.opencv.Model$MathBook
I/System.out: com.gjn.opencv.Model$EnglishBook
I/System.out: com.gjn.opencv.Model$Primary
I/System.out: com.gjn.opencv.Model$Intermediate
I/System.out: com.gjn.opencv.Model$Senior

We can see that the abstract factory pattern is the integration of multiple factory patterns.

We can see that both the factory pattern and the abstract factory pattern are for decoupling. When using the factory pattern type, look at the demand selection pattern!

data

Rookie tutorial

Tags: OpenCV

Posted on Mon, 04 May 2020 13:38:27 -0700 by nightdesigns