In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to implement the Prototype prototype pattern". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Prototype (prototype mode)
Prototype (prototype pattern) is a creative pattern, which is neither a factory nor a direct New, but creates objects in a copied way.
Intention: specify the type of object to be created with prototype instances, and create new objects by copying these prototypes.
As an example
If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.
Make a key
Obviously, for the sake of house safety, one key can only open one door as far as possible. the structure of each key is more or less different, but it is very similar. the person who made the key made a new one exactly like the key you given. what kind of model does this belong to?
Two state tables
When the website does non-stop maintenance, assuming that the maintenance content is to give each senior member account an extra 100 yuan in cash, now you need to change the database table. Known:
There are tens of millions of data in the database table, including thousands of senior members, which have been cached in the middle tier for convenience, and the corresponding cache of the database will be updated after the corresponding ID update.
It takes several minutes for thousands of data modification statements to be executed, and the problem of out-of-sync of user data cannot be accepted within a few minutes.
A common practice is that we generate a copy of the senior member list instead of the result of the database cache. As soon as the database reads the corresponding member ID, the database obtains it from the copy list, adds a column of status flag to the data table, and removes the copy after operation to update the advanced member cache.
But how to generate a copy of the advanced membership list? If you directly re-query from tens of millions of user data, there will be a higher database query cost.
Template component
In the general building system, we can set a block dragged to the page as a "template", and the template can be re-dragged as a new component and instantiated any number of times. In fact, this is a segmented copy and paste, how do you achieve this function?
Intention interpretation
The solution to the above problems is simple, which is to copy based on existing objects, which is more efficient than New or factory mode.
Intention: specify the type of object to be created with prototype instances, and create new objects by copying these prototypes.
The so-called prototype instance is the object selected to copy the template, such as the template key you gave to the boss in the example of making the key, the list of existing cached advanced members in the two state tables, and the selected component in the template component. Then, you can create the object you want by copying these prototypes.
Let's think abstractly that if each key follows the Prototype interface and provides a clone () method to copy itself, we can quickly copy any key. The key factory can not solve the problem that each key is different. What we want is exactly the same copy as a certain key. It is easiest to make a copy of the key.
In the example of the advanced membership status table, querying the database is expensive, but if you only copy the list that has been queried, the time is negligible, so the most economical solution is to copy it directly. instead of reconnecting to the database and executing the query through the factory mode.
Template components are even more so, we simply do not define so many component instances of the base class, as long as each component provides a clone () function, you can immediately copy any component instance, which is undoubtedly the most economical solution.
Seeing here, you should know that the essence of the prototype pattern is that the object provides the clone () method, and this clone () method is difficult to implement.
Generally speaking, it is recommended to use a deep copy for the copy of the prototype mode. After all, it is best not to affect the old object with the new object, but when the performance problem of the deep copy is large, you can consider combining the deep copy with the shallow copy, that is, in the new object, the data that will not be modified is copied using a shallow copy, while the data that may be modified uses a deep copy.
Structure diagram
Client is the client that issues instructions, Prototype is an interface that describes how an object clones itself, for example, it must have a clone () method, and ConcretePrototype is a specific implementation of cloning, and different objects have different implementations to copy themselves.
Code example
The following example is written in typescript.
Class Component implements Prototype {
/ * *
* component name
, /
Private name: string
/ * *
* component version
, /
Private version: string
/ * *
* copy yourself
, /
Public clone = () = > {
/ / the constructor is omitted, which is probably passing name and version
Return new Component (this.name, this.version)
}
}
We can see that Component that implements the Prototype interface must implement the clone method, so that any component can directly call the clone function when performing a copy, regardless of how each component is implemented.
From this, we can see that the prototype pattern is similar to the Factory and Builder patterns in hiding the details of object creation.
When we use it, we can create a new object like this:
Const newComponent = oldComponent.clone ()
There are two caveats here: in general, if you want to modify the generated object twice, it is not recommended to add parameters to the clone function, as this will lead to interface inconsistencies. We can provide some set functions for the object instance for secondary modification. In addition, the clone function should consider performance, as mentioned earlier, you can consider the combination of deep and shallow copies, and note that the copy function may not even be implemented when the object has a reference relationship or even a circular reference.
Malpractice
Every design pattern must have its drawbacks, but as said in every issue, it does not mean that the design pattern is not easy to use, but means that there are problems in certain scenes. We just need to avoid these scenarios and use corresponding design patterns in reasonable scenes.
The disadvantages of the prototype model:
Every class has to implement the clone method, and there is a certain intrusion into the implementation of the class. when you want to modify the existing class, it violates the opening and closing principle.
When the class calls other objects, if you want to achieve deep copy, the corresponding object also needs to implement the clone method, and the overall link may be very long, which is troublesome to implement.
This is the end of "how to implement the Prototype prototype pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.