In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "case study of Java builder model". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "case study of Java builder model".
What is the builder model?
The definition of Builder pattern: a design pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations. This design pattern is called builder pattern. It decomposes a complex object into several simple objects, and then builds them step by step. It will separate the change from the unchanged phase, that is, the components of the product are unchanged, but each part can be flexibly selected.
Advantages
1. Good encapsulation, separation of construction and presentation.
2. The expansibility is good, and the specific builders are independent of each other, which is conducive to the decoupling of the system.
3. The client does not need to know the details of the internal composition of the product, and the builder can gradually refine the creation process without having any impact on other modules, so it is easy to control the risk of details.
Shortcoming
1. The components of the product must be the same, which limits its scope of use.
2. If the internal change of the product is complex, if the internal change of the product occurs, the builder should also modify it synchronously, and the maintenance cost is high in the later stage.
Knowledge point
The focus of the Builder model is different from that of the factory model: the builder model focuses on the assembly process of parts, while the factory method model pays more attention to the creation process of parts, but the two can be used together.
Builder pattern implementation
Case: magic doctor Doudou alchemy
Product role (Product): Dan medicine
Abstract builder (Builder): abstract Dan
Specific builder (Concrete Builder): concrete Dan
Director: magic doctor Doudou
The miraculous doctor Doudou refines the nine-turn fairy elixir and Taixu Shendan.
Note: in order to save trouble, the content of Dan here is only the name and effect of Dan medicine, and there are no materials and so on.
Dan medicine
The elixir class declares the name and effect of alchemy.
Public class Dan {/ * name of cailiao danhuo effect of Dan * / private String name; private String xiaoguo; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getXiaoguo () {return xiaoguo;} public void setXiaoguo (String xiaoguo) {this.xiaoguo = xiaoguo;}} Abstract Dan
Abstract class, new an elixir object, declares two abstract methods to name and give the elixir effect, and a method to generate the elixir.
/ / Abstract Dan abstract class DanFang {Dan dan = new Dan (); public abstract void name (); public abstract void xiaoguo (); public Dan getDan () {return dan;}} Nine Zhuan Xiandan Fang
The concrete Dan class inherits the Dan class and implements two abstract methods.
/ / public class JiuZhuan extends DanFang {@ Override public void name () {dan.setName ("Jiuzhuanxian elixir");} @ Override public void xiaoguo () {dan.setXiaoguo ("become immortal");}} Taixu Shendandan Fang
The concrete Dan class inherits the Dan class and implements two abstract methods.
/ / public class Taixu extends DanFang {@ Override public void name () {dan.setName ("Taixu Shendan");} @ Override public void xiaoguo () {dan.setXiaoguo ("mind Shendan");}} Divine Medicine Doudou
Declare a Dan attribute, a parametric constructor, and an alchemy method.
Public class DouDou {private final DanFang danLu; public DouDou (DanFang danLu) {this.danLu = danLu;} / / Alchemy method public Dan lian () {/ / put into the material danLu.name (); / / release Danhuo danLu.xiaoguo (); / / Cheng Dan return danLu.getDan ();}} test
Refine two elixir.
Public class Demo {public static void main (String [] args) {/ / refine a nine-turn elixir / / new a nine-turn elixir DanFang jiu = new JiuZhuan (); / give the Dan to Doudou DouDou dou = new DouDou (jiu); / / Alchemy produces an elixir Dan dan = dou.lian () System.out.printf ("Dan name:% s Dan effect:% s", dan.getName (), dan.getXiaoguo ()); System.out.println (); / refine Taixu Hua Shen Dan / / new a Dan prescription DanFang tai = new Taixu (); / / give Dan Fang to Doudou DouDou dous = new DouDou (tai) / / Alchemy Dan dans = dous.lian (); System.out.printf ("Dan name:% s Dan effect:% s", dans.getName (), dans.getXiaoguo ());}}
Thank you for your reading, the above is the content of "case study of Java builder model". After the study of this article, I believe you have a deeper understanding of the case study of Java builder model, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.