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

How to get started with Builder Mode in web Development

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about how to get started with the builder model in web development. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Today we are going to talk about the builder pattern in the three design patterns (creative pattern, behavioral pattern, and structural pattern), which can also be called Builder pattern.

Like other creative patterns such as factory patterns, they are used to serve the same goals, but their scenarios are different and implemented in different ways, but the ultimate goal is the same: to let us write high-quality code that is well-structured, easy to understand and easy to extend.

Builder model

What is a builder? What is his application scenario?

When we need to instantiate a complex class to get objects with different structural types and different internal states, we can encapsulate their enumerated operation logic with different classes, which we call builders.

When we need to come from the same class, but have different structural objects, we can instantiate it by constructing another builder.

-the above definition comes from the Beauty of Design patterns.

In order to deepen our understanding, let's have another flow chart.

From the picture, we can see that the builders are mainly divided into four roles:

Product (product class): the specific class objects that we need to generate

Builder (abstract builder class): build different module attributes for the class objects we need to generate, that is, publicly build the properties of the product class and hide other functions of the product class.

ConcreteBuilder (concrete builder class): implement the class object we want to generate

Director (director class): determine which module attributes are used to build our class object, which can be handled directly through client without this role in practical application.

Give an example

There are many different types of goods in e-commerce, such as ordinary physical goods, electronic card voucher goods, virtual video learning goods and so on. They are all commodities but their attributes are different. E-card coupons: unique coupon codes, learning videos: unique video links, etc.

So how can we achieve this kind of created goods?

Let's first take a look at the most common way to create:

Let's first create a basic commodity Item class:

Here we can see that we can completely create the type of commodity we want according to the request type, but it is impossible for a commodity attribute to have only such a few attributes, so what about expanding more in the future? Then this code will look bloated and difficult to maintain.

Next, let's take a look at how the builder pattern is implemented:

Step 1: create our abstract builder class. Here we look at three abstract methods to determine different types of goods, and we call different methods to achieve the idea of decoupling.

Step 2: create a concrete builder class. Implement the assignment of the abstract method of the abstract builder class to achieve the result we need.

Step 3: create our director category. Guide us how to create objects, which we can simplify, depending on the specific use of the scene to determine it!

Finally, it depends on our test results. In fact, when omitting the director category, we can also build the results we want, because I am writing the test demo, so I did not write the pass parameters, which can be modified according to their own actual application scenarios.

Compared with the ordinary writing method, the builder mode makes this code more readable and easy to expand, and different types of goods achieve the effect of decoupling.

Example 2:

Suppose we now have another scenario, when we copy an item, when we do not fill in the inventory, we default to 0, when the user filled in, our inventory can not be greater than 999999999.

So how are we going to do that?

PS: the function of copying goods is a very common operation in the field of e-commerce, which simplifies the operation cost and improves the physical examination of users. Technology serves the business, which determines the long-term interests of the company.

We created an ItemBuilder internally to handle our validation logic. Of course, it is also possible for us to use the ordinary get,set method.

If you look at this, some people may ask, how is this different from using the get or set methods?

Explanation: mainly to solve the problem that our assignment is in an invalid state

Invalid state refers to the dependency relationship between object attributes, legal check, etc. If set is used, the relationship and verification will not be verified, and all may have invalid states, that is, An and B attributes must be set at the same time, which is indispensable, and then the set method may lead to omissions, etc.

Only when we understand what problems each design pattern solves, can we know which scenarios are combined with what patterns or multiple design patterns to avoid forcing the use of design patterns and making the code more difficult to maintain.

The above is how to get started with the builder model in web development, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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