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 java factory models?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "which java factory models are divided into", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "which java factory models are divided into"!

Definition:

Factory pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creative pattern, which provides the best way to create objects.

The main purpose of factory mode is to provide transition interface for creating objects, so as to shield and isolate the specific process of creating objects, so as to improve flexibility.

Factory patterns are divided into three types according to the degree of abstraction:

Simple factory mode (also known as static factory mode)

Factory method model (also known as pleomorphic factory)

Abstract factory pattern (also known as toolbox)

Simple factory model

Essentially, it is up to a factory class to dynamically determine which instances of product classes (which inherit from a parent class or interface) should be created based on the parameters passed in. The creation goal of the simple factory pattern, where all created objects are instances of a specific class that plays this role.

Factory method model

The factory method is a design pattern with very small granularity, because the representation of the pattern is only an abstract method. Define the interface used to create the object in advance, let the subclass decide to instantiate a specific class, that is, add an interface between the factory and the product, the factory is no longer responsible for the creation of the product, and the interface returns the specific class instance for different conditions. to be implemented by a concrete class instance.

Abstract factory pattern

A factory pattern used when there are multiple abstract roles. The abstract factory pattern provides an interface to the client to create multiple product objects without having to specify the specific product. It has multiple abstract product classes, each abstract product class can derive multiple concrete product classes, an abstract factory class can derive multiple concrete factory classes, and each concrete factory class can create multiple instances of specific product classes.

The factory method model should be used more in practice. Let's take the factory method model as an example.

(the example comes from Baidu to help understand)

Abstract product classes: define car vehicle classes

Public interface Car {

Void gotowork ();}

Define the actual product class, a total of two, bike and bus represent different vehicle classes

Public class Bike implements Car {@ Override public void gotowork () {System.out.println ("go to work by bike!") ;} public class Bus implements Car {@ Override public void gotowork () {System.out.println ("go to work by bus") ;}}

Define an abstract factory interface

Public interface ICarFactory {Car getCar ();}

Specific factory subclasses, creating different factory subclasses for each specific product class

Public class BikeFactory implements ICarFactory {@ Override public Car getCar () {return new Bike ();}} public class BusFactory implements ICarFactory {

@ Override public Car getCar () {

Return new Bus ();}}

A simple test class to verify that different factories can produce different product objects

Public class TestFactory {@ Test public void test () {ICarFactory factory = null; / / bike factory = new BikeFactory (); Car bike = factory.getCar (); bike.gotowork (); / / bus factory = new BusFactory (); Car bus = factory.getCar (); bus.gotowork ();}}

Advantages of the factory model:

1. A caller wants to create an object as long as he knows its name, which reduces the degree of coupling.

2. High expansibility. If you want to add a product, just extend a factory class. Make the code structure clearer.

3. Shielding the specific implementation of the product, the caller only cares about the interface of the product.

Disadvantages of the factory model:

Each time you add a product, you need to add a specific class and object implementation factory (here you can use the reflection mechanism to avoid it), which doubles the number of classes in the system and increases the complexity of the system to a certain extent. at the same time, it also increases the dependence of system specific classes. So for simple objects, using the factory pattern adds complexity.

Applicable scenarios for factory mode:

1, an object has many subclasses.

2. Many additional operations are required to create an object.

3. The system needs to be expanded frequently in the later stage, which leaves the task of object instantiation to the implementation class and has good expansibility.

Some common questions about factory patterns in Java:

Using the downward transformation of the parent class (using the reference of the parent type to the object of the subclass) can achieve an effect similar to the factory pattern, so why use the factory pattern?

Assigning a parent class reference to a subclass object to a subclass reference is called a downward transition, such as:

Class Student extends Person Person s = new Student (); s = (Student) person

When using downward transformation to instantiate a subclass on the client side, it relies heavily on the name of the specific subclass. When we need to change the constructor of the subclass, such as adding a parameter, or changing the class name of the subclass, all subclasses coming out of the new need to be changed.

But if we use the factory mode, we just need to modify the new code in the factory, and all the other projects that use this example will change, instead of having to do it manually.

At this point, I believe you have a deeper understanding of "which java factory models are divided into". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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