In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The article begins with:
Creative model: builder model
The fourth of the five major creative models: the builder model.
Brief introduction
Name: builder mode
English name: Builder Pattern
Values: specializing in the treatment of loss.
Personal introduction:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Separate the construction of a complex object from its representation, so that the same construction process can create different representations.
(from the Zen of Design patterns)
What I'm going to tell you today is the builder model. The use scenario of the builder mode is to create complex objects. What is a complex object? If an object only needs to be created by new XXX (), it is a simple object; if you need new XXX (), and you have to set a lot of properties, then this kind of object can be called a complex object because of its complex construction process. By adopting the builder model, you can take away this complex build process so that it does not depend on the creator. Let's explain it through a story.
The story you want.
I still remember that when I was a child, I just began to learn to cook soup, but I didn't know how to cook it. The first time I boiled chestnut spareribs soup, because the chestnut was more difficult to cook, so I put it into the pot with ribs, then boiled it for 40 minutes, and then added salt to the pot. The second time I cooked the wax gourd spareribs soup, I didn't understand that the wax gourd was easy to cook, so I boiled it with the ribs for almost 40 minutes, with salt and parsley. I found that the soup was a little strong, and the wax gourd was all gone (all thoroughly cooked). When my mother saw the soup, she told me that the wax gourd was easy to cook. We had to boil the ribs first, and then put the wax gourd in it. It was only then that I realized that there was such a difference in making soup.
The above is the background, ha, and then let's realize the soup process. Wax gourd spareribs soup is to first add ribs, boil for 30 minutes, add wax gourd, boil for 18 minutes, add salt and coriander; chestnut spareribs soup is to add ribs and chestnuts, cook for 40 minutes, then add salt. This soup needs meat, vegetables, boil and ingredients. We use the following code to implement the process of cooking winter melon and chestnut spareribs soup.
Public class NoBuilderTest {public static void main (String [] args) {/ / make wax gourd sparerib soup DongGuaPaiGuSoup dongGuaPaiGuSoup = new DongGuaPaiGuSoup (); / / add spareribs dongGuaPaiGuSoup.addMeat (); / / boil 30 minutes dongGuaPaiGuSoup.waitMinute (30); / / add wax gourd dongGuaPaiGuSoup.addVegetables (); / / boil 10 minutes dongGuaPaiGuSoup.waitMinute (10) / / add salt and coriander dongGuaPaiGuSoup.addIngredients (); / / make chestnut spareribs soup BanLiPaiGuSoup banLiPaiGuSoup = new BanLiPaiGuSoup (); / / add chop ribs banLiPaiGuSoup.addMeat (); / / add chestnut banLiPaiGuSoup.addVegetables (); / / boil for 40 minutes banLiPaiGuSoup.waitMinute (40); / / add salt banLiPaiGuSoup.addIngredients () }} / * soup making interface * / interface Soup {/ * * add meat * / void addMeat (); / * add vegetables * / void addVegetables (); / * * boil * / void waitMinute (int minute); / * add ingredients * / void addIngredients () } / * White Gourd spareribs Soup * / class DongGuaPaiGuSoup implements Soup {@ Override public void addMeat () {System.out.println ("add spareribs");} @ Override public void addVegetables () {System.out.println ("add wax gourd");} @ Override public void waitMinute (int minute) {System.out.println ("boil" + minute + "minutes") } @ Override public void addIngredients () {System.out.println ("add salt and coriander");}} / * * Chestnut chop soup * / class BanLiPaiGuSoup implements Soup {@ Override public void addMeat () {System.out.println ("add ribs");} @ Override public void addVegetables () {System.out.println ("add chestnuts") } @ Override public void waitMinute (int minute) {System.out.println ("boil" + minute + "minutes");} @ Override public void addIngredients () {System.out.println ("add salt");}}
The above code is simple to achieve the pot wax gourd spareribs soup and chestnut spareribs soup. The point we should pay attention to in making soup is: in the order of each operation, whether the meat is boiled first and then the dish is added, or whether the meat and vegetables are boiled together in the pot. In the above code, who controls this process? It is the person who makes the soup, so the order is determined by the person who makes the soup, and may even forget to put the ingredients or something, so the soup doesn't taste good enough. So how to solve these problems?
We can solve the above two problems through the builder model: the order of making soup and the behavior of forgetting to add ingredients. We separate this soup order from the soup maker, so that the soup maker only needs to decide what soup to cook, and let the builder ensure the soup order and prevent missing ingredients.
We use a SoupBuilder to standardize the soup-making process, and the method buildSoup provides the implementer with a place to set the soup-making order. Because the cooking process of wax gourd and chestnut ribs soup is different, DongGuaPaiGuSoupBuilder and BanLiPaiGuSoupBuilder are respectively used to realize the cooking process of wax gourd ribs soup and chestnut ribs soup, that is, to eliminate the dependence between the boiling process and the soup maker. Director, on the other hand, is the equivalent of a menu that provides soup makers to choose what soup to cook. The specific code is shown below.
Public class BuilderTest {public static void main (String [] args) {Director director = new Director (); / make wax gourd chop soup director.buildDongGuaPaiGuSoup (); / / make chestnut chop soup director.buildBanLiPaiGuSoup ();}} / * make soup construction interface * / interface SoupBuilder {void buildSoup (); Soup getSoup () } / * founder of wax gourd spareribs soup * / class DongGuaPaiGuSoupBuilder implements SoupBuilder {private DongGuaPaiGuSoup dongGuaPaiGuSoup = new DongGuaPaiGuSoup (); @ Override public void buildSoup () {/ / add spareribs dongGuaPaiGuSoup.addMeat (); / / boil for 30 minutes dongGuaPaiGuSoup.waitMinute (30); / / add wax gourd dongGuaPaiGuSoup.addVegetables (); / / boil for 10 minutes dongGuaPaiGuSoup.waitMinute (10) / / add salt and coriander dongGuaPaiGuSoup.addIngredients ();} @ Override public Soup getSoup () {return dongGuaPaiGuSoup;}} / * Chestnut chop soup builder * / class BanLiPaiGuSoupBuilder implements SoupBuilder {BanLiPaiGuSoup banLiPaiGuSoup = new BanLiPaiGuSoup (); @ Override public void buildSoup () {/ / add chop banLiPaiGuSoup.addMeat (); / / add chestnut banLiPaiGuSoup.addVegetables () / / make banLiPaiGuSoup.waitMinute (40) for 40 minutes; / / add salt banLiPaiGuSoup.addIngredients ();} @ Override public Soup getSoup () {return banLiPaiGuSoup;}} / * producer * / class Director {private DongGuaPaiGuSoupBuilder dongGuaPaiGuSoupBuilder = new DongGuaPaiGuSoupBuilder (); private BanLiPaiGuSoupBuilder banLiPaiGuSoupBuilder = new BanLiPaiGuSoupBuilder () / public DongGuaPaiGuSoup buildDongGuaPaiGuSoup () {dongGuaPaiGuSoupBuilder.buildSoup (); return (DongGuaPaiGuSoup) dongGuaPaiGuSoupBuilder.getSoup ();} / * Chestnut sparerib soup * / public BanLiPaiGuSoup buildBanLiPaiGuSoup () {banLiPaiGuSoupBuilder.buildSoup (); return (BanLiPaiGuSoup) banLiPaiGuSoupBuilder.getSoup ();}}
By using the builder, does it ensure the order in which the soup is cooked and must be filled with enough ingredients? Feel the mystery.
Code:
Builder Pattern
Summary
Through the builder model, we can unbind the things that we are strongly dependent on, not only solve the problem of dependence, but also improve the encapsulation, so that users do not have to understand the internal details, and use the above example to say that we do not have to care about the process of making soup. just like we want to drink wax gourd spareribs soup and tell our mother that we don't know how to cook it.
Recommended reading
Single responsibility principle (method: change the name or password? Interface: washing dishes, buying vegetables or taking out the trash? Classes: register, log in, and log out)
Richter substitution principle (my son is from New Oriental Cooking)
Rely on the principle of inversion (a stingy restaurant owner)
Interface isolation principle (young man's workshop)
Dimitt's rule (reading e-books on mobile phones)
The principle of opening and closing (social security)
Creative mode: singleton mode (Xiaoming has only one car)
Creative model: factory method (Xiaoming's garage)
Creative model: abstract factory (BMW cars have to use BMW tires and BMW steering wheels)
The design pattern article will be updated continuously. You are welcome to follow the official account and get the article push as soon as possible.
The official account replied "Gift package" to get tutorials such as Java, Python, IOS, etc.
Add personal Wechat comments "tutorials" to get tutorials for architects, machine learning, etc.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.