Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the factory patterns in the web design pattern

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what factory patterns are in web design patterns". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Simple Factory Model

Simple Factory Pattern: Also known as Static Factory Method pattern, it belongs to class creation pattern. In the simple factory pattern, instances of different classes can be returned depending on the parameters. The simple factory pattern defines a class that is responsible for creating instances of other classes, often with a common parent class.

mode structure

The Simple Factory pattern contains the following roles:

Factory: Factory role, responsible for implementing the internal logic that creates all instances

Product: abstract product role, abstract product role is the parent class of all objects created, responsible for describing the common interface common to all instances

ConcreteProduct: A product-specific role is a creation target, and all objects created act as instances of a specific class of that role.

public static void main(String[] args) { //Resource loading ClassPathResource classPathResource = new ClassPathResource("spring-bean.xml"); // XmlBeanFactory loads resources and resolves registration beans BeanFactory beanFactory = new XmlBeanFactory(classPathResource); // BeanFactory.getBean(); UserBean userBean = (UserBean) beanFactory.getBean("userBean"); System.out.println(userBean.getName());}

This XmlBeanFactory can be seen as a slightly deformed simple factory, getBean() method is to get the instance method of the product, userBean is our product. If we encounter a scenario similar to XmlBeanFactory in spring later, we can write a beautiful simple factory by drawing gourd.

factory method pattern

Also known as Virtual Constructor pattern or Polymorphic Factory pattern, it belongs to class creation pattern. In the factory method pattern, the factory parent is responsible for defining the common interface for creating product objects, while the factory subclass is responsible for generating specific product objects. The purpose of this is to delay the instantiation of the product class to the factory subclass, that is, the factory subclass determines which specific product class should be instantiated.

mode structure

The factory method pattern contains the following roles:

Product: abstract product

ConcreteProduct: Specific Product

Factory: Abstract Factory

ConcreteFactory: Specific Factory

Source Guide

The java.util.Collection interface defines an abstract iterator() method, which is a factory method. Let's look at the iterator() implementation in ArrayList

@NotNull public Iterator iterator() { return new ArrayList.Itr();}

It new an inner class Itr of ArrayList and returns it, Itr:

private class Itr implements Iterator { int cursor; int lastRet = -1; int expectedModCount; Itr() { this.expectedModCount = ArrayList.this.modCount; } ...... ......}

Here ArrayList is a factory class for Iterator, and its iterator() method is the factory method that produces Iterator.

Abstract Factory

Abstract Factory Pattern: Provides an interface for creating a series of related or interdependent objects without specifying their specific classes. The abstract factory pattern, also known as the Kit pattern, is an object-creation pattern.

mode structure

The Abstract Factory pattern contains the following roles:

AbstractFactory: Abstract Factory

ConcreteFactory: Specific Factory

AbstractProduct: abstract product

Product: Specific product

Source Guide

We can see that the difference between abstract factory and factory methods is that abstractions add other methods of producing the associated product. It can be understood as an upgrade to the factory method. Let's look at the HashMap class:

HashMap objectHashMap = new HashMap(); Set entries = objectHashMap.entrySet(); Collection values = objectHashMap.values();

Here HashMap is an abstract factory, its values() and entrySet() are two factory methods, Collection and Set are products. Note: Abstract factory abstract meaning is the abstraction of the product, no longer a product, but a series of products, factory pattern class names generally end with factory.

"What are the factory patterns in web design patterns" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report