Design pattern series, prototype pattern

In the interview, design patterns, source code, data structure, framework principles, JVM, GC, middleware, etc. are also indispensable. All these things need to be accumulated in peacetime. I will do a topic later here, and make the design pattern into a menu separately for the convenience of all netizens who will be interviewing later.

Design pattern series, prototype pattern

There are many ways to create objects in Java, such as: through new, reflection, Object.clone, deserialization, Unsafe.allocateInstance, etc. Their uses are different. Today I use Object.clone to talk about a prototype design pattern.

The prototype pattern (Prototype Pattern) is used to create repetitive objects while ensuring performance. This type of design pattern is a creational pattern, which provides the best way to create objects.

This mode is used when the cost of directly creating an object is relatively high. For example, an object needs to be created after a costly database operation. We can cache the object, return its clone on the next request, and update the database when needed to reduce database calls.

In real life, prototype mode is also inseparable everywhere. For example, cell division and so on.

The prototype mode is very simple. Simple enough to use the following 3 classes to get its class diagram.

Design pattern series, prototype pattern
Our common game is to fight planes, or Plants vs. Zombies. All can use the prototype design mode.

Design pattern series, prototype pattern

The coordinates of each aircraft or shell may be different, but there are a lot of aircraft to be produced. If you create one by one, the cost is too high. Therefore, we can use the prototype mode to solve it.

Design pattern series, prototype pattern

The prototype mode is very simple, and some netizens may find it difficult, because you have not figured out its usage scenarios. Below I summarize the common scenarios of prototype mode.

  • Resource optimization scenarios.

  • Class initialization needs to digest a lot of resources, this resource includes data, hardware resources, etc.

  • Scenarios with performance and safety requirements.

  • Generating an object through new requires very tedious data preparation or access permissions, so you can use the prototype mode.

  • A scene with multiple modifiers for an object.

  • When an object needs to be provided to other objects for access, and each caller may need to modify its value, consider using the prototype mode to copy multiple objects for use by the caller.

  • In actual projects, the prototype mode rarely appears alone, but generally appears together with the factory method mode. An object is created through the clone method, and then the factory method is provided to the caller.

Through these several scenarios, we can also see that the prototype mode: can improve performance; at the same time, it can also escape the constraints of the constructor. But in addition to these two advantages, the disadvantages are also obvious. The Cloneable interface must be implemented; and especially when a class reference does not support serialization of indirect objects, or the reference contains a cyclic structure, you have to consider the depth of cloning.

Many people have read design patterns many times, but they still won't be able to get to the interview. The reason is that the scene used is unfamiliar and has not been used in the official code.

Guess you like

Origin blog.51cto.com/15127565/2664950