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 is an abstract factory in Java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what is an abstract factory in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Abstract factory pattern 1. What is an abstract factory

In real life, many factories are integrated factories that can produce many kinds of products. Take the Coke in the case, for example, there may be a Christmas version of Coke during the holidays, or there may be a New year's version of Coke (hypothetical if it does not exist). The abstract factory model to be introduced in this article will consider the production of multi-grade products. A group of products at different levels produced by the same specific factory is called a product family.

For example:

Definition of abstract factory pattern:

It is a pattern structure that provides an interface for the access class to create a set of related or interdependent objects, and the access class can get different levels of products of the same family without specifying the specific class of the desired product.

The abstract factory pattern is an upgraded version of the factory method pattern, which produces only one level of products, while the abstract factory pattern produces multiple levels of products.

2. Advantages and disadvantages of abstract factory model.

The following conditions are generally met when using the abstract factory pattern:

There are multiple product families in the system, and each specific factory creates products of the same family but belong to different hierarchical structures.

The system can only consume one family of products at a time, that is, products of the same family can be used together.

Advantages:

The associated multi-level products in the product family can be managed together within the class, without having to introduce several new classes to manage them.

When a product family is needed, the abstract factory ensures that the client always uses only product groups of the same product

Abstract factory enhances the expansibility of the program. When a new product family is added, the original code does not need to be modified and the opening and closing principle is satisfied.

Disadvantages:

When a new product needs to be added to the product family, all factory classes need to be modified. It increases the abstraction and difficulty of understanding the system.

3. The structure and implementation of abstract factory pattern.

Like the factory method pattern, the abstract factory model is also composed of four elements: abstract factory, concrete factory, abstract product and concrete product, but the number of methods in the abstract factory is different, so is the number of abstract products. Now let's analyze its basic structure and implementation.

The structure of the pattern:

Abstract factory: provides an interface for creating products. It contains multiple methods to create products, newProduct (), and can create multiple products of different levels.

Concrete factory: mainly to implement multiple abstract methods in the abstract factory to complete the creation of concrete products

Abstract product: defines the specification of the product, describes the main features and functions of the product, and the abstract factory pattern has multiple abstract products.

Specific product: the interface defined by the abstract product role is implemented and created by the concrete factory, which has a many-to-one relationship with the concrete factory.

4. Abstract factory method pattern code implementation

Here is the expansion of the Coke factory from the above two articles, which will be designed and implemented according to the following structure diagram:

Abstract Coke factory structure diagram (self-drawn):

Coca-Cola interface:

Public interface CocaCola {public void CocaCola ();}

Pepsi interface:

Public interface PepsiCola {public void PepsiCola ();}

Christmas Coca-Cola:

/ * method for producing Christmas Coca-Cola * / public class ChristmasCocaCola implements CocaCola {@ Override public void CocaCola () {System.out.println ("produced Christmas Coca-Cola");}}

New year's Coca-Cola:

/ * production of New year's version of Coca-Cola * / public class NewYearCocaCola implements CocaCola {@ Override public void CocaCola () {System.out.println ("New year's version of Coca-Cola");}}

Christmas Pepsi:

/ * method for producing Christmas version of Pepsi * / public class ChristmasPepsiCola implements PepsiCola {@ Override public void PepsiCola () {System.out.println ("produced Christmas version of Pepsi");}}

New year's version of Pepsi:

/ * * method for producing New year's version of Pepsi * / public class NewYearPepsiCola implements PepsiCola {@ Override public void PepsiCola () {System.out.println ("produced New year's version of Pepsi");}}

Coke Factory (Abstract Factory):

/ * Coke Factory (Abstract Factory) * can produce two versions of Coke * / public interface ColaFactory {/ / produce Coca-Cola: two versions of public CocaCola getCocaCola (); / / produce Pepsi: two versions of public PepsiCola getPepsi ();}

Christmas edition series cola factory:

/ * * Christmas series processing plants * produce Pepsi and public class ChristmasFactory implements ColaFactory for Christmas * / public class ChristmasFactory implements ColaFactory {@ Override public CocaCola getCocaCola () {return new ChristmasCocaCola ();} @ Override public PepsiCola getPepsi () {return new ChristmasPepsiCola ();}}

New year edition series cola factory:

/ * * New year's version processing plant * can produce New year's versions of Coca-Cola and Pepsi * / public class NewYearFactory implements ColaFactory {@ Override public CocaCola getCocaCola () {return new NewYearCocaCola ();} @ Override public PepsiCola getPepsi () {return new NewYearPepsiCola ();}}

Client (test class):

Public class Test {public static void main (String [] args) {/ / production of Christmas Coke: ColaFactory factory = new ChristmasFactory (); CocaCola cola = factory.getCocaCola (); cola.CocaCola (); / / production of Christmas version of Pepsi: ColaFactory factory1 = new ChristmasFactory (); PepsiCola pepsiCola = factory1.getPepsi (); pepsiCola.PepsiCola () / / produce New year's version of Coca-Cola: ColaFactory factory2 = new NewYearFactory (); CocaCola cola1 = factory2.getCocaCola (); cola1.CocaCola (); / / produce New year's version of Pepsi: ColaFactory factory3 = new NewYearFactory (); PepsiCola pepsiCola1 = factory3.getPepsi (); pepsiCola1.PepsiCola ();}}

Output result:

Produced the Christmas version of Coca-Cola.

Produced a Christmas version of Pepsi.

Produced the New year's version of Coca-Cola.

Produced the New year version of Pepsi.

This is the cola factory of the abstract factory method pattern, and a group of products at different levels produced by the same specific factory is called a product family.

5. The application scenario of abstract factory pattern

When the object to be created is a series of interrelated or interdependent product families, such as multiple beverages in a beverage factory

There are multiple product families in the system, but only one of them is used at a time. For example, one only drinks Christmas version of Coca-Cola and New year's version of Pepsi.

The class library of the product is provided in the system, and the interface of all products is the same, and the client does not rely on the creation details and internal structure of the product instance.

6. Extension of abstract factory pattern

When adding a new product family, you only need to add a new specific factory, and there is no need to modify the original code to meet the opening and closing principle.

When a new type of product needs to be added to the product family, all factory classes need to be modified and do not meet the opening and closing principle.

Note: when there is only one hierarchical product in the system, the abstract factory pattern will degenerate to the factory method pattern.

Finally: a picture understands the simple factory pattern, the factory method pattern, the abstract factory pattern:

This is the end of this article on "what is an abstract factory in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report