Prototype Mode of Design Mode 7

If you encounter the need to create a large number of objects during development, you can use traditional constructors to create objects. But this is too troublesome for development. Is there an efficient way to generate objects?

Of course there is, the prototype mode can solve the above problems.

What is prototype mode

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. (Specify the kinds of objects to create using a prototype instance, and create new objects by copying these prototypes.)

If an object has been created, we can create a new object by copying. This is the prototype mode. Using the prototype mode to create objects, we don't need to know the creation details. In fact, this method is very efficient.

Let's give a chestnut: Everyone has installed JDK on Windows, we need to click many next steps when installing. You may also need to select the installation path and select the installed module. If we install JDK on Linux, we only need to copy the JDK installation file to Linux, unzip it and use it. You see, installing files on Windows is much more complicated than installing on Linux.

The key to the prototype mode is replication. The purpose of replication is to simplify the object creation process and improve efficiency.

The structure of the prototype pattern

It mainly contains several elements:

  • Abstract prototype class: define the interface that needs to be implemented

  • Concrete prototype class: clone()methods to implement abstract prototype class

  • Access class: clone()object copied by calling method

Prototype mode

Note that the clone()method here is shallow copy.

The results are as follows:

I checked Wikipedia about shallow copy and deep copy. If you are interested, you can go to the original text:

https://en.wikipedia.org/wiki/Object_copying

Here I translated:

Shallow copy

One way to copy objects is shallow copy. In this case, create a new object B and copy the field value of A to B. This is also called field-by-field copy, field-for-field copy, or field copy. If the field value is a reference to an object ( Such as memory address), it will copy the reference, so that it references the same object as A. If the field value is a primitive type, it will copy the value of the primitive type. In a language without primitive types (in this language, everything is an object), all the fields of the copied B refer to the same object as the original A field, so the referenced object is shared, so if One of the objects is modified (from A or B), then the change can be seen in the other object. Shallow copies are simple and usually cheap, because they can usually be achieved by simply copying bits accurately.

Let me summarize that in shallow copy, if the attribute in the original object is a basic type, the value will be copied to the new object, and if it is a reference type, the same reference will be used.

Deep copy

The other method is deep copy, which means that the field is dereferenced: instead of a reference to the object being copied, a new copy object is created for any referenced object, and references to these objects are placed in B. The result is different from that of a shallow copy, because the object referenced by copy B is different from the object referenced by A and is independent. Due to the need to create additional objects, the cost of deep copy is higher, and because references may form a complex graph, deep copy may be more complicated.

Deep copy refers to the process in which the copy process occurs recursively. It means first constructing a new collection object, and then recursively filling it with copies of the child objects found in the original object. In the case of deep copy, a copy of the object is copied to other objects. This means that any changes to the copy of the object will not be reflected in the original object. In python, this function is realized through the " deepcopy() " function.

In general, a deep copy creates a new reference.

Code display

RealizeType

PrototypeTest

Test Results:

具体原型创建成功!
具体原型复制成功!
obj1==obj2?false

The realization of the prototype mode is actually very simple, as long as the clone()method is called, the property copy can be realized.

Thinking about the prototype model

Using the prototype mode has excellent performance. In the prototype mode, objects are not directly new. By copying existing objects in memory, if you need to generate a large number of objects in the loop, you can use the prototype mode.

In fact, prototype mode is used in many places.

In Spring, the prototype pattern is widely used, for example  scope='prototype', JSON.parseObject() etc. are specific applications of the prototype pattern.

I believe you must have used it BeanUtils.copyPropertiesas a physical copy, in fact, this copy is also a shallow copy.

What you need to pay attention to about shallow copy

We know that shallow copies will share the same address. If the value of an object's attribute (reference type) changes, the value of another object will also change accordingly. The two objects share a private variable. You can change it and everyone can change it. This is a very insecure way and is rarely used in actual projects. (Of course, this is also a kind of "crisis" environment. Life-saving)

We use code to illustrate the problem:

Shallow copy problem

We create a class to copyCitation

Test code

Let's take a look at the results directly:

If obj2the Membercontent of in changes, obj1will the Membercontent of in change?

The answer to this question will change .

So clone()pay attention to this problem when using copy objects.

 

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109043823