【Example of application scenarios of builder Mode]
For example, if you are playing the "need for speed" game, the map on each level will be ever-changing. Simply put, the map will be divided into Sunny Days and cloudy days, when creating a map, you need to render the scene on the map on a sunny or cloudy day, such as the sky, tree, house, and road surface. This process is fixed, each time you create a new map, you need to perform the rendering. This is for computers with advanced configurations.
Now there are not a few people with low-configuration computers, so they cannot play games anymore? Yes! You only need to remove rendering that occupies a relatively high level of resources in the map. For example, you need to create different maps for a tree with reflection light and shade, but the map creation process is fixed, the builder mode can fully cope with such situations.
【BuilderMode description]
Type: Creation Mode
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
【BuilderMode UML diagram]
【BuilderMode-Java code implementation]
Create map interface:
Package map_package; Public interface map_interface { Public void create_weather (); Public void create_house (); Public void create_tree (); Public void create_way (); } |
Create a sunny map:
Package map_package; Public class map_sun implements map_interface { Public void create_weather (){ System. Out. println ("Sunday "); } Public void create_house (){ System. Out. println ("glass on the room is shiny "); } Public void create_tree (){ System. Out. println ("the color of the tree is light green "); } Public void create_way (){ System. Out. println ("road surface is dry "); } } |
Create a cloudy map:
Package map_package; Public class map_cloudy implements map_interface { Public void create_weather (){ System. Out. println ("Cloudy "); } Public void create_house (){ System. Out. println ("the glass on the room is dark "); } Public void create_tree (){ System. Out. println ("the color of the tree is dark green "); } Public void create_way (){ System. Out. println ("the road surface is wet "); } } |
Create a high-definition builder class:
Package map_build; Import map_package.map_interface; Public class map_build_adv { Private map_interface map_interface_ref; Public map_build_adv (map_interface map_interface_ref ){ Super (); This. map_interface_ref = map_interface_ref; } Public void create_map (){ System. Out. println ("Creating a high-definition map "); // The order of creation is very important to create from the sky to the road Map_interface_ref.create_weather (); Map_interface_ref.create_house (); Map_interface_ref.create_tree (); Map_interface_ref.create_way (); } } |
Create a low-definition builder class:
Package map_build; Import map_package.map_interface; Public class map_build_low { Private map_interface map_interface_ref; Public map_build_low (map_interface map_interface_ref ){ Super (); This. map_interface_ref = map_interface_ref; } Public void create_map (){ System. Out. println ("Creating a low-definition map "); // The order of creation is very important to create from the sky to the road Map_interface_ref.create_weather (); Map_interface_ref.create_house (); // Map_interface_ref.create_tree (); remove the tree creation process Map_interface_ref.create_way (); } } |
Create a client running class:
Package run_main; Import map_build.map_build_adv; Import map_build.map_build_low; Import map_package.map_cloudy; Import map_package.map_sun; Public class run_main { Public static void main (string [] ARGs ){ Map_cloudy = new map_cloudy (); Map_build_adv = new map_build_adv (map_cloudy ); Map_build_adv.create_map (); System. Out. println (); Map_sun = new map_sun (); Map_build_low = new map_build_low (map_sun ); Map_build_low.create_map (); } } |
The program running result is as follows:
Create a high-definition Map Cloudy The room is dark with glass The color of the tree is dark green. Wet road surfaceCreate a low-definition Map Sunny days Glass on the room is shining The road surface is dry. |
From the program, we can see that the builder mode encapsulates the constant creation process, and the creation process is separated from the main method. In this way, the internal creation process is separated from the code of the presentation layer, it facilitates the modification of the creation process function. In addition, we can find that the code design and functions are similar to the facade appearance mode. The difference is that the builder mode aims to get different results through different builders in the same build process, the appearance mode does not require different builders or different results, but simply combines several interfaces into an advanced interface without affecting the original results, the purpose is to make the call easier.
There are two creators in this program: the high painter builder and the Low-definition builder. They all encapsulate the map creation process, which is fixed, however, different builder classes can return non-style maps. The Builder specifies the object creation process. For example, the creation process of a high-definition builder is:
Map_interface_ref.create_weather (); Map_interface_ref.create_house (); Map_interface_ref.create_tree (); Map_interface_ref.create_way (); |
You must execute four methods to create a high-definition map. If you do not use the builder mode, you can directly call the create_xxxx method of the map class. If there are dozens of create_xxxx methods, it is very likely that some of these methods will not be called to affect the effect of the final map. Therefore, we need to use the builder mode to define the map creation process. This is a "code of conduct ".