编程知识 cdmana.com

Abstract factory pattern of Java design pattern

Definition of abstract factory pattern :

   Abstract factory pattern refers to providing an interface to create a series of related or interdependent objects , There is no need to specify their specific class . The client does not have to specify the specific type of product , You can create product objects in multiple product families .

All products are implemented with the same interface , This makes the client independent of the implementation .

About product hierarchy and product family :

   About product hierarchy and product family , For example, you can see . Like Gree , beauty , Oaks can make air conditioners , Washing machines and other electrical appliances . Product family is easy to understand , It's air conditioning , Washing machines and other electrical appliances belong to the product family .

Although all manufacturers say their products are the best , But the consumer's present certainly has the good and bad grade division , So each manufacturer has a hierarchical structure .

Abstract factory pattern 4 A character :

  • Abstract factory : Declare an operation interface for creating abstract product objects .
  • Specific factory : Implement the operation of creating specific objects .
  • Abstract product : Declare an interface for a class of product objects .
  • Specific products : Define a product object that will be created by the corresponding specific factory , Implement abstract product interface .

The abstract factory pattern is in spring Use in source code :

  spring Medium BeanFactory The corresponding role is the abstract factory , There are many specific factories , Here are two ClassPathXmlApplicationContext and FileSystemXmlApplicationContext.

Abstract products are in spring It's special in China , because spring The creation of specific products in is ultimately implemented by reflection , Just as Class Class is abstract product . The specific product is handed over to... In the project spring Various classes of Management

The object of production .



  
package com.liuyi;

import com.liuyi.entity.Person;
import com.liuyi.entity.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
* @ClassName Test
* @description:
* @author:liuyi
* @Date:2020/11/10 22:00
*/
public class Test {
public static void main(String[] args) {
BeanFactory ctx = new ClassPathXmlApplicationContext("spring.xml");
User user = (User)ctx.getBean("user");
System.out.println(user);
BeanFactory ctx2 = new FileSystemXmlApplicationContext("classpath:spring.xml");
Person person = (Person)ctx2.getBean("person");
System.out.println(person);
}
}
 

   You can see that in the code , Client acquisition bean Objects can be used BeanFactory Of getBean Method , In other words, the client does not depend on the specific implementation , And I can go through different specific factories ( Product hierarchy )

(ClassPathXmlApplicationContext and FileSystemXmlApplicationContext) Producing different class objects ( Product family ).

The advantages of abstract factory pattern :

  • When a product family is needed , Abstract factory can ensure that the client always uses only the product family of the same product .
  • Abstract factories enhance the extensibility of programs , For the addition of new products , You just need to implement a new specific factory , There is no need to modify the existing code , Comply with opening and closing principle .

Disadvantages of the abstract factory pattern :

  • Specifies all product sets that may be created , It's difficult to expand new products in the product family , The interface of the abstract factory needs to be modified .
  • Increases the abstractness of the system and the difficulty of understanding .

版权声明
本文为[A calf carrying a load]所创,转载请带上原文链接,感谢

Scroll to Top