In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what problems to consider when implementing prototype mode". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
achieve
Prototype is especially useful because in static languages like C++, classes are not objects and little or no type information is available at runtime. Prototype is less important in languages such as Smalltalk or Objective C, because these languages provide an equivalent to a prototype (i.e., a class object) to create instances of each class. The Prototype pattern is inherent in prototype-based languages like Self [US87], where all objects are created by cloning a prototype.
Simply put, there is no prototype mechanism provided in C++, so you need to build your own prototype pattern; conversely, when the equivalent mechanism is already provided in the language, it is good to use it directly.
When implementing a prototype, consider the following questions:
1 ) Use a prototype manager
When the number of prototypes in a system is variable (that is, they can be created and destroyed dynamically), maintain a registry of available prototypes. Customers do not manage prototypes themselves, but store and retrieve prototypes in the registry. Customers request a prototype from the registry before cloning it. We call this registry the prototype manager.
In cases where prototypes can be created dynamically, you need a place to store them in case they are needed. This management mechanism is the prototype manager. It provides at least two functions to the outside world, login and retrieval. One is an input prototype and one is an output prototype. Note that the customer here refers to the module that uses the prototype, not the user of the software.
The prototype manager is an associative store that returns a prototype that matches a given keyword. It has a number of operations that can be used to register prototypes and unregister via keywords. Customers can change or even browse this registry at runtime. This allows customers to extend and get system manifests without writing code.
Association refers to the relationship between keywords and prototypes. This relationship is required for both entry and retrieval of prototypes. Often, it is also necessary to browse the mechanisms of logged prototypes so that customers can easily use them even if they have no idea how they came to be.
2 ) Implementing cloning operations
The hardest part of Prototype mode is implementing Clone correctly. This is especially tricky when the object structure contains circular references.
For example, C++, A, B two objects circular reference situation, because this is A, B two objects appear at least twice, in the Clone operation needs to use some way (hard coding or some recognition mechanism) to determine which is an entity, which is a reference.
Most languages provide some support for cloned objects. For example, Smalltalk provides an implementation of copy that is inherited by all subclasses of Object. C++ provides a copy constructor. However, these facilities do not solve the "shallow copy and deep copy" problem [GR83]. That is, does cloning an object clone its instance variables in turn, or does the cloned object share those variables with the original object?
Consider the case of cloning in C++. Suppose a class has a pointer data member pointing to another object. How does the cloning process know whether the object is part of the class (requiring cloning) or a reference to a common object (not requiring cloning)?
Shallow copy is simple and usually sufficient, and is provided by default in Smalltalk. The default copy constructor in C++ implements copy-by-member, which means that pointers are shared between copied and original objects. But cloning a complex prototype usually requires deep copying because the copied object and the original object must be independent of each other. Therefore you must ensure that the artifacts of the cloned object are also clones of the artifacts of the prototype. Cloning forces you to decide what to do if everything is shared.
Just like instantiating objects from classes, cloned objects are used independently. So deep copies must be used instead of shallow copies. When doing Clone processing, you must also consider what to do if all objects are shared (this may not be accurate, but there is really no better way to say it).
If objects in your system provide Save and Load operations, you can provide a default implementation for Clone operations simply by saving the object and loading it immediately. The Save operation saves the object in a memory buffer, and Load creates a copy by reconstructing the object from that buffer.
This eliminates the hassle of hard-coding clones directly. But additional space is needed to store the data. Serialization features provided in the language library are often used.
3)Initialize clone object
While some customers are quite satisfied with the cloned object, others will want to initialize some or all of the object's internal state with values of their choice. It is generally not possible to pass these values in Clone operations because the number of values varies depending on the class of the prototype. Some prototypes may require multiple initialization parameters, others may require none. In Clone operations
Passing parameters breaks the unity of the clone interface.
Generally speaking, you want to modify the cloned object to some extent. But because this modification varies from class to class and application scenario to application scenario, it is unlikely to be digested by Clone processing parameters. It is common practice to modify objects after they are acquired.
It may be that the prototype class has defined actions for (re) setting some key state values. If so, customers can use these operations immediately after cloning. Otherwise, you might have to introduce an Initialize operation (see Code Example section) that uses initialization parameters and sets the internal state of the clone object accordingly. Note the Deep Copy Clone operation-some copies may have to be deleted before you can reinitialize them (deletion can be done explicitly or internally).
When modifying the cloned object, if there is already an operation to modify the state (or attributes), it is good to use it directly; if there is no operation, it is necessary to introduce an initialize operation (instead of adding the setting operation). Note that when cloning is a deep copy, some members need to delete the contents of the clone before initialization. This deletion can be explicit or done internally within Initialize.
The content of "what problems to consider when implementing prototype mode" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.