24 kinds of design patterns learning model prototype of notes

----- create a prototype model schema

    Specified kind prototype objects created instance, and create new objects by copying these prototypes (clone)

 

There are two general usage scenarios

  1. When a class object requires a large amount of prototype is the best choice, as a prototype model of the object is copied in memory, the object is much better performance than the new new direct, in which case, more objects, the more obvious advantages of the prototype pattern reflected.

  2. If the initialization of an object requires a lot of other data objects preparation or complicated computing other resources, you can use the prototype model.

  3. When needed when a large amount of public information object, a small field of personalized settings, there may be used the prototype copy of the object copy mode for processing occurs.

 

 

 

(1) client (Client) Role: client request to create an object class; that is, we use a user copy and paste functions.

Implemented method required for this role defines a specific prototype classes: (2) abstract prototype (the Prototype) role. That is the definition of a document describing what it has been cloned copy function.

(3) specific prototype (Concrete Prototype) roles: the role of a prototype implementation of the abstract interfaces clones. We realized that the file can be copied function.

We will find out that the core of the prototype model is the Prototype (abstract prototype), he needs to inherit the Cloneable interface and override the clone method of the Object class in order to have the copy and paste functions.

3, classification

Since we know that the core is the prototype copy of the object model, so we can copy what an object instance of it? This division is necessary to distinguish the deep copy and a shallow copy.

(1) shallow copy: we only copy the basic data types (8 types) object for the array, container, and so will not copy the object reference

(2) a deep copy: copy not only the basic data types, but also copies of those arrays, containers, and other reference object,

 

For the prototype model has several issues that we need to pay attention to

1, does not call the constructor clone

In fact, we can also be found from the above output, constructor only in the beginning we create prototypes when output, fileB and fileC did not call the constructor, because the time to perform the clone method is to obtain data directly from memory , the first time to create an object will keep a copy of the data in memory, when cloned directly call like

2, access to the prototype model is invalid

Principle is very simple, we are copied directly from memory, so it will be cloned directly ignored, copy the contents just fine.

Guess you like

Origin www.cnblogs.com/xiaoyangxiaoen/p/12377131.html