Preface
Prototype: Use a prototype instance to specify the object type and copy the prototype to create a new object.
Structure chart
Prototype, prototype class, declares an interface for cloning itself
Concreteprototype1 and concreteprototype2 are specific
would remain references.void __clone (void)Once The cloning is complete, if a __clone () method was defined, then the newly created object ' s __clone () method would be C Alled, to-allow any necessary properties, which need to be changed.Example #1 cloning an objectPHPclasssubobject{Static $instances= 0; Public $instance; Public function__construct () {$this->instance = ++self::$instances; } Public function__clone () {$this->instance = ++self::$instances; }}classmycloneable{ Public $obje
This article is based on Zen of design patterns and Java and patterns.
1. specify the type of the object to be created by providing a prototype object.
Ii. Semi-support in Java
1. The ojbect class provides a clone method.
2. Java must implement cloneable to explicitly demonstrate that JavaBean supports replication.
3. If you use the cloneable interface directly, java. Lang. clonenotsupportedexception w
Here is an example of copying a resume.
Package COM. WZS. design;/*** big talk design mode -- page88 prototype mode ** @ author administrator **/public class prototypepattern {public static void main (string [] ARGs) throws clonenotsupportedexception {workexperience = new workexperience ("2011-2-2", "2013-3-3", ""); resume a = New resume ("", "male ", "23", work
What is a prototype pattern?
In Gof's design pattern: The basics of reusable object-oriented software: Use a prototype instance to specify the type of object to create and create a new object by copying the prototypes. In this definition, the most important word is "copy",
() { alert (this.name); }}; var person1 = new Person (), var person2 = new Person (); Person1.friends.push ("Van"); alert (person1.friends);//"Shelby,court,van" alert (person2.friends);//"Shelby,court,van" alert (person1.friends=== Person2.friends); TrueHere, the Person.prototype object has a property named friends that contains an array of strings. Then, two instances of person are created. Next, an array of person1.friends references is modified, and a string is added t
to chat with GG on the Internet. GG is dedicated to copying the prepared words and sending them to mm after slight modification. mm is almost beautiful...
Prototype mode explanation:
Prototype pattern is an object creation mode. It creates an object instance by copying a prototype object. Instances created in
The prototype mode specifies the type that needs to be created through the instance object, which is essentially different from the factory method pattern, where the factory method pattern is defined by the inheritance of the class to create different types of objects, belonging to the class pattern, The
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.
Definition:
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.The prototype mode is a creation design mode. The prototype mode allows an object to create another custom object. You do not need to know how
JavaScript Object-orientedJavaScript is a language of no class, but can be modeled using functions, which involves design patterns. Patterns are proven reusable solutions that can be used to address common problems encountered in software design and are often used to create templates for reuse.
The common way for JavaScript impersonation classes is to define a JavaScript function, use this to define proper
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
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
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
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
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
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
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.