1. Introduction
In the commercial housing sales system, housing information is basic information. Before running the system, you must enter information about the house to the system. This is a tedious repetitive task. If you repeatedly enter the type, area, and bathroom style of the room, the system will surely not run. In fact, there are not many types of real estate in a residential area. The difference is only the building number. In addition, the room types in the real estate are also very limited, which provides inspiration for solving the input problem. The logical structure of the project.
A residential area contains multiple real estate projects. One real estate consists of multiple floors, and the first floor includes multiple rooms. Real estate and rooms have their own styles (this model ignores high-end residential buildings and villas). The problem to be solved is how to create these class instances. The data in these instances is basically the same.
Solution 1: manually input the object based on the input data.
While increasing user pain, this solution does not bring more benefits to programmers.
Solution 2: Users enter some basic information and rules to instantiate objects by programs.
The system execution process is as follows.
(1) The user selects to add a new real estate and the system creates a real estate instance.
(2) The user enters the basic information of a room and the floor to be automatically generated, and the system instantiates the information,
And then added to the real estate.
(3) The user repeats Step 1 until entering the data of the entire project.
The user operations in this solution have been greatly reduced, but there are still problems. If the structure of each real estate in a residential area is the same, how can we automatically generate it? To this end, you need to add a building group on the user interface, and then enter the quantity and room information to be generated. This makes the system very inflexible and cannot be expanded. For example, you need to build a similar community. It is also difficult to remove some features, such as the need to automatically generate real estate groups. If an error occurs during automatic generation, for example, if the room number rule is incorrect, the object must be destroyed and processed again. Although a ready-made building already exists in the system (for example, a sold building has the same structure as a new one), it cannot be used.
From the perspective of programs, it is even more difficult to maintain. Every new level-1 input requires changes to the factory method for object creation.
Unfortunately, this scheme is often used in systems that repeatedly input information, making it difficult to maintain the system.
Solution 3: copy/paste.
From the user's point of view, it is best for the system to support copying/pasting. For example, if you copy a ready-made project and paste it, you only need to modify the building number, or copy and paste multiple floors and then add them to the floor automatically. In this way, the user interaction process is much simpler.
2. Overview
2.1 intent
The prototype can solve the problems mentioned in the previous section. We use existing objects (buildings, floors, rooms, and even residential areas) as prototypes. Users can create new objects by copying these prototypes.
2.2 usage
The prototype mode can be used when a system is independent from the creation, composition, and representation of products. In prototype mode, product creation and initialization are completed in the Clone method of the class. In use, we can use some column prototype objects to generate the factory objects of the corresponding objects, and copy, paste, and other operations can be independent of the objects to be copied.
2.3 structure of the prototype mode
The structure of the prototype mode.
(1) Prototype: declare an interface for cloning itself.
(2) ConcretePrototype: implements a clone operation.
(3) Client: let a prototype clone itself to create a new object.
2.4 results
The prototype has the following advantages.
(1) add or delete a product at runtime: You can add the new product type to the system by using the customer's prototype instance, for example, each tool in the toolbox in the configuration software can correspond to a registered prototype object, and the toolbox can be extended by adding a prototype object.
(2) easy to create complex objects: complex elements are often created in software such as graphic editing and configuration. These elements are composed of simple elements. Using the prototype mode, you can easily use complex elements as common elements, so that the toolbox of the software can be expanded automatically.
(3) Reduce factory layers: Because reflection factories can be used in. NET, this advantage is not obvious.
The disadvantage of using the prototype mode is that the clone function is not easy to implement in some cases, especially when the object is referenced cyclically.
3.. NET implementation
3.1Icloneable Interface
. NET has provided the necessary basis for implementation, that is, the ICloneable interface in the System namespace. This interface only supports the Clone method. The class that implements this interface also has the Clone method, and we need to write code to implement the Clone.
3.2 Implementation Structure
If we need a computer class, its attributes include brand, CPU, RAM, and hard disk capacity. If you clone a computer with the same configuration and want this class to become a prototype, the Code is as follows:
Public Class ComputerClass Computer
Implements Icloneable
Private m-CPU As String
Private m-Brand As String
Private m-RAM As String
Private m-HardDisk As String
Private m-Number As String
Public Property Number () As String
Public Property CPUr () As String
Public Property Brand () As String
Public Property RAM () As String
Public Property HardDisk () As String
Public Function Clone () As Object Implements System. Icloneable. Clone
Begin
Dim c As New Computer
C. RAM = m-RAM
C. Brand = m-Brand
C. CPU = m-CPU
C. HardDisk = m-HardDisk
Return c
End Function
End Class
This code is derived from the device management system and simplified. Note that the Number attribute is not copied when the code is copied. This attribute is used to distinguish the device Number of a computer and cannot be duplicated. Therefore, it should be assigned by the customer program or generated by the serial Number generator.
3.3 deep replication and light Replication
If we only look at the sample code, it is not difficult to implement the prototype mode. However, the complexity is that if an object is a composite object or contains other objects, will the clone object only copy the object itself or copy it together with the referenced object?
For example, when cloning a project, we want to obtain all new data about the project for modification. At this time, when copying the Real Estate, copy the Real Estate object and all the room objects contained in it, and belong to the new building. In this operation, the room is replicated indirectly. In this case, it is a deep copy. That is, except variables of other objects, all variables of the Copied object contain the same value as the original object. Variables of other objects referenced will point to the copies of the original variables.
Sometimes we do not want to copy included objects, but need to copy the objects contained in the object still point to the original reference. For example, the cloned user list contains user objects, and the cloned user list does not need to copy the included user objects. In this way, no matter which list of users is modified, the user information can be reflected in another list. In this case, it is a light copy. That is, all variables of the Copied object contain the same value as the original object. In other words, all variables that reference other objects still point to the originally referenced object.
5. prototype mode in. NET
Many classes in. NET support the prototype mode. For example, if we want to obtain a dataset with the same structure as the existing one, we can use the clone method:
Dim dsnew As DataSet = ds. Clone
Note that there is also a method for copying data-related classes. Different from cloning, this method not only copies the structure of the object, but also copies the data contained in the object. For example, if we want to copy the structure and data of an existing dataset, this method is used:
Dsnew = ds. Copy
When using Clone, you must note whether it is deep replication or light replication .. The IClone interface class in. NET basically adopts the shortest replication policy. developers need to implement the IClone interface in scenarios where deep replication is required.