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

Factory pattern of Design pattern-Abstract Factory pattern

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

Share

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

Factory pattern of design patterns-abstract factory pattern

Abstract Factory Pattern:

Defines an interface for creating clusters of related or dependent objects without explicitly specifying a specific class.

Let's look at a pizza project implemented using the abstract factory pattern.

Source: KaigeJava (kaigejava)

Let's look at the project structure:

Description:

1: Factory interface

2: Two implementation classes

3: Order object

4: Test category.

Look at the specific code:

IFactoryInterface:

BeiJinFatory:

FactoryOrderPizza

Test Category:

Three factory models:

1: Put the instantiation of objects in simple classes, then this is the simple factory pattern (also called static factory pattern)

2: If we abstract the object instantiation code into the method of creating the object and the concrete instantiation (that is, its subclass), this is the factory method pattern. The meaning of factory method pattern: the object creation process will be delayed from the original creation in the class to the creation of objects in the subclass.

Dependence on abstract principles:

1: Variables do not hold references to specific classes.

Understanding: A a = new A(); this way is that variable a holds a specific reference to object A. In factory mode, we can do this: A a = SF("A"); or A a = create("A"); or A a = new Factory(new CreateA());

2: Do not let classes inherit from concrete classes, but inherit from abstract interfaces or abstract classes.

We need to know if class A inherits a specific class B. Such strong dependencies are not easy to scale. And we use interfaces or abstract classes, then it is very convenient to extend.

3: Do not override methods already implemented in base classes

When we design a base class, we implement methods that are, in principle, common to all subclasses that inherit from the base class. A subclass that can be used without overriding. For example, in our pizza case, pizza baking, cutting and packaging methods.

Description:

With this article and the previous two articles, we have completed the explanation of factory patterns. However, to be honest, Kai Ge himself felt that he was not very clear. So, in the next article, Keg will compare the three models and summarize them. I hope the next article will explain the factory model.

The code has been uploaded to git.

Contact KaigeJava (kaigejava)

Or Kaige personal blog: www.kaigejava.com personal blog above can privately chat Kaige oh!

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