Prototype mode—creative

Creation type

1. Singleton design pattern
2. Factory design pattern
3. Builder design pattern
4. Prototype design pattern

Structure type

5. Agency design pattern
6, bridging design pattern
7, decoration design pattern
8, adapter design pattern
9, appearance design pattern
10, flyweight design pattern
11, combination design pattern

Behavioral

12. Template design mode

Introduction:

The word prototype indicates that the model should have a template instance, from which the user copies (clone) an object with consistent internal properties. The copied instance is the prototype. If the creation cost of the object is relatively large, and the difference between different objects of the same class is not much (most of the fields are the same), in this case, we can use the existing object (prototype) to copy (or copy ) To create new objects to save creation time. This way of creating objects based on prototypes is called Prototype Design Pattern, or prototype mode for short.

Application scenario

1. Class initialization requires a lot of resources. Creating objects through prototype replication can avoid these resource consumption;

2. Creating an object through new requires very tedious data preparation, which can be used in prototype mode;

3. There is little difference between different objects of a class, most of the fields are the same;

achieve

For front-end programmers who are familiar with the JavaScript language, the prototype mode is a common development mode. This is because, unlike Java, C++ and other class-based object-oriented programming languages, JavaScript is a prototype-based object-oriented programming language. In the Java language, the simplest implementation is to call the clone() method;

Case:

prototype

Book.class is the prototype, copy more Book objects;

import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;

public class Book implements Cloneable{

    private String name;
    private ArrayList<String> mImages = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getImages() {
        return mImages;
    }

    public void setImage(String image) {
        this.mImages.add(image);
    }

    public void showConten() {
        System.out.println("**************** Start **************");
        System.out.println("name: "+ this.name);

        for (String img : mImages) {
            System.out.println("image name: " + img);
        }
        System.out.println("**************** End **************");
    }

    @NonNull
    @Override
    protected Book clone() {
        try {
            Book book = (Book) super.clone();
            book.name = this.name;
            book.mImages = this.mImages;
            return book;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Copy object

public class CloneBook {

    public static void main(String[] args) {
        Book book = new Book();
        book.setName("活法");
        book.setImage("图1");
        book.setImage("图2");

        book.showConten();

        Book book1 = book.clone();
        book1.setName("Android");
        book1.showConten();

        book.showConten();
    }
}

 run:

**************** Start **************
name: 活法
image name: 图1
image name: 图2
**************** End **************
**************** Start **************
name: Android
image name: 图1
image name: 图2
**************** End **************
**************** Start **************
name: 活法
image name: 图1
image name: 图2
**************** End **************
 

Shallow copy and deep copy

There are two ways to implement the prototype mode, deep copy and shallow copy. Either shallow copy or deep copy is the realization of the prototype mode;

Shallow copy: Shallow copy only copies the basic data type data in the object and the memory address of the reference object, and does not recursively copy the reference object and the reference object of the reference object;

The copy of the Book object above is a shallow copy, you can try if you don’t believe it, the code is as follows

public class CloneBook {

    public static void main(String[] args) {
        Book book = new Book();
        book.setName("活法");
        book.setImage("图1");
        book.setImage("图2");

        book.showConten();

        Book book1 = book.clone();
        book1.setName("Android");
        book1.setImage("图3");//修改引用对象的数据
        book1.showConten();

        book.showConten();
    }
}

 operation result:

**************** Start **************
name: 活法
image name: 图1
image name: 图2
**************** End **************
**************** Start **************
name: Android
image name: 图1
image name: 图2
image name: 图3
**************** End **************
**************** Start **************
name: 活法
image name: 图1
image name: 图2
image name: 图3
**************** End **************

It can be seen that the copied object book1 modifies the data (mImages) of the referenced object in the prototype, and as a result the data of the referenced object in the book object is also changed; book and book1 point to the same object address of mImages at the same time;

Deep copy: What a deep copy gets is a completely independent object.

Modify the clone method in the Book class

@NonNull
@Override
protected Book clone() {
     try {
         Book book = (Book) super.clone();
         book.name = this.name;
         book.mImages = (ArrayList<String>) this.mImages.clone();
         return book;
     } catch (CloneNotSupportedException e) {
         e.printStackTrace();
     }
     return null;
}

Re-run the following code

public class CloneBook {

    public static void main(String[] args) {
        Book book = new Book();
        book.setName("活法");
        book.setImage("图1");
        book.setImage("图2");

        book.showConten();

        Book book1 = book.clone();
        book1.setName("Android");
        book1.setImage("图3");//Modify the data of the referenced object
        book1.showConten();

        book.showConten();
    }
}

Result: book1 modifies the data in the reference type object and has no effect on book; this is a deep copy;

**************** Start **************
name: 活法
image name: 图1
image name: 图2
**************** End **************
**************** Start **************
name: Android
image name: 图1
image name: 图2
image name: 图3
**************** End **************
**************** Start **************
name: 活法
image name: 图1
image name: 图2
**************** End **************

 

Guess you like

Origin blog.csdn.net/ezconn/article/details/106754096