banner



abstract factory design pattern in java video

0x01. Definition and Type

  • Definition: The abstract factory pattern provides an interface for creating a series of related or interdependent objects
  • There is no need to specify their specific classes
  • Type: Creative
  • UML

Abstract factory pattern of design pattern

  • Basic Java implementation
            /**  * Factory Interface  */ public interface IFactory {     AProduct1 createProduct1();     AProduct2 createProduct2(); }  /**  * product 1  */ public abstract class AProduct1 {     public abstract void produce(); }  /**  * product 2  */ public abstract class AProduct2 {     public abstract void produce(); }  /**  * Factory Realization  */ public class FactoryImpl implements IFactory {     @Override     public ProductImpl1 createProduct1() {         return new ProductImpl1();     }     @Override     public ProductImpl2 createProduct2() {         return new ProductImpl2();     } }  /**  * product 1实现  */ public class ProductImpl1 extends AProduct1 {     @Override     public void produce() {         System. out. println ("Product 1");     } }  /**  * product 2实现  */ public class ProductImpl2 extends AProduct2 {     @Override     public void produce() {         System. out. println ("Product 2");     } }  /**  * Testing and Application  */ public class Test {     public static void main(String[] args) {         FactoryImpl factory = new FactoryImpl();         ProductImpl1 productImpl1 = factory.createProduct1();         ProductImpl2 productImpl2 = factory.createProduct2();         productImpl1.produce();         productImpl2.produce();     } }          
  • Output result
            Product 1 Product 2          
  • Elements in abstract factories

    • Factory Interface: In the abstract factory model, factory interface defines that the same type of factory can produce many different products, and defines the types of products. When the type of factory is changed, the method in the interface should be adjusted. This violates the open-close principle, but expanding different types of factory only needs to realize the interface.
    • Factory Implementation Class: It defines specific factories and specific generated types, and there are as many specific implementation classes as there are medium-sized factories.
    • Abstract classes of specific products: a factory may produce different products. abstract classes define the types of products produced by the factory. there are as many abstract classes as there are methods in the abstract factory interface.
    • Specific product realization: The final product determines the specific behavior of the product in the client.

0x02. Applicable scenarios

  • Client (application layer) does not depend on details such as how product class instances are created, implemented, etc.
  • Emphasis is placed on a series of related product objects (belonging to the same product family) that require a lot of repetitive code to create objects together
  • Provide a library of product classes, all products appear in the same interface, so that the client does not depend on specific implementation

0x03. advantages

  • Specific products are segregated at the application level without concern for creating details.
  • Unify a series of product families to create together
  • The product family is constrained within the class. The so-called product family, generally more or less, has a certain relationship. The abstract factory pattern can define and describe the product family relationship within the class, without introducing a new class to manage it.

0x04. drawback

  • It specifies all possible product sets to be created. It is difficult to extend new products in the product family. It is necessary to modify the interface of abstract factories.
  • It increases the abstraction of the system and the difficulty of understanding.

0x05. Product Grade Structure and Product Family Introduction

Abstract factory pattern of design pattern
Abstract factory pattern of design pattern

  • Take household appliances for example:

    • Beautiful: Air conditioner, water heater, refrigerator, washing machine
    • Haier: Air conditioning, water heater, refrigerator, washing machine
    • All products of the United States belong to a product family.
    • Mei Mei's water heater and Haier's water heater belong to one product grade.
    • BeautifulandHaierBelong to different factories
    • Air conditioner, water heater, refrigerator, washing machineDifferent products produced in the same factory

0x06. Sample Implementation

Introduction: Every course on Mucho has handwriting, video and so on. The course is divided into Java course and Python course.

  • Course Factory Category
            // Course Factory public interface ICourseFactory {     Video getVideo();     Article getArticle(); }          
  • Articles/Video Abstraction Classes
            // Articles abstract class public abstract class Article {     public abstract void produce(); }  // Video abstraction class public abstract class Video {     public abstract void produce(); }          
  • Implementation of Java Course
            // Java Course Factory public class JavaCourseFactory implements ICourseFactory {     @Override     public JavaVideo getVideo() {         return new JavaVideo();     }      @Override     public JavaArticle getArticle() {         return new JavaArticle();     } }  //Java video public class JavaVideo extends Video {      @Override     public void produce() {         System. out. println ("Recording JAVA Course Video");     } }  //Java article public class JavaArticle extends Article {     @Override     public void produce() {         System. out. println ("Writing Java Course Notes");     } }          
  • Python Course Implementation
            // Python Course Factory public class PythonCourseFactory implements ICourseFactory {     @Override     public Video getVideo() {         return new PythonVideo();     }      @Override     public Article getArticle() {         return new PythonArticle();     } }  // Python Video Implementation public class PythonVideo extends Video {     @Override     public void produce() {         System. out. println ("Recording Python Video");     } }  // Python articles public class PythonArticle extends Article {     @Override     public void produce() {         System. out. println ("Writing Python Course Notes");     } }          
  • Test and Application Classes
            public class Test {     public static void main(String[] args) {         // Java product family         ICourseFactory courseFactory = new JavaCourseFactory();         Video video = courseFactory.getVideo();         Article article = courseFactory.getArticle();         video.produce();         article.produce();     } }          
  • Output result
            Recording JAVA Course Video Writing Java Course Notes          
  • Sample UML class diagram

Abstract factory pattern of design pattern

0x07. abstract factory in source code

  • java.sql.Connection

    • Statement
    • PreparedStatement
  • java.sql.Statement

    • executorSet
  • MyBatis: SqlSessionFactory

0x08. Source address

  • Abstract factory pattern: https://github.com/sigmako/design-pattern/tree/master/abstract-factory

0x09. reference

  • Exquisite lecture on the design mode of Muchou Network: https://coding.imooc.com/class/270.html
  • 23 design patterns (3): Abstract Factory Pattern: https://blog.csdn.net/zhengzhb/article/details/7359385

abstract factory design pattern in java video

Source: https://developpaper.com/abstract-factory-pattern-of-design-pattern/

Posted by: dowlingwitir1961.blogspot.com

0 Response to "abstract factory design pattern in java video"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel