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
classmainclass{ Public Static voidMain (string[] args)throwsclonenotsupportedexception {Person person =NewPerson (); Person.setnanme ("Jeff Lee"); Person.setage (20); Person.setsex ("Boy");//Person person2 = person; reference same change same time changePerson Person2 = Person.clone ();//refer to different content and also change theirSystem.out.println (Person2.getnanme ()); Person2.setnanme ("Jeff Lee 2"); System.out.println (Person2.getnanme ()); System.out.pri
//user.name = "Cloner name"; A returnuser; - } -}Cons: 1. The cloning method requires an overall consideration of the function of the class, which is not difficult for a new class, but not necessarily easy for existing classes, especially when a class reference does not support serialization of indirect objects, or when a reference contains a looping structure. 2, must implement the Cloneable interface. 3, Escape from the constraints of the constructor function.usage Scenari
The prototype pattern belongs to the creation pattern of the object, by giving a prototype object to indicate the type of object to create, and then creating more objects of the same type with the method of copying the prototype object. is to give you an existing object that
Prototype prototype mode is a creation design pattern, prototype mode allows an object to create another customizable object without knowing the details of how to create it, by passing a prototype object to the object to be create
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
= (enterpriseorder) Order; e2.setenterprisename (e1.getenterprisename (); e2.setproductid (e1.getproductid (); e2.setorderproductnum (1000 ); // then set it to neworderneworder = e2;} // 2.2 the original order is retained and the quantity is reduced by 1000order. setorderproductnum (Order. getorderproductnum ()-1000); // then, the service function is processed. If it is omitted, print the output and check the system. out. println ("split to generate order =" + neworder );}4. Solution
Do you thi
objectWhen an object requirement is provided to other object access, and each caller may need to modify its value, consider using the prototype schema to copy multiple object callers.In real-world projects, prototype patterns rarely appear alone, typically in conjunction with Factory mode patterns, creating an object by using clone, and then appearing together with the factory method
Prototype mode (PROTOTYPE): Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes. The prototype pattern is actually creating another customizable object from one object without needing to know the specifics of t
There are 23 modes in the design pattern. This is just for one purpose: decoupling + decoupling + decoupling ... ( cohesion poly-low coupling satisfies the opening and closing principle)Introduced:Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.Why use proto
Definition: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.
The prototype pattern is actually creating another customizable object from one object without needing to know the details of any creation.
Prototype mode mainly considers deep copy and sh
initialize information without changing it, cloning both hides the details of the object creation, and there is a significant improvement in performance.Instead of re-initializing the object, you dynamically get the state of the object runtime.Prototype mode using cloning will certainly involve deep replication and shallow copy, Java implementation of the direct substitution clone method, but that is shallow copy, the true use of deep replication also need to implement the serializable interfac
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
I do not know what the reason is always the prototype model and template method of mixing, really very distressed ah. Do not know whether we have the same trouble, some words of treatment, no words of prevention AH. Therefore this article carries on the detailed comparison study. prototype Mode
The so-called prototype pattern
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.