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 implement BUILDER pattern by Python

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

Share

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

This article mainly introduces "how to achieve BUILDER mode in Python". In daily operation, I believe many people have doubts about how to achieve BUILDER mode in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve BUILDER mode in Python". Next, please follow the editor to study!

Realize

There is usually an abstract Builder class that defines an operation for each component that the guide may require to create. These operations do nothing by default. A ConcreteBuilder class redefines these operations for the artifacts it is interested in creating.

Again, this is not a patent for Builder schemas, as long as there is an abstract / concrete relationship.

Here are some other implementation issues to consider:

1) Assembly and construction interface

Generators gradually construct their products. Therefore, Builder class interfaces must be common enough to construct products for various types of concrete generators.

This was also mentioned in the previous article, and it is common enough to mean that it is enough for each concrete class to generate its own product.

A key design problem is the model of the construction and assembly process. The result of the construction request is simply added to the product, and such a model is usually sufficient. In the case of RTF, the generator converts the next tag and adds it to the body it has already converted.

But sometimes you may need to access product parts that have already been constructed. In the Maze example given in the code example section, the MazeBuilder interface allows you to add a door between existing rooms. Bottom-up tree structures such as parsing trees are another example. In this case, the generator returns the child nodes to the guide, which then passes them back to the generator to create the parent node.

What this passage is trying to say is that sometimes you need to return the created node to Director for finer control. We couldn't find an example of using the Builder pattern in the parsing tree, so we wrote the following example of building a menu. The first is the abstract class:

You can then write code like this:

The execution result of the program is as follows:

The MenuTester,ItemCheck here has nothing to do with the previous Menu and MenuItem.

Comparing the two Builder implementations, we can also find that the abstract behavior is the same, but the concrete behavior is completely different.

The code on the user side is almost the same:

The content is a series of test actions.

Users can build completely different products without knowing it, as long as they use different representations of Builder.

At this point, the study on "how to implement the BUILDER pattern in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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