Alibabacloud.com offers a wide variety of articles about prototype design pattern real world example, easily find your prototype design pattern real world example information here online.
voidMain (string[] args) throws Ioexception,classnotfoundexception {Prototype Prototype = new Prototype (20); Prototype Cloneproto = (Prototype) prototype.deepclone ();/** * can be seen by printing: Prototype and Cloneproto Thes
newly created object when memory has been allocated to an object. Although JavaScript does not support the concept of classes, it does support special constructor functions that are used with objects, such as:
code is as follows
copy code
function WebSite (name,url) { this.url=url; this.name=name; this.getinfo=function () { return this.url+ "-" +this.name; } } var ws= New WebSite ("Open source Window", "oseye.net"); Console.log (Ws.getinfo ())
.
Example : The same above resume problem, most people can not be a fixed job in a lifetime, so in the process of writing a resume is doomed to have a column is the work experience. But each job experience is probably the same, nothing more than the entry year, the term of work content, separation time. But the specific content is not the same, with the constant job search, this change, not every job to rewrite a
prototype class or interface, which has only one clone (), and all the concrete prototype classes implement the method and define the specific behavior of replicating itself. A clone method that invokes a specific prototype object in the client can replicate the prototype object into a new object.
Figure 1
time
· Changing objects, this is the common advantage of combinatorial patterns
· Reduce the number of subclasses
· Dynamically configuring Applications with classes
The biggest disadvantage of prototype is that it can be difficult to implement a clone operation in some languages, especially if the amount of circular references is included.
Next, continue to use the example in the previous article, de
This time for you to bring PHP prototype design patterns use case analysis, PHP prototype design patterns used in the attention of what, the following is the actual case, together to see.
First, what is the prototype design mode
Prototype modePrototype mode (Prototype pattern) is used to create duplicate objects while guaranteeing performance. This pattern implements a prototype interface that is used to create clones of the current object. This pattern i
Prototype of Design Pattern
The object created by the factory method is a new object, and its attributes are generally default.
What I understand is that the prototype operation method creates a clone body, which can be considered as two steps: 1. creates an object. 2. copy the attributes of the created object as the c
Implementation of design prototype design pattern
Implementing the Prototype design pattern
Download this article code
When I create an instance of a class that is complicated, we
The advantages and application scenarios of prototype modeCreating an object using the prototype schema is much better performance than a direct new object, because the Clone method of the object class is a local method that directly operates in-memory binary streams, especially when copying large objects, where performance is significantly different.Another benefit of using
words, a deep copy copies the objects that are referenced by the object being copied again. 1. Shallow copy (Shallow clone) UML:This example is when we are in charge of the mail. Do not copy attachments (Attachment)Again, all Java classes inherit the Java.lang.Object, and do not write extends Object in the code.Super.clone () is required to invoke the Clone () method;2. deep Copy (Deep clone) UML:"Shallow Clone" Public Object Clone () { null;
Prototype mode is also a created design pattern, creating new objects by copying prototypes, understanding that prototype patterns must understand shallow and deep replication in Java. Replication is also known as cloning. deep copies will occur with 8 of the basic types in Java and their package types, plus the string
backgroundSometimes you will find that the reference type is also very annoying, because at some point you may need two of the same reference object, one for backup, one to continue operation, but continue operation that does not affect my backup that, this time the trouble comes, two references if point to the same memory space object, Then modify one, and the other will certainly change.1. Intention to useInstead of re-initializing the object, you dynamically get the state of the object runtim
instance to specify the type of object to be created, that is, our daily life. If we want to generate a daily life pattern for many days, you only need to assign values to the objects created by the prototype, instead of having to create an instance object every time. This will greatly save the various resources used to create objects. Next, let's take a look at how to implement it.
Using system; using s
automatically generates a constructor called the default copy constructor. By default, the copy constructor uses the shortest COPY method. If the class contains dynamic data members, you must use the deep COPY method to implement the copy constructor. Otherwise, when the object is destroyed, the destructor of the two objects will release the same memory twice, resulting in a runtime error.
// Prototype. h # include View code
References
1
: This article mainly introduces the php design pattern learning series (7)-prototype objects. if you are interested in PHP tutorials, refer to it. Disclaimer: Cheng Jie, author of this series of blog references "big talk Design Mode.
Use a prototype instance to specify the
OverviewThe prototype pattern is designed to solve some unnecessary object creation processes. When the Cloneable interface is available in the Java JDK, the prototype schema becomes unusually simple. Although the introduction of cloneable has become simpler, there are some things that need to be noted and noticed. The text will explain these precautions in detai
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.