Objective
In the previous article we learned about the factory model, introducing simple factory models, factory methods, and abstract factory models. This article introduces the builder pattern and prototype pattern in the design pattern that belong to the creation pattern.Introduction to Builders ' models
Builder mode is the creation mode. The builder mod
The 11th chapter of the design pattern-builder mode (Java implementation) "I was drunk in that restaurant 、、、" "What's the matter?" "The Order of serving, the first dessert, then a cold drink, then the dish or something, no words dead." "That's a bit of a order, too." However, it is very rare, normal if it is Chinese food, are cold dishes before the hot dish, then the soup, and finally some dessert or some
assembly process, which contains the classes that define the constituent parts, including the interfaces that assemble the parts into the final product.Role builder: [Java] View plaincopyprint? On code to see a snippet derived from my Code slice public interface Personbuilder {void Buildhead (); void Buildbody (); void Buildfoot (); Person Buildperson (); }Role ConcreteBuilder: [
Item2:consider a builder when faced with many constructor parametersWhen a constructor method has more than one parameter, you can consider using the builder approach for processing.Instance code: Public classnutritionfacts {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate; Public Static classbui
()
{
pb.buildhead ();
Pb.buildbody ();
Pb.buildarmleft ();
Pb.buildarmright ();
Pb.buildlegleft ();
Pb.buildlegright ();
}
Test Case:
Package com.shejimoshi.create.Builder;
/**
* Function: Client program
*
/public class Test
{
//create the corresponding character public
static void Create (Personbuilder pb)
{
////The model with parameters is used to create the corresponding object
persondirector pd = new Persondirector (PB);
Pd.cr
Flash Builder 4 Memory optimized Java heap space solution
Flash build 4 Compile slow, report Java heap Space heap spatial exception error resolution:1,Locate the Flexbuilder.ini file under the installation path, edit it, and add the following parameters
-vmargs-xms256m-xmx1024m-xx:maxpermsize=256m-xx:permsize=64m #不要设置太高, otherwise it will be slower.-djava.net.
figure3. Code public interface Builder { void Buildparta (); void Buildpartb (); void Buildpartc (); Product getresult (); } //Concrete Build Tool public class ConcreteBuilder implements Builder { Part parta, PartB, PARTC; public void Buildparta () {//Here is the code that specifically constructs Parta }; public void Buildpartb () {//Here is the code specifically for how to build PARTB }; public
mode, one part is the component construction and the product assembly, the other part is the overall built algorithm . It is important to recognize this because in the construction model, the emphasis is on fixing the overall built algorithm, while flexibly extending and switching the specific construction of the part and the way the product is assembled.To put it bluntly, the focus of the construction model is to separate the construction algorithm and the concrete construction implementation,
my custom addtocollection rule. It is also very simple to execute this rule, get the current object (the top element of the stack), and then remove the second element from the top of the stack, which is linkedlist. You can then add it to the collection.4) EndElement, object out of the stack, only the LinkedList object is left in the stack.Finally, it encounters the end of Xxobejcts, which is the bottom of the stack at the end of the resolution. What is returned is the bottom object. So the resu
. Do not know how to handle the reasonable?
In addition, after writing a few articles about implementing design Patterns in Java code, it is found that this builder builder pattern seems to combine abstract factory patterns with template method patterns. The above paragraph has said my doubts, as for the abstract factory model, I personally think the above code
; System.out.println ("roundgroph.grougph (). radius=" + radius);} void Draw () {System.out.println ("Roundgroph.draw (). radius=" + radius);} public static void Main (String args[]) {Roundgroph a = new Roundgroph (22);}}This occurs because of dynamic binding. The reason why Java is polymorphic is due to The above code is an explanation of the phrase "thinking in Java"
If the constructor is simply
("2");Wuyi } the - @Override Wu PublicProduct getproduct () - { About returnproduct; $ } - } - - Public classDirector A { + PrivateFactory Factory =Newfactory1a2b (); the - PublicProduct newproduct () $ { the factory.init (); the Factory.buildparta (); the FACTORY.BUILDPARTB (); the returnfactory.getproduct (); - } in } the the Public classTest About { the Public Static voidMain (string[] args) the { theDirector Director =NewD
constructor of the parent class is called according to the list of arguments passed in the super call.2. The first line of code for the subclass constructor execution uses the this display to invoke the overloaded constructor in this class, and the system will determine another constructor that calls this class based on the list of arguments that are passed in this call (the first case when executing another constructor in this class;3. When there is no super () or this () call in the subclass
(); }
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
Advantage 4: Static factory methods can make code more concise when creating instances of parameterized types.
Private map
③. What are the disadvantages of creating an object using a static factory method?
Disadvantage 1: If a class can only get an instance through a static factory method, then the constructor of the class cannot be shar
system variable is configured, it will command to open the browser automatically, the open browser will appear in the database test connection, it provides a different language and database for users to choose to use. It is important to note that the DOS window and browser opened by the command are not closed until the end of the process. For reference, see figure:5, for the above configuration, it is necessary to connect with the database, then you must create a new data table structure in the
getinstance to return a reference to the same object, never create another evlis instance;Why do I need a private constructor if the class cannot be instantiated how to use this class method?The existence of a private constructor can make certain classes impossible to instantiate and subclass , and these classes are typically tools classes, such as Java.lang.Math, to access the methods of these classes we can define a public static method to implement, such as A.methon ()public Class A { pri
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.