Introduction to prototype patterns

The prototype mode mainly solves the problem of creating duplicate objects, which are complicated and may take a long time to obtain data from libraries or RPC interfaces. Therefore, cloning is adopted to save time.

The prototype pattern is a creative design pattern that allows you to copy existing objects without making your code dependent on the class to which they belong.

Characteristics of prototype patterns

  • One of the most important tools needed in prototyping is cloning.
  • The prototype pattern is not used very often.
  • In a very complex hierarchy of classes, when the system must create new objects from many of these types, you can consider using prototypes.

It is easy to clone complex objects, avoid repeated initialization operations, and do not need to be coupled to other classes that belong to the class.

Advantages:

  • Hide the complexity of creating new instances from customers
  • Provides options to enable customers to generate objects of unknown type
  • In some circumstances, copying objects is more efficient than creating new ones
  • You can handle different configurations of complex objects in ways other than inheritance

Disadvantages:

  • Copying objects can be complicated

The structure of the prototype pattern

The Prototype interface will declare clones. In most cases, there will be only one method named Clone.

2. The Concrete Prototype class implements the clone method. In addition to copying the data of the original object into the clone, the method sometimes has to deal with extreme situations in the cloning process, such as cloning associated objects and teasing out recursive dependencies.

Clients can copy any object that implements the prototype interface.

All stereotype classes must have a common interface that makes it possible to copy objects even if the specific class to which they belong is unknown. Prototype objects can produce full copies of themselves because objects of the same class can access each other’s private member variables.

Demo

Let’s take student information as an example to learn the prototype model. Filling in student information when entering school is repetitive and simple. If we set the format well and the rest of the students follow a certain format, then we can use the prototype mode.

    public class Student
    {
        public int Age;
        public DateTime BirthDate;
        public string Name;
        public IdInfo IdInfo;

        /// <summary>
        ///Shallow copy
        /// </summary>
        /// <returns></returns>
        public Student ShallowCopy() 
        {
            return (Student)this.MemberwiseClone();
        }

        /// <summary>
        ///Deep copy
        /// </summary>
        /// <returns></returns>
        public Student DeepCopy()
        {
            Student clone = (Student)this.MemberwiseClone();
            clone.IdInfo = new IdInfo(IdInfo.IdNumber);
            clone.Name = String.Copy(Name);
            returnclone; }}Copy the code
    public class IdInfo 
    {
        public int IdNumber;
        public IdInfo(int id){ IdNumber = id; }}Copy the code
        static void Main(string[] args)
        {
            Student student=new Student();
            student.Name = "Hui";
            student.BirthDate = Convert.ToDateTime("1990-10-08");
            student.Age = 27;
            student.IdInfo = new IdInfo(001);

            Student studentTwo = student.ShallowCopy();
            var studentThree = student.DeepCopy();

            Console.WriteLine("Student Information");
            Console.WriteLine("One");
            DisplayValues(student);
            Console.WriteLine("Two");
            DisplayValues(studentTwo);
            Console.WriteLine("Three");
            DisplayValues(studentThree);
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");


            student.Name = "Seven o";
            student.Age = 18;
            student.BirthDate = Convert.ToDateTime("2018-10-08");
            student.IdInfo.IdNumber = 002;
            Console.WriteLine("Modified Student Information");
            Console.WriteLine("One");
            DisplayValues(student);
            Console.WriteLine("Two");
            DisplayValues(studentTwo);
            Console.WriteLine("Three");
            DisplayValues(studentThree);
            Console.ReadKey();
        }

        public static void DisplayValues(Student s)
        {
            Console.WriteLine(" Name: {0:s}, Age: {1:d}, BirthDate: {2:MM/dd/yy}",
                s.Name, s.Age, s.BirthDate);
            Console.WriteLine(" ID: {0:d}", s.IdInfo.IdNumber);
        }
Copy the code

Explanation:

You can see that when the student object is first created the shallow copy and deep copy values are the same as the original value.

When we modify student information, student AH Hui is changed to AH Qi, and other attributes are also overwritten for modification. In the shallow copy and deep copy data, only the ID value is changed, and other values are the same as the original data. All data output in the deep copy is the same as the original data, that is, the modified data does not overwrite the data in the deep copy. The deep copy copies the original data.

All stereotype classes must have a common interface that makes it possible to copy objects even if the specific class to which they belong is unknown. Prototype objects can make full copies of themselves because objects of the same class can access each other’s private member variables.

For the above sentence, you taste, you fine taste.

Although the prototype pattern is not used very often, we need to have a general idea of how and why to use it.

Small remarks

Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.