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 the Kotlin and Java Builder pattern

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

Share

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

This article focuses on "how to implement the Kotlin and Java builder model". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the Kotlin and Java builder model.

The Director communicates the requirements directly with the Client.

After communication, the commander divides the customer's requirements for creating products into construction requests (Builder) for each component.

Delegate the construction request of each part to the specific builder (ConcreteBuilder)

Each specific builder is responsible for the construction of product components.

Finally build into a specific product (Product).

The construction model is very simple. For example, computer components generally do not change much, but computer assembly is a complex process.

UML figure:

1. Create a build request

Abstract class Builder {/ / first step: install CPU / / as an abstract method, implement abstract fun buildCPU () / / by subclasses. Step 2: install motherboard / / declare as abstract methods, and implement abstract fun buildMainboard () / / by subclasses. Step 3: install hard disk / / declare as abstract methods The method to realize abstract fun buildHD () / / return the product by subclass: get the assembled computer abstract fun getComputer (): Computer}

two。 Delegate the construction request to the builder for implementation

Class ConcreteBuilder: Builder () {/ / create product instance private var computer = Computer () / assemble product override fun buildCPU () {computer.add ("assemble CPU")} override fun buildMainboard () {computer.add ("assemble motherboard")} override fun buildHD () {computer.add ("assemble hard drive")} / / return Return to the assembled computer override fun getComputer (): Computer {return computer}}

3. Command assembly element

Class Director {/ / directs the installer to assemble the computer fun construct (builder: Builder) {builder.buildCPU () builder.buildMainboard () builder.buildHD ()}}

4. Define specific product categories (Product): computers

Class Computer {/ / Collection of computer components privateval parts = ArrayList () / / used to assemble components into a computer fun add (part: String) {parts.add (part)} fun show () {for (I in parts.indices) {println ("components" + parts [I] + "installed")} println ("computer assembled" Please accept ")}}

Client call

Object BuilderTest {@ JvmStatic fun main (args: Array) {/ / after visiting for a long time, finally found a suitable computer store / / found the owner of the store and the installer val director = Director () val builder = ConcreteBuilder () / / after the boss asked the installer to install the computer director.construct (builder) / / after the installation was finished. The assembler moved to the assembled computer val computer = builder.getComputer () / / the assembler showed the computer to Xiaocheng to see computer.show ()}.

Result output

Each specific builder is relatively independent, and has nothing to do with other specific builders, so it is convenient to replace specific builders or add new specific builders. Users can get different product objects by using different specific builders.

At this point, I believe you have a deeper understanding of "how to implement the Kotlin and Java builder pattern". 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